Efficient JPEG Image Resizing in PHP Efficient JPEG Image Resizing in PHP php php

Efficient JPEG Image Resizing in PHP


People say that ImageMagick is much faster. At best just compare both libraries and measure that.

  1. Prepare 1000 typical images.
  2. Write two scripts -- one for GD, onefor ImageMagick.
  3. Run both of them a few times.
  4. Compare results (total executiontime, CPU and I/O usage, resultimage quality).

Something which the best everyone else, could not be the best for you.

Also, in my opinion, ImageMagick has much better API interface.


Here's a snippet from the php.net docs that I've used in a project and works fine:

<?function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {    // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.    // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".    // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.    // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.    //    // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.    // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.    // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.    // 2 = Up to 95 times faster.  Images appear a little sharp, some prefer this over a quality of 3.    // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled, just faster.    // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.    // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.    if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }    if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {        $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);        imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);        imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);        imagedestroy ($temp);    } else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);    return true;}?>

http://us.php.net/manual/en/function.imagecopyresampled.php#77679


phpThumb uses ImageMagick whenever possible for speed (falling back to GD if necessary) and seems to cache pretty well to reduce the load on the server. It's pretty lightweight to try out (to resize an image, just call phpThumb.php with a GET query that includes the graphic filename and output dimensions), so you might give it a shot to see if it meets your needs.