Resize animated GIF file without destroying animation
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):
- Detect if the image is an animated gif: Can I detect animated gifs using php and gd? (top answer)
- Split the animated gif into individual frames: http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html
- Resize the individual frames: http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
- 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.
Related videos on Youtube
![riad](https://i.stack.imgur.com/q2za0.jpg?s=256&g=1)
riad
Updated on August 24, 2020Comments
-
riad almost 4 years
I need to resize an animated GIF file without destroying the animation.
How can I do it using PHP?
-
riad about 15 yearssorry, I don't have access on imagemagic. Any other solution?
-
Siva - Sambasiva Rao Dodigam about 15 yearsPosted an update with a number of steps using GD and third party classes to make this happen (in theory).
-
Tommi Forsström over 13 yearsI think I got the theory just about working in practice: forssto.com/gifexample
-
transilvlad over 11 yearsimagick is the best at complex image manipulation. PHP does know a few tricks to.
-
Rolf over 10 yearsThanks. 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 over 10 yearsAmazing!! 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 over 10 yearsThat'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 over 10 yearsHello. 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 about 10 yearsThank you so much @TommiForsström!
-
Admin over 9 yearsthis did the trick when writeImages() would strip the animation.
-
FONGOH MARTIN over 7 years@JeremyStanley's approach is very correct. You can see a full tutorial here
-
TechNyquist over 7 yearsAlas, GDEnhancer website is dead.
-
BredeBS over 7 years@TechNyquist now GDEnhacer is deprecated, use github.com/coldume/imagecraft instead