Recente berichten
- Add custom fields to a taxonomy
- WordPress plugin: Widgets Master
- WordPress wp_get_category sort order workaround
- How to get SSH access on a LaCie Network Space 2
- Last.FM blocks module
- OAuth PhoneGap plugin
- Howto: Setup your Unify Project development environment
- Howto: Generate animated GIF with PHP
- iPhone: Why not use NSMutableDictionary to communicate with an API?
- Howto: iPhone Application Development Environment
Reacties
- Jorge Hernández on Howto: Generate animated GIF with PHP
- josue on Howto: Generate animated GIF with PHP
- Jeffrey on How to get SSH access on a LaCie Network Space 2
- Remco on OAuth PhoneGap plugin
- J Ras on Howto: Generate animated GIF with PHP
Mobypicture
Howto: Generate animated GIF with PHP

Recently I was asked if it was possible to generate animated GIFs with PHP GD.
Knowing that it’s not possible to do this with PHP GD directly, I still wanted to try if it would be possible with some other PHP solutions.
After searching the web I found the ‘GIFEncoder.class’ by László Zsidi on phpclasses.org
In this blogpost I’ll write a small demo on generating an animated gif with this class. We’ll merge 2 PNG files, and add some text to them before mergin into the animated GIF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | header ('Content-type:image/gif'); include('GIFEncoder.class.php'); $text = "Hello World"; // Open the first source image and add the text. $image = imagecreatefrompng('source01.png'); $text_color = imagecolorallocate($image, 200, 200, 200); imagestring($image, 5, 5, 5, $text, $text_color); // Generate GIF from the $image // We want to put the binary GIF data into an array to be used later, // so we use the output buffer. ob_start(); imagegif($image); $frames[]=ob_get_contents(); $framed[]=40; // Delay in the animation. ob_end_clean(); // And again.. // Open the first source image and add the text. $image = imagecreatefrompng('source02.png'); $text_color = imagecolorallocate($image, 200, 200, 200); imagestring($image, 5, 20, 20, $text, $text_color); // Generate GIF from the $image // We want to put the binary GIF data into an array to be used later, // so we use the output buffer. ob_start(); imagegif($image); $frames[]=ob_get_contents(); $framed[]=40; // Delay in the animation. ob_end_clean(); // Generate the animated gif and output to screen. $gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin'); echo $gif->GetAnimation(); |
To save the animated gif to file: Remove the header(); call on top, and instead of echo $gif->GetAnimation(); add the next lines at the bottom.
This will save an animated gif file named ‘animegif.gif’ in the current folder, _if_ the folder has write permissions.
Download Animated GIF demo
animegif.zipTags: animated gif, php
Reacties
4 reacties op "Howto: Generate animated GIF with PHP"
- | 15 March 2011 - 03:45
- | 1 June 2011 - 15:39
Great article! Great help!
How can I save the output as a file on the server?
- | 15 November 2011 - 00:25
I cant download the source file :[
- | 3 January 2012 - 11:19
Thanks a lot



Wow thank you for writing this tutorial–I spent a long time trying to figure out how to create a GIF from PNG sources using PHP.
Thanks again!