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.

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.

$fp = fopen('animegif.gif', 'w');
fwrite($fp, $gif->GetAnimation());
fclose($fp);

This will save an animated gif file named 'animegif.gif' in the current folder, _if_ the folder has write permissions.

Download the sources below.

Add comment

The content of this field is kept private and will not be shown publicly.

Comments

Great, just what I needed :-)

Much appreciated!

Thank you, i will fix my problem with this code.

I tryed to imagegif command for create gif but fwrite is better.

I've tested it here, and it's really simple.
Just save the output of $gif->GetAnimation() to a file.

$fp = fopen('animegif.gif', 'w');
fwrite($fp, $gif->GetAnimation());
fclose($fp);

Where the folder where you're trying to save the 'animegif.gif' file to must have write permissions.

Hi too many thanks for this post.

But i have a little problem, if you help me i will be happy.

Everting working fine, but how i can auto save output animated gif.

Please help me about this.

Twitter feed

  • 7 hours 56 min ago
    Does anyone know a free opensource streaming server that's able to stream 3GP and MP4 files?
  • 9 hours 54 min ago
    @greg_harvey haha.. need help to port them ? :P
  • 9 hours 57 min ago
    Having a hard time compiling ffmpeg so it can transcode to 3GP :S
  • 12 hours 5 min ago
    Can the #drupal module #video transcode 1 uploaded file into multiple different formats ? By default it transcodes to flv.
  • 14 hours 34 min ago
    Over 18 dagen vertrek ik voor wintersport naar flachau, oostenrijk. Een gevoel van binnen zoals een klein kind vlak voor sinterklaas heeft.

Flickr feed

vuurtje?close upD01_2546DSC_1116DSC_1422DSC_1420DSC_1413DSC_1387DSC_1365

Last.fm recently played

Bloc Party - Like Eating GlassBloc Party - Ion SquareBloc Party - Better Than HeavenBloc Party - TalonsBloc Party - ZephyrusBloc Party - One Month OffBloc Party - SignsBloc Party - Trojan HorseBloc Party - Pioneers (M83 Remix)