centos Imagemagick无法在php中工作,但可以在终端中工作

rryofs0p  于 2022-11-07  发布在  PHP
关注(0)|答案(1)|浏览(204)

我想把图片转换成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();

但此代码会产生以下错误:
无法设置图像格式
当以下代码在终端中正常工作时,会发生这种情况:

magick convert 1.jpg 2.webp

convert 1.jpg 2.webp

操作系统版本:

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
zrfyljdw

zrfyljdw1#

看起来您的安装不支持webp格式。请检查您的imagick-php-extension是否支持webp格式。您可以在phpinfo()转储文件的imagick部分或命令行中找到支持的格式:php -r "print_r(Imagick::queryFormats());"
如果webp未列出,您可以参考此帖子:Add WebP to ImageMagick supported formats for PHP 7.3 on a WHM dedicated server

相关问题