Nilesh Nilesh - 1 year ago 89
PHP Question

How to resize image size

How to reduce image sizes in Linux. I have used ImageMagick for this. But it is not working well for gif images. I have used it as:

To resize image

exec("convert $original -resize " . $width . 'x' . $height . ' ' . $destination);

To reduce image size

exec("convert $original -quality 80% $new_img");

Please let me know if you have any way to reduce image size. This code works well for jpg, but not works well for gif images.

Appreciate your help.

fvu fvu
Answer Source

Quoting from the Imagemagick manual:

-quality value 
JPEG/MIFF/PNG compression level.

That means that using the quality level will only work on the aforementioned image types, which do not include GIF.

For GIF's your easiest option is to use the -colors command to reduce the number of colors used in your image. The quality of the result depends very much on what your initial image contains, I have seen cases where a reduction from 256 to 16 colors did not cause significant quality loss, and others where a reduction to 128 colors rendered the image unusable. You'll have to experiment.

A last remark: you could transform your GIF to PNG format and use the -quality command on the resulting image. Quality on PNG's however is a bit of a misnomer:

For the MNG and PNG image formats, the quality value sets the zlib compression level (quality / 10) and filter-type (quality % 10). The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering, unless the image has a color map, in which case it means compression level 7 with no PNG filtering.

So don't be surprised if the size reduction is less on PNG's than on JPG', it only improves compression of the lossless PNG image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download