使用Imagick在PHP中将动态gif转换为webp

4

使用Imagick能否将动态GIF转换为WEBP格式?

$im = new \Imagick();
$im->pingImage( _INTERNAL_FOLDER_ . $dir . "/" . $id . "/animation.gif" );
$im->readImage( _INTERNAL_FOLDER_ . $dir . "/" . $id . "/animation.gif" );
$im->setImageFormat( "webp" );
$im->setImageCompressionQuality( 80 );
$im->setOption( 'webp:lossless', 'false' );
$im->writeImage( _INTERNAL_FOLDER_ . $dir . "/" . $id . "/animation.webp" );

当我这样做时,只会得到GIF的第一帧而不是整个动画。我知道WEBP格式支持动画,但我无法确定Imagick是否也具备这种能力。

1个回答

0

我曾经遇到过同样的问题。我无法弄清楚如何使用Imagick进行转换。

我的解决方法是仅使用谷歌的gif2webp来处理gif文件。

以下代码示例对我有效:

$finfo = new finfo(FILEINFO_MIME_TYPE);
$fileType = $finfo->file($_FILES['profileImg']['tmp_name']);
if($fileType == 'image/gif')
{
    if(move_uploaded_file($_FILES['profileImg']['tmp_name'], 'image.webp'))
    {
        exec('gif2webp image.webp -o image.webp');
    }
    else
    {
        die('Cannot move the file');
    }
}
else
{
    $profileImg = new Imagick($_FILES['profileImg']['tmp_name']);

    $profileImg->setImageFormat('webp');
    $profileImg->setImageCompressionQuality(100);
    $profileImg->setOption('webp:lossless', 'true');

    $profileImg->writeImage('image.webp');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接