php - Handling unknown images with imagick -


as people have found, when attempting load file imagick cannot handle whatever reason, throw exception cannot caught.

i created ico file , attempt load using readimagefile():

$image = new imagick(); $handle = fopen('icon.ico', 'rb'); $image->readimagefile($handle); 

this throw:

php fatal error:  uncaught exception 'imagickexception' message 'no decode delegate image format `' @ error/constitute.c/readimage/501' 

when specify image ico file, works. code runs fine:

$image = new imagick(); $image->setformat('ico'); $handle = fopen('icon.ico', 'rb'); $image->readimagefile($handle); 

using readfile instead of readimagefile, apparently looks @ extension, because code runs fine:

$image = new imagick(); $image->readimage('icon.ico'); 

however, when rename ico file icon.jpg , run this:

$image = new imagick(); $image->readimage('icon.jpg'); 

it fails following error:

php fatal error:  uncaught exception 'imagickexception' message 'not jpeg file: starts 0x00 0x00 `icon.jpg' @ error/jpeg.c/jpegerrorhandler/322' 

the following code, however, handles ico file renamed icon.jpg fine:

$image = new imagick(); $image->setformat('ico'); $handle = fopen('icon.jpg', 'rb'); $image->readimagefile($handle); 

obviously best way handle images not @ extension, anything, @ actual file. imagick apparently fails this. have php functions such mime_content_type(), getimagesize() , finfo_buffer() (which php.net recommends think). return "image/x-icon", setformat() function not take.

what best way go this?

it's true can not trust filename, file formats not have unique magic file signature, or have vague identifier. when imagemagick reads image, it'll attempt trust magic signature first, , fall-back file extension if can't resolve image type.

as you've correctly discovered, reading image file descriptor doesn't allow imagemagick fall-back on file extension, , you're responsible telling imagick format before calling imagick::readimagefile.

what best way go this?

you have explicate what's allowed. built-in fileinfo methods mime_content_type & finfo_buffer start (but not fool proof). if of file descriptor open, recommend "peeking" @ data...

$filename = 'untrusted'; $handle = open($filename, 'rb'); // allow common & modern formats work expected. try {    $image = new imagick();    // pass original file name im fall-back    $image->setfilename($filename);    $image->readimagefile($handle); // attempt handle specific formats. } catch (imagickexception $e) {    // create new instance    $image = new imagick();    // rewind fd    fseek($handle, 0);    // read first 4 bytes    $peek = fread($handle, 4);     // collect mime-type    $mime = mime_content_type($filename);     // explicitly allow known formats. (also see http://www.garykessler.net/library/file_sigs.html)    if ($mime == 'image/x-icon' && $peek == "\x00\x00\x01\x00") {        $image->setformat('ico');    } else if ($mime == 'image/x-icon' && $peek == "\x00\x00\x02\x00") {        $image->setformat('cur');    } else {      // error handle    }    fseek($handle, 0); // rewind again.    $image->readimagefile($handle); } 

ymmv, , i'm sure there's better php folks can help.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -