PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

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

 July 18, 2022     animated-gif, gif, imagemagick, php     No comments   

Issue

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?


Solution

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]


Answered By - Johann Bauer
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing