I want to convert images to webp format.
This is my php code :
$im = new Imagick();
$image = file_get_contents($url);
try {
$im->readImageBlob($image);
$im->setImageFormat( "webp" );
$im->setOption('webp:method', '6');
} catch (ImagickException $e) {
echo $e->getMessage();
}
$im->destroy();
But this code gives the following error:
Unable to set image format
This happens while the following code is working properly in the terminal:
magick convert 1.jpg 2.webp
and
convert 1.jpg 2.webp
CentOS Linux release 7.9.2009 (Core)
PHP 7.4.29 (cli) (built: Apr 18 2022 22:43:47) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with the ionCube PHP Loader + ionCube24 v11.0.0, Copyright (c) 2002-2021, by ionCube Ltd.
with Zend OPcache v7.4.29, Copyright (c), by Zend Technologies
Version: ImageMagick 7.1.0-30 Q16-HDRI x86_64 19924 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(3.1)
Delegates (built-in): bzlib djvu fontconfig freetype jng jpeg lzma openexr pangocairo png tiff x xml zlib
Compiler: gcc (4.8)
Version: ImageMagick 7.1.0-30 Q16-HDRI x86_64 19924 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(3.1)
Delegates (built-in): bzlib djvu fontconfig freetype jng jpeg lzma openexr pangocairo png tiff x xml zlib
Compiler: gcc (4.8)
0.3.0
imagick
imagick module => enabled
imagick module version => 3.7.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
imagick.allow_zero_dimension_images => 0 => 0
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.set_single_thread => 1 => 1
imagick.shutdown_sleep_count => 10 => 10
imagick.skip_version_check => 0 => 0
Looks like your installation does not support webp format. Please check if webp is supported by your imagick-php-extension. You can find the supported formats in the imagick section of a phpinfo() dump or from command line: php -r "print_r(Imagick::queryFormats());"
If webp is not listed you can consult this post: Add WebP to ImageMagick supported formats for PHP 7.3 on a WHM dedicated server