Imagemagick 在 php 中不工作,但在终端中工作
Imagemagick not working in php but working in terminal
我想把图片转成webp格式。
这是我的 php 代码:
$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();
但是这段代码给出了以下错误:
Unable to set image format
当以下代码在终端中正常工作时会发生这种情况:
magick convert 1.jpg 2.webp
和
convert 1.jpg 2.webp
OS版本:
CentOS Linux release 7.9.2009 (Core)
PHP版本:
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)
CWEBP 版本:
0.3.0
Imagick PHP 扩展信息:
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
看起来您的安装不支持 webp 格式。请检查您的 imagick-php-extension 是否支持 webp。您可以在 phpinfo() 转储的 imagick 部分或从命令行找到支持的格式:php -r "print_r(Imagick::queryFormats());"
如果未列出 webp,您可以参考此 post:Add WebP to ImageMagick supported formats for PHP 7.3 on a WHM dedicated server
我想把图片转成webp格式。
这是我的 php 代码:
$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();
但是这段代码给出了以下错误:
Unable to set image format
当以下代码在终端中正常工作时会发生这种情况:
magick convert 1.jpg 2.webp
和
convert 1.jpg 2.webp
OS版本:
CentOS Linux release 7.9.2009 (Core)
PHP版本:
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)
CWEBP 版本:
0.3.0
Imagick PHP 扩展信息:
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
看起来您的安装不支持 webp 格式。请检查您的 imagick-php-extension 是否支持 webp。您可以在 phpinfo() 转储的 imagick 部分或从命令行找到支持的格式:php -r "print_r(Imagick::queryFormats());"
如果未列出 webp,您可以参考此 post:Add WebP to ImageMagick supported formats for PHP 7.3 on a WHM dedicated server