Using ImageMagick and GD2 together with Drupal ImageCache


A quick (working!) introduction to using ImageMagick in custom ImageCache actions while using GD as your default toolkit. Posted by Thomas Sutton on October 11, 2010

A lot of Drupal sites use ImageCache and ImageAPI to automatically scale and process images for display. Most of them (the vast majority, unless I miss my guess) will be using the GD toolkit and running with mod_php embedded in Apache. This is fine in most cases, but sometimes it’s definitely the wrong thing to do; in my case, I’m processing large image files (~4M or so) and keeping my memory limit down. The easiest way to do this is to offload the processing to another process. Something like ImageMagick perhaps? With a custom action from ImageCache Actions, this is simple!

A quick Google led me to an article called Create PDF thumbnails with imagecache and ImageMagick while GD is still the default toolkit which supplies the following code (simplified a little by removing the PDF-y bits):

Alas, this code it pretty useless: because it stomps on $image->info any further actions on this $image will probably break.

Thankfully, there’s an easy fix: when you update $image, make sure you update everything that needs fixing. Here’s the amended code:

Make sure that you’ve enabled the ImageMagick toolkit, drop this code in a custom action and you’ll be on externally processing images in no time!

This post was published on October 11, 2010 and last modified on November 4, 2021. It is tagged with: php, images, resize, imagemagick, gd.