Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagemagick Specific webp calls in PHP

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...

like image 993
Blaise Avatar asked Feb 06 '23 20:02

Blaise


1 Answers

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.

like image 75
emcconville Avatar answered Feb 10 '23 09:02

emcconville