php code exicution time

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

php code exicution time

Post by Bonzo »

In reply to a question on another forum I did a test and here is the post:

Thats strange; the code for GD in the example I tried is:

Code: Select all

$canvas = imagecreate( 200, 100 );

$black = imagecolorallocate( $canvas, 0, 0, 0 );
$white = imagecolorallocate( $canvas, 255, 255, 255 );

imagefilledrectangle( $canvas, 9, 9, 189, 89, $white );

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

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

imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );

imagejpeg( $canvas, "Label_GD.jpg" );

ImageDestroy( $canvas );
The code for ImageMagick was:

Code: Select all

$text = "Title";
exec("/usr/local/bin/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");
The run time for the GD code was 0.003138 seconds and the time for ImageMagick was 0.111307 seconds. So GD was 35 times faster ? although the code was 14 lines and the ImageMagick was 2 lines !

Unless you are processing a lot of images the difference is not worth worrying about but I think its an interesting result.
Is it that GD is built into php and ImageMagick is an external program ?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: php code exicution time

Post by magick »

You would need to code in MagickWand For PHP to make a reasonable timing comparison between GD and ImageMagick. You would also need to run the test multiple times in a loop and take the average time. ImageMagick does have a bit of start-up overhead when it reads it configuration files from disk. However, once its loaded it performs quite well.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: php code exicution time

Post by Bonzo »

I have done another test for the post and the result was:

I have a couple of problems at the moment and so I have just knocked together some code to resize an image.
GD method

Code: Select all

// Temporary upload image name
$original_image = '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_img, 0, 0, 0, 0, $max_width, $max_height, $size[0],$size[1] );
ImageJPEG( $thumbnail, 'flowers_GD_time.jpg' );
ImageDestroy( $thumbnail );
ImageMagick method

Code: Select all

// Temporary upload image name
$original_image = '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("/usr/local/bin/convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height flowers_IM_time.jpg");
GD did the resize in 0.162674 seconds and Image magick took 0.082352 seconds. So in this case ImageMagick was 50% faster.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: php code exicution time

Post by anthony »

As 'magick' mentioned. To properly compare the two you need to use MagickWand PHP module, or at least the 'Magick' PECL module for PHP.

Calling the command line adds a exec process system call, shell interpretation, IM option interpretation and image read/write overhead to the comparision.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply