Imagemagick Specific webp calls in PHP

我与影子孤独终老i 提交于 2019-12-04 19:25:21

How do I set the "method" (from 0 to 6)?

Try this...

$im = new Imagick();
$im->pingImage($src);
$im->readImage($src);
$im->resizeImage($width,$height,Imagick::FILTER_CATROM , 1,TRUE ); 
$im->setImageFormat( "webp" );
$im->setOption('webp:method', '6'); 
$im->writeImage($dest); 

How do I set compression quality? (I tried setImageCompressionQuality and it does not work, ie the output is always the same size)

Imagick::setImageCompressionQuality seems to work for me, but note that webp:lossless becomes enabled if the values is 100, or greater (see source). You can test toggling lossless to see how that impacts results.

$img->setImageFormat('webp');
$img->setImageCompressionQuality(50);
$img->setOption('webp:lossless', 'true');

Edit from comments

Try testing the image conversion to webp directly with the cwebp utility.

cwebp -q 50 photo.jpg -o photo.webp

This will also write some statistical information to stdout, which can help debug what's happening.

Saving file 'photo.webp'
File:      photo.jpg
Dimension: 1170 x 1170
Output:    4562 bytes Y-U-V-All-PSNR 55.70 99.00 99.00   57.47 dB
block count:  intra4: 91
              intra16: 5385  (-> 98.34%)
              skipped block: 5357 (97.83%)
bytes used:  header:             86  (1.9%)
             mode-partition:   2628  (57.6%)
 Residuals bytes  |segment 1|segment 2|segment 3|segment 4|  total
    macroblocks:  |       0%|       0%|       0%|      98%|    5476
      quantizer:  |      45 |      45 |      43 |      33 |
   filter level:  |      14 |      63 |       8 |       5 |

Also remember that for some subject matters, adjusting the compression quality doesn't always guaranty a file size decrease. But those are extreme edge cases.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!