Imagemagick Specific webp calls in PHP

10,526

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.

Share:
10,526

Related videos on Youtube

Blaise
Author by

Blaise

Updated on September 15, 2022

Comments

  • Blaise
    Blaise over 1 year

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

  • Blaise
    Blaise almost 8 years
    Thank you so much for the detailed answer. I never saw a documentation for the "SetOption" in the context of webP anywhere so this is the first. It still does not work but now I think it's due to my compilation of Imagemagick...
  • Phantom Lord
    Phantom Lord about 4 years
    @emcconville, could you specify your OS and versions of php, imagemagick, webp? I tried using webp with php and imagick on different distribs with different versions, and each has its own bugs, but neither works as expected.