简体   繁体   中英

PHP Imagick Fatal Error on PNG file

I got script creating thumbnails , the problem got today that some images creating fatal error at this line:

$im = new imagick($filename);

Below is the error message -

Fatal error: Uncaught exception 'ImagickException' with message 'corrupt image `imagename.png' @ error/png.c/ReadPNGImage/3789' in common.php:370 Stack trace:

0 common.php(370): Imagick->__construct('...')

After some research I notice that it could be bug of imagick with some png files, this happens 1st time so its probably very rare

How to handle this error so it wont stop script ?

I thought about validating image format before but did not find anything working.

How to handle this error so it wont stop script ?

As pointed out in the comments, you need to handle the exception to allow the script to continue.

try {
    $im = new Imagick($filename);
} catch (ImagickException $e) {
    $im = null;
    // .. handle error ..
}
if ($im) { // .. do work ..

I thought about validating image format before but did not find anything working.

There's the Imagick::pingImage method to help identify meta-information about an image. This can be used to ensure the file matches what the application is expecting -- without decoding the image-data into memory.

However the image your working with seems to have bad chunk-data during the decoding sub-routines, so pinging the image wouldn't catch it. Corrupted images are common, and your solution should anticipate it with error handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM