Some comparisons between ImageMagick and GD

While in discussion on a forum the speed of GD against ImageMagick came up and I tried a couple of speed tests.

With the examples below the flower image resize was twice as fast in ImageMagick 0.083450 seconds against 0.160992 seconds in GD. But the text generation image was 35 times faster in GD 0.003110 seconds as apposed to 0.119943 seconds in ImageMagick.
I am told that if you use one of the other ImageMagick API's they are faster.

In reality unless you are doing a lot of processing you are not really going to notice the difference.

I have looked into this a bit further and found that if you are doing a small amount of large image conversions it is quicker to use ImageMagick than GD. If you are doing a lot of smaller image conversions it is better to use GD. This seems to be due to the extra time involved using exec( ). Things can be speed up if you use an API like Imagick.

GD Icon I have uploaded some more GD example code.

GD

Image resized using GD
You will notice that the ImageMagick code has retained the "aspect ratio"
whereas the GD code has resized the image to 100x100.
To get GD to keep the aspect ratio you would have to
do some more calculations on the original image size.

<?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
$max_height "100";
// Resize the image and save
        
$src_img ImageCreateFromJPEG$original_image );
$thumbnail ImageCreateTrueColor$max_width$max_height );
ImageCopyResampled$thumbnail$src_img0000$max_width$max_height$size[0],$size[1] );
ImageJPEG$thumbnail'flowers_GD.jpg' );
ImageDestroy$thumbnail );
?>

To keep the aspect ratio based on the original image width:

<?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
//$max_height = "100";


$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_img0000$max_width$max_height$size[0],$size[1] );
ImageJPEG$thumbnail'flowers_GD.jpg' );
ImageDestroy$thumbnail );
?>

ImageMagick

Image resized using ImageMagick
This code was to demonstrate the speed test differences.

<?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
$max_height "100";
// Resize the image and save
exec("convert -size {$size[0]}x{$size[1]} $original_image //
-thumbnail 
{$max_width}x{$max_height} flowers_IM.jpg");
?>

The code can be reduced to this:

<?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");
?>

GD

Image created containing some text with GD
<?php
$canvas 
imagecreate200100 );

$black imagecolorallocate$canvas00);
$white imagecolorallocate$canvas255255255 );

imagefilledrectangle$canvas9918989$white );

$font "verdana.ttf";
$text "Title";
$size "30";

$box imageftbbox$size0$font$text );
$x = (200 - ($box[2] - $box[0])) / 2;
$y = (100 - ($box[1] - $box[7])) / 2;
$y -= $box[7];

imageTTFText$canvas$size0$x$y$black$font$text );

imagejpeg$canvas"Label_GD.jpg" );

ImageDestroy$canvas );
?>

ImageMagick

Image created containing some text with ImageMagick
<?php 
exec
("convert -background white -fill black -pointsize 30 //
-font verdana.ttf -gravity Center -size 180x80 label:
$text //
-mattecolor black -frame 10x10+0+0 Label_IM.jpg"
);
?>

GD

An image watermarked with text using GD
The rotation of the text would not work; I assume you need
to save an extra tempory image and rotate that.

<?php 
$canvas 
imagecreate200100 ); 

$black imagecolorallocate$canvas00); 
$white imagecolorallocate$canvas255255255 ); 

imagefilledrectangle$canvas00200100$white ); 

$font "../fonts/verdana.ttf"
$text "Sunflower"
$size "30"
$degrees "30"
$photo"../original_images/sunflower.jpg";

$box imagettfbbox$size0$font$text ); 
$x = (200 - ($box[2] - $box[0])) / 2
$y = (100 - ($box[1] - $box[7])) / 2
$y -= $box[7]; 

imageTTFText$canvas$size0$x$y$black$font$text ); 

imagecolortransparent $canvas$white ); 

imagerotate$canvas$degrees0); 

imagepng$canvas"temp.png" ); 

ImageDestroy$canvas ); 

$watermark imagecreatefrompng('temp.png');   
$watermark_width imagesx($watermark);   
$watermark_height imagesy($watermark);   
$image imagecreatetruecolor($watermark_width$watermark_height);   
$image imagecreatefromjpeg($photo);   
$size getimagesize($photo);   
$dest_x $size[0] - $watermark_width 100;   
$dest_y $size[1] - $watermark_height 200;   
imagecopymerge($image$watermark$dest_x$dest_y00$watermark_width$watermark_height50);   
imagejpeg($image'watermark_GD.jpg');   
imagedestroy($image);   
imagedestroy($watermark);   

unlink'temp.png'); 
?> 

ImageMagick

An image watermarked with text using ImageMagick
# As the rotate text did not work for GD I have left it off from this example as well.
It is quite easy to rotate the text on ImageMagic you just alter the -annotate operator.

<?php

$font 
"../fonts/verdana.ttf"
$text "Sunflower"
$size "40"
$degrees "30"
$photo"../original_images/sunflower.jpg";

exec("convert $photo -font $font -pointsize $size -fill rgba(0,0,0,0.4) //
-gravity north -annotate +0+25 
$text watermark_IM.jpg"); 
?>

An other good thing about Imagemagick is it supports a lot more image types. Unlike GD where you have to use imagecreatefromjpeg for jpg's and imagecreatefrompng for png's etc. you can give the same line of code any image type you like; you can then save it as any image type you want and not worry about wether it is a jpg, png, pdf etc.