GD or ImageMagick?

hi all,
i want to know the advantages and disadvantages of GD and imagemagick.
i referred a lot of sites, but came across different opinions.

i don’t need animated gifs. also i know that GD comes preinstalled with php. what are the other differences?

which takes less time?
which consume less resources?

From my personal experiences ImageMagick has been much quicker and consumed alot less resources then its counterpart, GD.

Although it would probably be best if you did some sort of benchmark on your own server.

As far as I have heard, Imagemagick has more features than GD and can easily manipulate the images. But since it is third party library all the servers might not have it. GD in itself is also not a simple one it can do a lot for you with their available functions. Also as far as I know Imagemagick commands are run with PHP’s exec or system functions so in some servers might have disabled or not allowed those functions. I am not experienced in Imagemagick.

Found a small benchmarking is here http://www.jamesarmes.net/blog/2009/03/php-image-benchmarks-gd-vs-image-magick which might be helpful to you.

Lets see what GD/Imagemagick gurus have to say here. :slight_smile:

I prefer ImageMagick:
More options
Easier to write but can be complicated at first
Quicker than GD
Uses less memory - in some speed tests I did the GD file would not resize. I suppose I could have changed some memory limit if I had access to the server.
The images were a better quality than GD but GD has improved in release 2.0 and is getting more development - nothing seemed to be done to it for a few years.

Imagick is now built into php but again you need ImageMagick installed as well ( you can check in your php.ini file ). It depends on what operators the developers have used from the hundreds of available Imagemagick ones. It does not seem very well supported - but that is why I started my site with php and Imagemagick examples a few years ago.

Some examples of GD and Imagemagick code: http://www.rubblewebs.co.uk/imagemagick/compair.php
Some speed tests: http://www.rubblewebs.co.uk/imagemagick/notes/speed.php

Its funny Ruju that the link you provided for speed tests have completly the opposite results to mine !

yes Rubble, you are right.

according to http://www.rubblewebs.co.uk/imagemagick/notes/speed.php - IM is faster
according to http://www.jamesarmes.net/blog/2009/03/php-image-benchmarks-gd-vs-image-magick - GD is faster

have anyone tried out a speed test on GD & IM themselves?

Actually I am not the benchmarker for that case I just googled and found. So Rubble must be sure about his benchmarking because he is working on IM since a long and helping guys here in SPF since a long. So follow what Rubble has suggested.

i had been over an issue with jpg images and all.

Suppose an image is of size 100 x 100. If i change its dimensions to 101 x 101 or 99 x 99, it gets blurred.
even a small change of 1 pixel makes the image blurred.

when i did a little search, i got that if we use vector images, the .svg files, this problem will be solved.

i had been using GD to make these images. using ImageMagick is there a way to solve this? i only want the image to retain the quality when the image’s dimension is changed by a small amount, say 4px.

using ImageMagick, is it possible?

using Imagick::tintImage , ImagickDraw::setVectorGraphics or some other functions, can we achieve the effect of svg images in jpegs and all?

just for clarification - i resized the original image using GD’s functions and it was having good quality. suppose the resized image is having a dimension of 100 x 100, and if i change the image’s width and height in the <img> html tag, the image gets blurred. is there a way to avoid this, so that if the width and height in the <img> tag is varied by a small quantity, the image does not get much blurred

Do not adjust the dimensions on the image in the HTML IMG element. That is how you solve it.

Oops!! isn’t there a better solution? :wink:

Imagemagick is not made for svg and there are probably better programs.

I am losing track of what you actualy want to do; just resize an image? On the fly ? On upload ?

Imagemagick can resize a 100x100px image to 101x101px but everytime you save a jpg you will get some compression and so image degrading.

I do not know much about Imagick use.

From my experience most people don’t understand that you can pretty much get the same result from GD than that of ImageMagick; people just don’t use GD properly.

The main difference between GD and IM I see is the simplicity.
For example resizing an image and keeping the aspect ratio ( some old code and may be able to be improved upon ):
GD


<?php
// Temporary upload image name
$original_image = '../original_images/flowers.jpg';
// Get the image dimensions
$size=GetImageSize( $original_image );
// Maximum image width
$max_width = "100";
// Maximum image height
$ratio = 100/$size[0];
$max_height = $size[1]*$ratio;

// Resize the image and save
$src_img = ImageCreateFromJPEG( $original_image );
$thumbnail = ImageCreateTrueColor( $max_width, $max_height );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $max_width, $max_height, $size[0],$size[1] );
ImageJPEG( $thumbnail, 'flowers_GD.jpg' );
ImageDestroy( $thumbnail );
?> 

Imagemagick


<?php
// Temporary upload image name
$original_image = '../original_images/flowers.jpg';
// Maximum image width
$max_width = "100";
// Maximum image height
$max_height = "100";
// Resize the image and save
exec("convert $original_image -thumbnail {$max_width}x{$max_height} flowers_IM.jpg");
?>

I know once the code is written it would be unlikely to be visited again unless there is a problem.

This is a batch script version - creates two images and save to two different folders.
I was using a php script on upload but its quicker running this on my PC and uploading.
Within reason you can keep building on the command to get what you want - this is why I prefer ImageMagick over Imagick.


convert.exe &#37;1 -auto-orient ( +clone -resize 900x570 -bordercolor Black -border 1x1 -font handsean.ttf ^
 -fill black -pointsize 40 -gravity southeast -annotate +13+13 "Anthony" -font handsean.ttf ^
 -fill white -pointsize 40 -gravity southeast -annotate +15+15 "Anthony" ( +clone -background black -shadow 85x5+6+6 ) ^
 +swap -background white -layers merge +repage -write "C:\
ormal\\%~n1.jpg" +delete ) ^
 -thumbnail 150x80 -unsharp 1.5x1+0.7+0.02 ( +clone -background black -shadow 85x3+3+3 ) ^
 +swap -background white -layers merge +repage -quality 80 "C:\	humbs\\%~n1.jpg"

Example results:

Other reasons I prefer IM over GD is also the active development it has - stuff like seam carving resize has made it into imagemagick in a quite short space of time from when it was first popularised as a image processing concept. Batch processing is another, mogrify sure beats writing a bunch of loop code. I now use imagemagick on the desktop a lot, I can have a folder of images processed before photoshop has even opened.

With regards to speed, if you need more speed it’s worth looking at graphicsmagick (fork of IM from a while back) that is supposed to be a lot faster on many common image processing tasks.

And from my experience - not many people know that in certain areas GD cannot match the same image quality as IM, because the differences are not visible at first sight before making direct comparisons. I’m talking here about 2 operations which are pretty commonly used in web site applications:

  1. Resizing images. Very often it is used for shrinking images uploaded by users - GD uses a very simple bilinear algorithm which produces pretty blurry result. IM uses Lanczos by default - one of the best resizing methods available - pretty sharp and detailed results (and it surpasses Photoshop big time!).

  2. Saving JPEGs. Again, very often used - after downsizing a picture you want to save is as JPEG for web display. GD has a limit to JPEG quality even if you set it to the max because it has a chroma subsampling active and can’t be turned on. This results in minute saturated details being blurred, smoothed out and desaturated (for example thin vivid lines, patterns of colourful fabrics, etc.). In IM, however, you can set subsampling to what you require or turn it off so that the JPEG will be virtually indistinguishable from the original. Not many people know what chroma subsampling is so they assume that quality 100 in GD is best - in fact it is far from best - unless your material is already monochromatic or unsharp.

These two differences are enough for me to always choose IM for photo manipulation. I just want the best possible quality. Sure, if you only want to produce thumbnails it may not be a problem since usually you want thumbnails to be highly compressed and of moderate quality. But if you want to show off sharp, detailed, high quality photos this may be important.

GD is easier to set up, as it comes bundled with PHP, wheras ImageMagick needs to be set up. If you both have them up-and-running, then go for ImageMagick, as it has a sightly better performance and support for more image formats.
Main difference between the two is the way batch-add is being done: when using GD, one pic after the other is processed, which is good in terms of server load. Drawback is that you might have issues resizing larger pics (both in terms of dimensions and filesize), as you’re limited by the memory assigned to PHP. ImageMagick on the other hand is quite resources-hungry, and it will start a separate process for each pic that needs to be resized during batch-add, which can bring the server down.
Bottom line: both have advantages and disadvantages. Your choice. If the server is yours to administer and you know what you’re doing, go for ImageMagick. Won’t hurt to test both, you can change the library at any time.

<snip/>

I can speak from experience here…

If you are resizing large images then you must use IM. I was trying to resize 5MB JPGs and GD/PHP always crashed.

This mainly happened when the JPEG had large areas of white which compressed easily. When PHP tried to load the JPEG into memory the memory requirments were HUGE.

Also, the image quality was better IM.

Will