Resize Images - ImageMagick vs GD

From PhotoblogWiki

Jump to: navigation, search

PHP now has the ImageMagick PECL library to do image manipulation. I recently wrote some code and did a benchmark of resizing around 40 6 mega pixel images that were around 3300px wide

Below are the results

 the time for imagemagick: 41.452951192856
 the time for gd: 47.792565822601 

Also from what I know the Imagick::FILTER_LANCZOS filter is one of the slowest in ImageMagick

Below is the code I used

function getmtime() {
    $a = explode (' ',microtime());
    return(double) $a[0] + $a[1];
}
 
$start = getmtime();
foreach (glob("./pics/*.jpg")  as $file) {
	/**
	 * loading the image
	 */
	$image = new Imagick($file);
 
	/**
	 * getting the width and height
	 */
	$width = $image->getImageWidth();
	$height = $image->getImageHeight();
 
	/**
	 * getting the resized height based off a set width
	 */
	$resize_width = 500;
	$resize_height = floor(($resize_width / $width) * $height);
 
	/**
	 * resizing it now
	 */
	$image->resizeImage($resize_width,$resize_height,Imagick::FILTER_LANCZOS,1);
 
	/**
	 * saving the image
	 */
	$image->writeImage("./resized/" . basename($image->getImageFilename()));
 
	/**
	 * deleting the resource
	 */
	$image->destroy();
}
 
$end = getmtime();
 
 
$time = $end - $start;
echo "the time for imagemagick: $time <br/ >";
 
 
 
$start = getmtime();
foreach (glob("./pics/*.jpg")  as $file) {
	/**
	 * loading the image
	 */
	$image = imagecreatefromjpeg($file);
 
	/**
	 * getting the width and height
	 */
	$width = imagesx($image);
	$height = imagesy($image);
 
	/**
	 * getting the resized height based off a set width
	 */
	$resize_width = 500;
	$resize_height = floor(($resize_width / $width) * $height);
 
	/**
	 * resizing it now
	 */
	$image_new = imagecreatetruecolor($resize_width, $resize_height);
	imagecopyresampled($image_new, $image, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height);
 
	/**
	 * saving the image
	 */
	imagejpeg($image_new, "./resized/pic.jpg", 95);
 
	/**
	 * deleting the resource
	 */
	imagedestroy($image_new);
	imagedestroy($image);
}
$end = getmtime();
 
 
$time = $end - $start;
echo "the time for gd: $time <br/ >";
Personal tools