Resize animated GIF file without destroying animation

81,210

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.

Solution 5

The example on http://www.php.net/manual/en/imagick.coalesceimages.php will resize your gif while retaining your frame timing. Something most of the other examples do not do.

Other examples rebuild the gif while this one allows you to modify the frames of the image.

Share:
81,210

Related videos on Youtube

riad
Author by

riad

Updated on August 24, 2020

Comments

  • riad
    riad almost 4 years

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

    How can I do it using PHP?

  • riad
    riad about 15 years
    sorry, I don't have access on imagemagic. Any other solution?
  • Siva - Sambasiva Rao Dodigam
    Siva - Sambasiva Rao Dodigam about 15 years
    Posted an update with a number of steps using GD and third party classes to make this happen (in theory).
  • Tommi Forsström
    Tommi Forsström over 13 years
    I think I got the theory just about working in practice: forssto.com/gifexample
  • transilvlad
    transilvlad over 11 years
    imagick is the best at complex image manipulation. PHP does know a few tricks to.
  • Rolf
    Rolf over 10 years
    Thanks. Can it be used with PHP 5.2? On the site it says 5.4+, which is pretty cutting edge for most environments (including my target environment in this case). Otherwise looks great!
  • FidoBoy
    FidoBoy over 10 years
    Amazing!! This saved my day! Thanks Anthony. Old versions of Imagick are buggy when using writeImages but using your method works fine and no problems at all! :)
  • Rolf
    Rolf over 10 years
    That's the solution I went for finally, I tried other solutions, but they were prohibitively slow. Make sure you have Imagick on your hosting (it's not such a rare thing nowadays) and use that.
  • Rolf
    Rolf over 10 years
    Hello. Thanks. I tried your solution. I modified the sources to read the image from a string (let me know if you're interested) but it turned out to be WAAAY to slow. I finally realized that Imagick was available on the hosting and used that instead.
  • hultberg
    hultberg about 10 years
    Thank you so much @TommiForsström!
  • Admin
    Admin over 9 years
    this did the trick when writeImages() would strip the animation.
  • FONGOH MARTIN
    FONGOH MARTIN over 7 years
    @JeremyStanley's approach is very correct. You can see a full tutorial here
  • TechNyquist
    TechNyquist over 7 years
    Alas, GDEnhancer website is dead.
  • BredeBS
    BredeBS over 7 years
    @TechNyquist now GDEnhacer is deprecated, use github.com/coldume/imagecraft instead