Imagemagick特定的webp调用PHP [英] Imagemagick Specific webp calls in PHP
问题描述
我能够为imagemagick安装webp支持。但我错过了一些精确的命令。
基本涵盖通过:
$ im = new Imagick();
$ im-> pingImage($ src);
$ im-> readImage($ src);
$ im-> resizeImage($ width,$ height,Imagick :: FILTER_CATROM,1,TRUE);
$ im-> setImageFormat(webp);
$ im-> writeImage($ dest);
但是我缺少很多微调选项,如imageMagick命令行文档中所述:
http://www.imagemagick.org/script/webp.php
具体来说:
如何设置压缩质量? (我尝试了setImageCompressionQuality并且它不起作用,即输出总是相同的大小)
如何设置方法(从0到6)?
谢谢
编辑:我按照下面的@ emcconville的建议(谢谢!),方法和压缩都没有工作。所以我开始怀疑我的imagemagick汇编。
我尝试使用命令行:
convert photo.jpg -resize 1170x1170 \> -quality 50 photo.webp
我们更改50变量的质量结果文件始终是相同的大小。所以我的imagemagick肯定有问题......
如何设置方法(从0到6)?
试试这个......
< pre class =lang-php prettyprint-override>
$ 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);
如何设置压缩质量? (我尝试了setImageCompressionQuality并且它不起作用,即输出总是相同的大小)
Imagick :: setImageCompressionQuality
似乎对我有用,但请注意如果值为100或更高, webp:lossless
将变为启用状态(请参阅 source )。你可以测试切换无损,看看它会如何影响结果。
$ img-> setImageFormat( 'WEBP');
$ img-> setImageCompressionQuality(50);
$ img-> setOption('webp:lossless','true');
从评论中编辑
尝试使用 cwebp
实用程序直接测试图像转换为webp。
cwebp -q 50 photo.jpg -o photo.webp
这也会写一些统计信息到stdout,这可以帮助调试正在发生的事情。
保存文件'photo.webp'
文件:照片.jpg
维数:1170 x 1170
输出:4562字节YUV-All-PSNR 55.70 99.00 99.00 57.47 dB
块数:intra4:91
intra16:5385( - > 98.34%)
跳过块:5357(97.83%)
字节使用:标题:86(1.9%)
模式分区:2628(57.6%)
剩余字节数|段1 |段2 |段3 |段4 |总计
宏块:| 0%| 0%| 0%| 98%| 5476
量化器:| 45 | 45 | 43 | 33 |
过滤级别:| 14 | 63 | 8 | 5 |
还要记住,对于某些主题,调整压缩质量并不总是保证文件大小减小。但那些是极端边缘的情况。
I was able to install webp support for imagemagick. But I'm missing some precise commands. The basic is covered thru:
$im = new Imagick();
$im->pingImage($src);
$im->readImage($src);
$im->resizeImage($width,$height,Imagick::FILTER_CATROM , 1,TRUE );
$im->setImageFormat( "webp" );
$im->writeImage($dest);
But I'm missing lots of fine tuning options as described in the imageMagick command line documentation here: http://www.imagemagick.org/script/webp.php
Specifically:
How do I set compression quality? (I tried setImageCompressionQuality and it does not work, ie the output is always the same size)
How do I set the "method" (from 0 to 6)?
Thanks
EDIT: I followed @emcconville's advice below (thanks!) and neither the method nor the compression worked. So I start to suspect my compilation of imagemagick. I tried using command line:
convert photo.jpg -resize 1170x1170\> -quality 50 photo.webp
Wehn changing the 50 variable for quality the resulting file was always the same size. So there must be something wrong with my imagemagick...
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.
这篇关于Imagemagick特定的webp调用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!