4

Is it possible to convert an animated GIF to WEBP using Imagick?

$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" );

When I do it this way, I get only first frame from the GIF and not the whole animation. I know that WEBP format supports animation, however I was unable to find if Imagick has this ability too.

Get Schwifty
  • 143
  • 2
  • 10

1 Answers1

0

I had the same problem. I can`t figure out how to use Imagick for this conversion.

My workaround is to use only for the gif google`s gif2webp.

The following code example worked for me:

$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');
}
TOminerTV
  • 23
  • 1
  • 5