Efficient JPEG Image Resizing in PHP

Question :

Efficient JPEG Image Resizing in PHP,

Answer :

What’s the most efficient way to resize large images in PHP?

I’m currently using the  function imagecopyresampled to take high resolution images, and cleanly resize them down to a size for web viewing (roughly 700 pixels wide by 700 pixels tall).

This works great on small (under 2 MB) photos and the entire resize operation takes less than a second on the server. However, the site will eventually service photographers who may be uploading images up to 10 MB in size (or images up to 5000×4000 pixels in size).

Doing this kind of resize operation with large images tends to increase the memory usage by a very large margin (larger images can spike the memory usage for the script past 80 MB). Is there any way to make this resize operation more efficient? Should I be using an alternate image library such as

Read More  Ant build scripts, antcall, dependencies, etc

Right now, the resize code looks something like this

function makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality) {      // Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it      // and places it at endfile (path/to/thumb.jpg).        // Load image and get image size.      $img = imagecreatefromjpeg($sourcefile);      $width = imagesx( $img );      $height = imagesy( $img );        if ($width > $height) {          $newwidth = $thumbwidth;          $divisor = $width / $thumbwidth;          $newheight = floor( $height / $divisor);      } else {          $newheight = $thumbheight;          $divisor = $height / $thumbheight;          $newwidth = floor( $width / $divisor );      }        // Create a new temporary image.      $tmpimg = imagecreatetruecolor( $newwidth, $newheight );        // Copy and resize old image into new image.      imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );        // Save thumbnail into a file.      imagejpeg( $tmpimg, $endfile, $quality);        // release the memory      imagedestroy($tmpimg);      imagedestroy($img)


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, one
    for ImageMagick.
  3. Run both of them a few times.
  4. Compare results (total execution
    time, CPU and I/O usage, result
    image quality).
Read More  Cannot add WebViewer of ActiveReports to an ASP.NET page

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

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

That’s the answer Efficient JPEG Image Resizing in PHP, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  JQuery.Validate failure in Opera