Compress jpeg on server with PHP Compress jpeg on server with PHP php php

Compress jpeg on server with PHP


I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:

$img = new Imagick();$img->readImage($src);$img->setImageCompression(Imagick::COMPRESSION_JPEG);$img->setImageCompressionQuality(90);$img->stripImage();$img->writeImage($dest); $img->clean();


you're not telling if you're using GD, so i assume this.

$img = imagecreatefromjpeg("myimage.jpg");   // load the image-to-be-saved// 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)imagejpeg($img,"myimage_new.jpg",50);unlink("myimage.jpg");   // remove the old image


You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.

For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html