PHP Imagick GIF Animation Is Not Preserved

2019-08-07 04:24发布

问题:

I am having a bit up trouble upload gifs and keeping their animation. My code looks like this:

$image = new Imagick($data['file']);
$object -> uploadFile('test.gif', $image -> getImageBlob());

And the gif goes not seem to be preserved when the getImageBlob() is used. Ive event tried:

$image = new Imagick($data['file']);
$image = $image->coalesceImages(); 
$image = $image->deconstructImages(); 
$object -> uploadFile('test.gif', $image -> getImageBlob());

Is there way I can preseve the gif when using getImageBlob()?

回答1:

Try Imagick::getImagesBlob instead of Imagick::getImageBlob. getImagesBlob will return the sequence of loaded images, as opposed to just a single image; in this case, the each frame in the GIF.

A quick check with PHP 5.5.7 and PECL imagick 3.1.2 reveals two different behaviors:

$image = new Imagick('test.gif');
var_dump(strlen($image->getImagesBlob())); // int(4316519)
var_dump(strlen($image->getImageBlob()));  // int(61413)