How to resize gif with standard php functions without losing the animation

2.5k views Asked by At

I have a picture uploading PHP script, which handles png, jpg and jpeg images well. The problem is, that since I am using the combo of imagealphablending, imagesavealpha, imagecreatetruecolor and imagecopyresampled to resize the picture, the gif animation is lost. I am using imagecreatefromgif to load the gif into memory.

I know one can use ImageMagick to achieve the desired result, but I wonder whether there is a solution with a combo of standard php functions to resize the gif without losing the animation. I there such a solution, or should I start to use a library?

1

There are 1 answers

0
Johann Bauer On BEST ANSWER

It is possible to resize an animated GIF using GD (what you call "standard PHP").

You need to split the image into individual frames, resize them and then put everything back together.

You can see a more detailed explanation here.

However, I would really suggest you to use imagick, since it's easy to install and much easier to use for advanced tasks like this.

EDIT This is the relevant part from the answer I linked above:

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: https://stackoverflow.com/questions/280658/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][2]
  3. Resize the individual frames: [http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/][3]
  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][4]