views:

423

answers:

4

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?

+2  A: 

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.

Pekka
I agree, when you go below 70 the compression starts becoming obvious.
TravisO
A: 

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.

Wim
A: 

From the docs

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

experiment with $quality

meouw
A: 

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.

TravisO
I definitely agree on this.I did some test playing with $quality argumnet of the PHP imagejpeg function. The result was sadly that teh maximu quality ($quality = 100) is comparable to a Photoshop quality of 9 (over a scale that goes from 0 tp 12). So Photoshop could save in much better quality if it wants, while imagejpeg CAN NOT!
Marco Demajo
Yes, Photoshop isn't cheating, it's really "that good". Photoshop will have better quality images that are smaller, nothing is going to compare, not even other image apps.
TravisO