Recently I’ve been working with image manipulation in PHP, using GD and ImageMagick. Both libraries are very useful but they’re quite different in a number of ways. I thought I’d take this opportunity to go over some of the differences and provide some suggestions on which might be better for different uses.
Batch processing.
ImageMagick has it, GD doesn’t. IMagick (as the PHP extension is called) allows you to load multiple image files with one object and perform the same operations on all of them at once. Of course with GD you can always open each one in turn and perform the operations, but this is a little less efficient both in terms of code written and processing time.
Built-in.
Since version 4.3, a version of GD has been bundled with PHP by default. This wouldn’t even be that big a deal, except setting up IMagick for PHP is quite a hassle. Especially on Windows. Especially with IIS. It’s not a configuration I’d usually recommend for production, but it’s what I most often run for local development. After looking through countless sets of instructions on which DLL to use, many recommending downgrading the ImageMagick program version by version until finding one that worked with the extension, or suggesting the Apache (which I wasn’t using,) the DLL, and ImageMagick all be compiled with VC6, I finally found a configuration that worked. In my case, I just had to upgrade PHP to 5.3. The latest version of ImageMagick (7.1.0-49) runs on Linux, Windows, Mac OS X, iOS, Android OS, and others. That being said, lots of hosting services will have IMagick installed already.
Image composition.
IMagick has some really cool ways of combining images, but Imagick::compositeImage forces you place an entire image on another entire image. Of course you can crop it first, but you’ll want to clone it before that if you’re still going to need the source image. On the other hand, GD’s imagecopyresampled in particular allows you to very easily copy a portion of one image to another, resizing and even interpolating pixels. To do the same in IMagick, it’ll take several steps. On the upside, IMagick is smart about handling pixel buffers, so cloning the image resource to crop it won’t result in doubling your memory usage. Further, IMagick allows copying of specific channels which can be quite useful in a number of situations.
Speed.
IMagick has a lot of functions built in that GD doesn’t, and because it runs as a separate program through an API, it’s not forced to run at the highly-abstracted speed of uncompiled PHP. Therefore, for many complicated functions, such as blurring and shadows, in my experience, IMagick is much faster. When applying a shadow to a 2400 x 2400 pixel image, using IMagick’s clone, paint opaque, blur, evaluate, and compose functions (because the built-in shadow function didn’t have enough options,) it took about 30 seconds, whereas pixel-by-pixel manipulation in GD timed out at 300 seconds.
Ultimately, the specifics of what you’re doing will determine which library is most suitable for your situation. If you’re converting 100 images to grayscale thumbnails with sky blue borders, you’ll be best of with IMagick. But if you’re adding a watermark from an image sprite dynamically one image at a time, GD might be a little easier. If you’re not sure about your production environment, you might also want to consider that GD is bundled with PHP and IMagick isn’t. And of course there are other image manipulation libraries for PHP as well.