开发者

How to reduce file size using PHP and JPEGs

开发者 https://www.devze.com 2022-12-19 17:46 出处:网络
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开发者

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?


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.


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 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

0

精彩评论

暂无评论...
验证码 换一张
取 消