Compressing JPEG images with JPEG Reducer

One of the most appreciated features of PHP is the ability to create and manipulate graphical images using the GD extension functions. This extension comes with all PHP distributions, so everybody can use it. You can load and save images in several formats and process them in a way that is convenient to your PHP applications.

Sometimes the available disk space is scarce and it is necessary to minimize the size that the image files take on the server disk. Some time ago I replied to a post from an user that had that problem. He wanted to assure that the JPEG images uploaded to his server did not exceed a given size limit per image.

Taking Cisco 350-018 exam in not that much difficult because of availability of test training guides and practice test. CISCO 640-822 certification is the first choice of those professional who’ve already 642-503 certified. The students who want to be IT professional in future prefer to be N10-003 certified. Even if they go for VCP-310 certification, their future will be more secured than others. Since 220-602 exam is related to N10-003, therefore, if you have passed N10-003 then it’s better to be 220-602 certified as well. However, CISCO 640-801 certification is more difficult if you don’t have the proper information and guidance for taking exam.

The PHP GD function imagejpeg takes an optional argument to control the quality factor of the JPEG images generated by the function. The quality factor is a measure of the fidelity of the generated image in comparison with the original image. A lower quality factor leads to the generation of smaller JPEG files.

However, there is no predictable relation between the quality factor and the resulting JPEG image file size. If you want to generate JPEG images with a limited file size, and at the same time you do not want to sacrifice too much the image quality, you have to find an approximated value for the quality factor that leads to an image with a file size near the desired limit.

That is the approach used by the JPEG Reducer class written by Huda Elmatsani from Indonesia. He wrote the class precisely with the intention of solving this optimization problem.

First the class determines the file size that image would take using 3 different quality factor values. Then it uses interpolation to find an approximate quality factor that matches the desired image file size limit.

The class usage is very straightforward. It just takes a JPEG image file name and the desired file size limit. The resulting image may be stored in another server side file or served as the current script output. Here is an example:
require(”class.jpegreducer.php”);

$im = new JPEGReducer(”jakarta.jpg”, 10000);
$im->OutputImage(”jakarta1.jpg”);

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Furl
  • Netvouz
  • Google Bookmarks
  • DZone
  • MisterWong

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image