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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题