使用Imagick的php图像服务器在裁剪/缩小后生成更大的png8文件

时间:2016-07-10 21:20:32

标签: php image imagick

我试图设置一个简单的PHP图像服务器,允许我为每个图像添加大文件,然后根据需要进行缩放和裁剪。对于我的测试文件,我从png8导出,通过"保存为web"来自插图画家大小2400 x 1200,其文件大小为21.6KB。

当我使用Imagick的cropThumbnailImage函数将其缩小到600 x 600时,生成的文件为62.1KB(对于一个小得多的图像,大小是三倍)。从插画家时钟中保存的600 x 600作物相同的图像大约8.2KB。我可以接受适度的文件大小增加以增加便利性,但是增加了大约8倍。

保存文件时,我确保强制输出为png8,因此它不会默认为无损png格式,但除此之外,我对如何解决它无能为力。

这是我的处理代码:

//create working image
$image = new Imagick( $this->orig_file );

// Set Compression
$image->setImageCompressionQuality( 9 );

//scale image to height
$image->cropThumbnailImage ( $this->w, $this->h );

// strip extra data
$image->stripImage();

// save file
$image->writeImage( 'png8:'.$this->output_file );

以下是我的测试文件:

Original Full scale image outputted by illustrator.

Cropped 600 x 600 image generated by imagick.

[编辑:根据马克的建议,我添加了以下更改]

// replacing cropThumbnailImage with:
$image->resizeImage(0, $this->h, imagick::FILTER_TRIANGLE, 1);

// crop
$start = ($image->getImageWidth() / 2) - ($this->w / 2);
$image->cropimage($this->w, $this->h, $start, 0);

// reduce colors to 20
$image->quantizeImage($this->q, 1, 0, true, false); // using 20 as $this->q

最终结果从62.1KB到50.4KB,更好但仍然是完整图像大小的两倍,并且是插图画家为该版本的Web版本保存的大几倍。

600x600 image reduced to 20 colors and resized not thumbnailed

1 个答案:

答案 0 :(得分:1)

您的原始图像有33种颜色,重量为22kB。

如果您像这样调整大小(尽管在命令行中):

convert jabba.png -resize 600x600 -strip png8:result.png

输出文件为6.6kB。

如果您按照我建议的-scale调整大小:

convert jabba.png -scale 600x600 -strip png8:result.png

输出文件为5.0kB。

如果你在那里保留-quality 9,你最终会得到> 25KB。