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.
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.
To reduce GIF images in size, you can try converting them to PNG or to reduce the color depth. The color depth is the number of colors used in the image. Less colors means a smaller image size.
So you can use GD http://ua.php.net/manual/en/function.imagecopyresampled.php <
?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>
The following works well for JPEG images and likely works for other image formats. The given density (300) is pretty high and suitable for keeping text readable, so adjust for your situation.
convert input-file -density 300 -quality 50% output-file
精彩评论