Resize animated GIF file without destroying animation

I need to resize an animated GIF file without destroying the animation.

How can I do it using PHP?


Solution 1:

if you have imagemagick access, you can do this:

system("convert big.gif -coalesce coalesce.gif");
system("convert -size 200x100 coalesce.gif -resize 200x10 small.gif");

this is most likely possible with the imagemagick plugin if you don't have system() access

NOTE: this may create a larger filesize though a smaller dimensions image due to coalescing essentially deoptimizing the image.

UPDATE: If you don't have ImageMagick access, you should be able to use a combination of the following steps to resize an animated gif (assuming you have GD access):

  1. Detect if the image is an animated gif: Can I detect animated gifs using php and gd? (top answer)
  2. Split the animated gif into individual frames: http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html
  3. Resize the individual frames: http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
  4. Recomposite the frames into an animated gif again: http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

This is definitely much more intensive than the ImageMagick route, but it should be technically possible.

If you get it working, please share with the world!

Solution 2:

Try GDEnhancer (Use ImageCraft). It only need GD Library, and it keep gif animation

Solution 3:

You would need to decompose the gif into frames, thumbnail and re-assemble.

Have a look at ImageMagick and this tutorial.

Solution 4:

I have tried numerous examples of resizing animated GIFs with Imagick PHP module, but none of them worked for me. Then after some debugging time at last I found the actual issue: the animation was lost upon saving image to disk, by $animation->writeImage($file_dst); or $animation->writeImages($file_dst, true);

I've changed it to file_put_contents($file_dst, $animation->getImagesBlob()); and most examples started working immediately.

Hope it helps someone.