douzen1880 2016-07-10 21:20
浏览 185
已采纳

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

I'm trying to set up a simple PHP image server to allow me to add just large file for each of my images and then scale and crop them as needed. For my test file I start with a png8 exported via "save for web" from illustrator of size 2400 x 1200, which has a filesize of 21.6KB.

When I use Imagick's cropThumbnailImage function to reduce it to 600 x 600 the resulting file is 62.1KB (three times the size for a substantially smaller image). A 600 x 600 crop of the same image saved from illustrator clocks in at about 8.2KB. I can accept a modest file size increase for the added convenience, but an ~8x increase is just too much.

When saving the file I make sure to force the output to png8 so it doesn't default to a lossless png format, but other than that I'm clueless as to how to resolve it.

Here is my processing code:

//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 );

Here are my test files:

Original Full scale image outputted by illustrator.

Cropped 600 x 600 image generated by imagick.

[EDIT: As per Mark's suggestion below I added the following changes]

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

The end result goes from 62.1KB to 50.4KB, better but still over double the size of the fullsized image, and many times larger that the illustrator save for web version at that size.

600x600 image reduced to 20 colors and resized not thumbnailed

  • 写回答

1条回答 默认 最新

  • dopgl80062 2016-07-11 08:11
    关注

    Your original image has 33 colours and weighs in at 22kB.

    If you resize like this (albeit at the command line):

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

    the output file will be 6.6kB.

    If you resize like I suggested with -scale:

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

    the output file will be 5.0kB.

    If you retain -quality 9 in there, you will end up with > 25kB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能