Cropping and resizing a GIF with PHP’s imagick extension

I work for Norway’s largest web site and on our site we have a daily Dilbert comic. We receive these comics in large TIFF files and we have previously done some manual work to generate a nice preview and a scaled version of the comic. Manual work sucks so I have written a small PHP script that uses ImageMagick (using the imagick extension) to do all the work for us. The imagick part of the script looks something like:

// Open a new image
$image = new Imagick($filepath); // $filepath is a path to a TIFF file

// Scale the image
$image->resizeImage(600, 189, imagick::FILTER_LANCZOS, 1);

// Store the scaled version
$image->writeImage($outputDir . '/' . $outputFilename);

// generate a thumb that includes the first tile of the comic
$image->cropImage(194, 188, 0, 0); 

// Scale the thumb
$image->resizeImage(115, 111, imagick::FILTER_LANCZOS, 1);

// Store the thumb
$image->writeImage($outputDir . '/' . $outputThumbFilename);

Now … at first glance this should work pretty nice. Well … it doesn’t!

Take a look at the two files that are generated:

Scaled version of the comic

Scaled version of the comic

Thumbnail of the comic

Thumbnail of the comic

As you can see the thumbnail is fubar. Mats happened to figure out that the image actually is 115×111 but the header of the GIF says otherwise. Because of this image viewers (like a browser for instance) will show the image inside the size specified in the header. Sounds like a bug in the crop function in either ImageMagick or the imagick extension.

To fix the problem we need to set the page size before we store the thumb:

// More stuff (see snippet above)
// ...

// Scale the thumb
$image->resizeImage(115, 111, imagick::FILTER_LANCZOS, 1);

// Set the page size
$image->setImagePage(115, 111, 0, 0);

// Store the thumb
$image->writeImage($outputDir . '/' . $outputThumbFilename);

The result of this is a correct thumbnail:

Correct thumbnail

Correct thumbnail

Hopefully Scott Adams won’t mind me displaying one of his comics in my blog. If you (Scott Adams) happen to read this, and want them removed, just say the word. :)

Advertisements
This entry was posted in PHP, Technology, Work related and tagged , , . Bookmark the permalink.

10 Responses to Cropping and resizing a GIF with PHP’s imagick extension

  1. Image Magick is awesome. I use it with PHP for the development of my upcoming Social Network and for a Classified Ad site that I’m working on for a client.

    I found it when I was looking for an alternative to PHP’s Image Processing and Generation which is limited when it comes to simple cropping and thumb nailing. Most of the effects/filters that are enabled in Image Magick blow away anything PHP’s other Image Processing modules have to offer.

    It’s quick and convenient when your in larger development and you don’t want to spend your time writing a new image cropping class for a few thumb nails.

    Peace Out!

  2. Pingback: PHP, ImageMagick and Cropping to GIF: Digging into GIFs again! | Mats Lindh

  3. Ed says:

    the smaller one still looks unsharpened, why not just take a second and alter in paint.net or something? it can’t be that much slower and you’re assured a high quality pic

  4. christer says:

    @Ed: I would like to avoid any manual work because we publish these every day. The thumb is not optimized so I can probably make it look sharper with the imagick extension anyways. :)

  5. andrewacomb says:

    Great tutorial, I’m going to start using it today.

  6. senthil says:

    hi
    how can i download this script? i am not able to get download link.

  7. Carl says:

    Thanks for the write up. This was exactly the issue we were running into and simply adding a -page geometry option to the crop command worked like a champ. As a bonus, jpgs don’t appear to be affected by the new option either.

    We are not using imagick, so if it is a bug, it resides in ImageMagick itself.

  8. PhpCatalog says:

    thanks..will this work with jpg also?

  9. Josh says:

    Christer, just a quick note to thank you for posting this, it would have taken us a long time to figure this one out, and your solution solved the issue completely. Thank you!

  10. Sam says:

    Hi,
    Thanks a lot for nice Article,Can tell how to set the density and resample in image magick using php.

    have dream day

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s