2

I am using imagecreatefromgif / imagecreatefromjpeg / imagecreatefromjpeg functions with uploaded images and sometimes get an error (e.g. Warning: imagecreatefromgif(): ... is not a valid GIF file) when the image is corrupted. I read lots of posts about this topic and couldn't find a "working" answer.

I did try some of the following to validate the image (as suggested on other posts) but none of them worked in ALL situations. If the image does not have the header info, the following works, but if the image has the header/mime type info and is corrupted, it doesn't work.

if( imagecreatefromjpeg($uploaded_image) !== false ) {
    // image is okay.
}

OR

try {
    $test = imagecreatefrompng($uploaded_image);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

OR

using getimagesize($uploaded_image); // will return false or an error if image is not valid.

None of those situations worked with a corrupted image file that has the header info/mime type info.

I would like to prevent having those errors and if possible detect if the image is not corrupted and can be used without problems with any of the imagecreatefromgif / imagecreatefromjpeg / imagecreatefromjpeg functions. If corrupted, I would like to display an error message instead of running the funciton. I am looking for a solution in PHP.

Thank you for your help and suggestions.

1

1 Answer 1

8

You can try

$gd = @imagecreatefromstring(file_get_contents($file_path));    
    if ($gd === false) {
        throw new Exception ('Image is corrupted');
    }

It should work with most image formats known to gd. Also if you need specific error message you can use error_get_last().

@ will suppress error messages, and imagecreatefromstring tries to open known image formats. If this fails, $gd will have value 'false' and no error messages are thrown.

Edit:

Please not that in this example @ operator also will suprpess any errors from file_get_contents function.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the best solution I could think of. All other solutions I found would analyze the URL for the file type, but this one does not need to and is way more reliable; especially if the file type is not visible from the URL. Also, it saves a lot of code. Thank you.
Nice one! I could process a lot of files with this but I have a situation (just 1 file) where script says "Image is corrupted" but I can see the image in the browser (Firefox 32.0), so I'm guessing that GD is not prepared to surpass small errors (again, image displays correctly in the browser, also in image viewers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.