dsyrdwdbo47282676 2016-04-03 12:26
浏览 77
已采纳

想象力 - 逐步降低压缩质量

I'm trying to calculate the best compression quality for an image to be less than 150kB. But Imagick makes me sick...

Here's my code :

<?php
// {...}
// $white is my image
// self::THUMBNAIL_SIZE_KO is 150

$quality = 100;
$white->setImageFormat('jpg');
$white->setImageCompression(Imagick::COMPRESSION_JPEG);
$white->setCompressionQuality($quality);
$data = $white->getImageBlob();
var_dump(strlen($data));
while(strlen($data) > self::THUMBNAIL_SIZE_KO * 1024 && $quality > 0){
    $quality--;
    $white->setCompressionQuality($quality);
    $data = $white->getImageBlob();
    var_dump($quality);
    var_dump(strlen($data));
}

$this->_canvas = $white;

I made var_dumps to control the size in bytes of the rendered image. But it's alaways the same size :

// var_dump rendered :

int 167963
int 99
int 167963
int 98
int 167963
int 97
int 167963
int 96
int 167963
int 95
int 167963
int 94
int 167963
int 93
int 167963
// etc.

Do you know why Imagick has it strange behaviour, or if there is something wrong with my code ?

Thanks ! :)

  • 写回答

1条回答 默认 最新

  • doutangguali32556 2016-04-03 12:30
    关注

    It appears your image is an existing image, therefore you need to use setImageCompressionQuality not setCompressionQuality, because the latter only works for new images created with Imagick::newPseudoImage.

    $white->setImageCompressionQuality($quality);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗