Friday, October 29, 2010

ICO image processing with Imagick

Used to have an issue with Imagick when I convert ICO image into png. Somehow when I create the Imagick object from image content string, the final result is a crappy image. But when I create the Imagick object directly from a file, it works fine, with only the problem of a different size.



So, I filed a bug (http://pecl.php.net/bugs/bug.php?id=15701), but still got no resolution.



Got some time recently and did a bit research, found that the problem is with the ICO format: it may contain the same image with different sizes. So, when you convert the format directly, it will not work. It is similar in the case when you try to convert an animated GIF image into JPG or PNG, you get one of the frames instead of an animated image.



There are two solutions:




  1. use foreach($ico as $im) loop to get one image and just convert that image

  2. use flattenImages function before the format conversion



Note that after the format conversion, the image dimensions might be different from when you directly open the ICO image from a browser or image viewer. You probably need to resize the image manually.



The bug report has some code snippets if you want to try it out.

No comments:

Post a Comment