With the intention to optimise a picture in PHP, it is advisable have a picture saved to a recognized location. You possibly can then name the next little bit of php code to load the picture into PHP’s reminiscence, and the output it to a vacation spot with a given high quality. The operate that does all of the work is imagejpeg(). This operate takes the vacation spot of the output picture and a top quality specification (1-100) along with passing it the picture object you created.

$img = imagecreatefromjpeg($filename);
header("Content-Type: image/jpeg");
imagejpeg($img, $filename, 100);


One factor to remember is that you possibly can by no means optimize a picture to greater high quality than the standard of the unique picture. So, if a picture was saved in PhotoShop as a low high quality picture, and then you definitely attempt to optimize it in PHP, you’ll by no means be capable to improve the picture previous the standard of the unique low high quality picture. To avoid wasting complications all the time begin with the very best high quality picture doable.

Fairly easy proper? Be aware that this script doesn’t show a picture on the display screen. As an alternative it outputs the picture to a file, proven above because the $filename variable. As a result of we created the picture and output the picture utilizing the identical $filename variable, the picture will overwrite itself. In case you needed to output the picture elsewhere, you possibly can change the vacation spot variable to one thing else. For instance: imagejpeg($img, “optimized_images/my_new_image.jpg”, 75); In case you needed to show a picture on the display screen you possibly can give the vacation spot operator as an empty string, and this could ship the picture to the browser.

So what does this numeric high quality worth imply? There isn’t a actual science to choosing the very best quality quantity. The upper the quantity, the higher the standard, however the bigger the file measurement. Typically, the 100 worth will even have a bigger filesize than the unique picture. Beneath is a check case the place i experimented with the standard of an optimized picture.


Taking each high quality and measurement into consideration, I might select 70 high quality for strange photos needing to be optimised, and I might select 80 high quality for circumstances the place the picture goes to scrutinised nearer (pictures web sites, e-commerce). Photos with high quality greater than 80 have a drastically elevated file measurement (see graph under) with out a lot elevated high quality.

Extending This Instance

So now that you’ve decided the best picture high quality, what do you do subsequent? As talked about within the opening paragraph, you possibly can additionally resize the picture on the identical time you optimize it. Footage from at present’s digital cameras are far too giant for webpages and have to be resized considerably earlier than show. PHP’s imagecopyresampled() and imagecopyresized() features can do that for you.

Have to resize and optimize a number of photos without delay? If in case you have PHP 5+ you should use the scandir() operate to get an array of all of the information in a selected folder. You possibly can then loop via them one after the other and resize all of them. In case you’re nonetheless utilizing PHP four, then the readdir() operate will allow you to loop via all of the information in a listing.