1

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?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • You would have to break the gif into its `x` images and then fiddle with each individual image and then put the new images back into an animated gif and that is not possible using GD – RiggsFolly Jun 21 '15 at 19:13
  • Thank you. If you add this as an answer and explain it a bit I will certainly accept your answer. However, I will wait a bit for other answers. Maybe someone knows something we both are unaware of. – Lajos Arpad Jun 21 '15 at 19:20
  • What was imagemagick implemented in? If php, then it is possible. Surely, we should be able to solve this if we implement the image processing from scratch, but from a management point of view it is better to use a library if that is the case. – Lajos Arpad Jun 21 '15 at 19:21
  • 1
    @LajosArpad Imagemagick, much like imagick and the php interpreter are written in C. – Johann Bauer Jun 21 '15 at 19:36
  • Thanks for the info about imagick and imagemagick. – Lajos Arpad Jun 22 '15 at 17:07

1 Answers1

2

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]
Community
  • 1
  • 1
Johann Bauer
  • 2,488
  • 2
  • 26
  • 41