Image Editing: GD2 vs. IMagick 3
I asked Maxwell to set this blog up so we can talk about any of the issues that we who labor for brokendisk encounter, to serve as a place to brag or weep.
So. I recently needed a library to do some very basic image editing in a php script. Mostly I wanted to resize an image but there may be a possible future need for watermarking images, creating captchas, who knows..
The two options I hear people talking about are IMagick (which is the PHP interface for the ImageMagick library) and GD 2. At the time of this blog post, both are natively supported by a properly configured PHP and simply need to have their modules included in the php.ini, so setting up either was a breeze. Both have functionality to do all the things I mentioned above and much, much more.
It sounds like GD used to be more widely available and did not support certain filetypes, but fortunately has an active enough development community to solve this problem faster than Google could flush their old search results ;) GD2 supports every image file type I could ever conceive of using, as does ImageMagick.
They both have very strong supporters but I haven’t found many people who have used both and made an objective comparison of the two. I decided to do some benchmarking for my _very specific_ needs, which is to resize an image into a 130×130 thumbnail.
I got this image from my old favorite ballache blog #uncov and resized it 10 times with each library. I ran the test a couple of times but didn’t aggregate the results, each sample was about the same and.. come on, I’m a computer scientist not a real scientist. It’s a testament to both libraries that I had to up the number of iterations by two orders of magnitude to even get significant results, but it seems like over 1000 iterations GD2 is just a hair faster:
jon@mabel:~$ php image-test.php
GD finished in 8 seconds.
Imagick finished in 13 seconds.
jon@mabel:~$ php image-test.php
GD finished in 9 seconds.
Imagick finished in 11 seconds.
jon@mabel:~$ php image-test.php
GD finished in 8 seconds.
Imagick finished in 13 seconds.
IMagick library resized image
GD2 library resized image
They both look really good to me, here is the
code to run the test.
Tasting notes:
Setting up GD on OS X was a real pain in the butt but getting it going on linux was super easy, it’s a registered package in any of the aptitude-like package managers. I never bothered trying to get Imagick going on OS X but it seems like it’s doable, and one added bonus of that is you get all the tools that your PHP script can use in command-line form as well.
php.net has really nice documentation for GD, Imagick is not so well-documented.. all the function signatures are in there but you’ll run into pages like this…
Now that all the science is out of the way, this is about to get reeeal subjective. I used either library for all of 20 minutes, they are both really easy to get going and to achieve what you need to do. I really can’t imagine needing to do anything significantly more complex than what I’ve done above (in a web application). It seems like ImageMagick tries to dumb things down for you and abstract all the actual processing, whereas GD is pretty honest and gives you options that seem more intuitive. For example, with the GD image resizing function you take your source image and draw a box with integer coordinates, and this becomes your new resized image. With the Imagick library, the default action is to not lock the aspect ratio and distort the image, which doesn’t seem to me like something you’d ever want to do, although you do have the option to leave either the X or Y column parameter as 0 and it will retain the aspect ratio. I like how ImageMagick is organized into what seems like more of an object orientated approach whereas GD is just a collection of static functions.
In the end, it was a moot point because DreamHost did not have ImageMagick enabled by default, and they have a weird policy about installing programs and modifying ini files. It’s tiresome to figure out the DreamHost way to do things over and over again so I just went with GD2.
I think the only real conclusion I can draw from this is that anyone with image library needs is very lucky to have such great open source options.

