Moved filename extraction
Const
Clearer retval check
Write image to a temp file first
Simplified limiting of file size
Formatting