Tutorial :How to reduce file size using PHP and JPEGs


I am using a the standard PHP functions imagecopytruecolor and imagejpeg to rescale and produce uploaded images from a standard HTML form.

The images appear at the correct size however the image filesize is quite high (e.g. 540px * 350px = 250kb)

When compared to Photoshop's Save for Web using JPEG high quality settings the same files are come out at about 60kb, so about 4 times as small.

Is there anything I can do to reduce the filesize?


No image library I've ever seen comes anywhere close to how efficiently Photoshop compresses a file. You could try using another library like ImageMagick (aka Imagick) which might saves smaller files at the same quality size than the default stuff your using, but keep in mind you'll never get a 60K image from these libraries at this res.


You can set $qualityin imageJPEG. From the manual:

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).

In my experience, it is not advisable to go much under 70%, though.

You may want to try whether you can get better results with smaller file sizes from other image processing engines like ImageMagick, if you can use that. I often have the feeling that GD's JPG encoder is not top of the line, but that's nothing more than a subjective impression at the moment.


You can change the quality setting (3rd argument of imagejpeg, see the docs). The default is only 75% though, which should already be rather small.


From the docs

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

experiment with $quality

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »