ImageMagick vs GD Benchmark – Resize image in PHP Script

From curiosity and for my own knowledge, I wanted to test whether there is any significant difference in performance between ImageMagick and the GD php library, and it seems like yes, there is a difference and it is not at all small, is huge!

The test was done on a local Windows server, based on XAMPP stack, version 7.3.0. The basic hardware configuration of the server:

For the first time I installed ImageMagick what gave me a little headache, 3 separate archives, a change in php.ini and manual file copying, but in the end everything was perfect, with no problems!

On a Linux server it still did not do the test, because I did not have the patience to install ImageMagick there, but I come back with details in the future.

I do not go into details, because the image in the article says exactly what’s needed:

“imagick” is much faster than “gd” , even 3 times more faster!

benchmark gd-vs-imagick php performance

I have not tested the entire library but only what is most important for a wordpress photo gallery site: resizing images.

Unfortunately, on the Windows platform, ImageMagick does not come with OpenCL enabled, meaning it does not automatically use the best performing device: CPU or GPU. is compiled only with the OpenMP (Open Multi-Processing) option that only uses the processor in a more efficient way. From ImageMagick documentation, the performance using a GPU can be very large, even 10 times higher (a magnitude order). It remains to be seen in the future that there will be results on linux with OpenCL, but for now the results are clear:

GD is far inferior than ImageMagick!

Ca si test, practic am redimensionat o imagine in format JPG de 6.61Mb cu o rezoultie de 4752 x 3168 pixeli, calitate 97% intr-o alta imagine in acelasi format JPG dar la o rezolutie mai mica, 640 x 427 px si o calitate/compresie de 90%. Imaginea folosita o gasiti aici: Gemstone Collection.

Pentru cei care vor sa verifice performantele scriptul folosit de mine este mai jos:

<?php 
  PHP_OS == "Windows" || PHP_OS == "WINNT" ? define("DIRSEPARATOR", "\\\\") : define("DIRSEPARATOR", "/"); 

  function get_FileFullPath($img) {
    $folder_img = dirname(__FILE__);
    return str_replace("\\", DIRSEPARATOR,  $folder_img).DIRSEPARATOR.$img;
  }

  $fsource = "benchmark-img.jpg";
  $fdest_imagick = "test-imagick.jpg";
  $fdest_gd = "test-gd.jpg";

  $file_source = get_FileFullPath($fsource);
  $file_dest_imagick = get_FileFullPath($fdest_imagick);
  $file_dest_gd = get_FileFullPath($fdest_gd);

  $w = 640;
  $h = 640;

  // ~~~~~~~~~~~~~~~~~ ImageMagick ~~~~~~~~~~~~~~~~~~~~~

  $start_time = microtime(true);

    $im = new imagick( $file_source );
    $im->setImageCompression(Imagick::COMPRESSION_JPEG);
    $im->setImageCompressionQuality(90);
    $im->stripImage(); 
    $im->thumbnailImage($w, 0);
    //$im->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
    $im->writeImage( $file_dest_imagick );

  $end_time = microtime(true);

  echo ($end_time-$start_time) . " sec";
  echo "<br />";
  echo "<img src='$fdest_imagick'>";

  echo "<hr />";

  // ~~~~~~~~~~~~~~~~~~ GD php library ~~~~~~~~~~~~~~~~~~~~

  $start_time = microtime(true);

    
    list($width, $height) = getimagesize( $file_source );
    $r = $width / $height;
    
    if ($w/$h > $r) {
      $newwidth = $h*$r;
      $newheight = $h;
    } else {
      $newheight = $w/$r;
      $newwidth = $w;
    }		
    
    $src = imagecreatefromjpeg($file_source);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    imagejpeg($dst, $file_dest_gd, 90 );

  $end_time = microtime(true);

  echo ($end_time-$start_time) . " sec";
  echo "<br />";
  echo "<img src='$fdest_gd'>";
?>

Resource:

byrev Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *