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

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试设置一个简单的PHP图像服务器,以便为每个我添加一个大文件 图像,然后根据需要缩放和裁剪它们。 对于我的测试文件,我首先从插件大小2400 x 1200的“save for web”导出png8,其文件大小为21.6KB。

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

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

这是我的处理代码:

  //创建工作图像
 $ image = new Imagick($ this->  orig_file); 
 
 //设置压缩
 $ image-> setImageCompressionQuality(9); 
 
 //将图像缩放到高度
 $ image-> cropThumbnailImage($ this-> w,$  this-> h); 
 
 //剥离额外数据
 $ image-> stripImage(); 
 
 //保存文件
 $ image-> writeImage('png8:'。$  this-> output_file); 
   
 
 

以下是我的测试文件:

插图画家输出的原始全尺寸图像。

由imagick生成的裁剪600 x 600图像。

[编辑:按照马克的说法 建议在下面我添加了以下更改]

  // replaci 使用:
 $ image-> resizeImage(0,$ this-> h,imagick :: FILTER_TRIANGLE,1); \ crop 
 $ start =($ image-&gt  )/ 2) - ($ this-> w / 2); 
 $ image-> cropimage($ this-> w,$ this-> h,$ start,0); 
 
 /  /将颜色减少到20 
 $ image-> quantizeImage($ this-> q,1,0,true,false);  //使用20作为$ this-> q 
   
 
 

最终结果从62.1KB变为50.4KB,更好但仍然超过完整图像大小的两倍 ,以及插图画家为该版本的网页版本保存的次数要大很多倍。

600x600图像缩小为20种颜色并调整大小而不是缩略图

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题