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.

1
2
3
$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 Animated GIF demo

animegif.zip

Tags: ,

Reacties

19 reacties op "Howto: Generate animated GIF with PHP"

  • Matt | 15 March 2011 - 03:45

    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!

  • J Ras | 1 June 2011 - 15:39

    Great article! Great help!

    How can I save the output as a file on the server?

  • josue | 15 November 2011 - 00:25

    I cant download the source file :[

  • Jorge Hernández | 3 January 2012 - 11:19

    Thanks a lot

  • Black images become white images GIFEncoder | 19 August 2012 - 14:44
  • kerimov | 7 September 2012 - 22:09

    How to ad font and fontsize function in this script?

  • Jeroen | 7 September 2012 - 22:33

    Looking at Imagestring() function on php.net you can see that the 2nd parameter is used for the font.

  • Lachee | 1 May 2013 - 13:54

    It doesn’t seem to work with png’s that generate through a separate php script. Is there any way around this?

  • Tomm1 | 18 July 2013 - 20:30

    This is an old blog. Hope you are still listening.
     
    I tried to run the example. I got 4 errors from line 39 of the gif.php file.
     
      PHP Notice:  Use of undefined constant ERR01
      PHP Notice:  Use of undefined constant ERR02
      PHP Notice:  Use of undefined constant ERR03
      PHP Notice:  Use of undefined constant ERR04
     
    Was I supposed to install another php item (atp-get …) ?
     
    Also, when I get this going, I will need to make the gif file loop.
    Is that possible.
    Thanks, Tomm

  • williamrlazenby | 11 July 2015 - 19:36

    Tomm1 I just ran across this blog. I will try to duplicate your problem with the files provided in the .zip either tonight or tomorrow and get back with you on the results. I suspect that your PHP script is interpreting something as a constant that isn’t rather than you needing to install anything extra. This can sometimes be caused by the lack of quotes around data.

    What version of PHP are you using?

  • John7575 | 8 December 2015 - 18:25

    Hi :), 
    Thanks for this nice tutorial, 
    Is there a way to force GIF quality?
    Because, when I do a trial there is lost due to GIF compression… Can I force the quality up?
    Thanks, 
    John.

  • navasshareif | 23 March 2016 - 09:45

    Tomm1  this is the solution bro..

    var $ERR = Array (
    ‘ERR00’=>”Does not supported function for only one image!”,
    ‘ERR01’=>”Source is not a GIF image!”,
    ‘ERR02’=>”Unintelligible flag “,
    ‘ERR03’=>”Does not make animation from animated GIF source”,
    );

  • navasshareif | 23 March 2016 - 09:48
  • krish veni | 24 May 2016 - 08:21

    i got the error like Notice: Use of undefined constant ERR00 – assumed ‘ERR00’ in C:xampphtdocsgifimagegif.php on line 39
    Notice: Use of undefined constant ERR01 – assumed ‘ERR01’ in C:xampphtdocsgifimagegif.php on line 39
    Notice: Use of undefined constant ERR02 – assumed ‘ERR02’ in C:xampphtdocsgifimagegif.php on line 39
    Notice: Use of undefined constant ERR03 – assumed ‘ERR03’ in C:xampphtdocsgifimagegif.php on line 39

  • RobWatkins | 29 May 2016 - 15:37

    Yep that works nice job with this.

  • Lacan | 11 June 2016 - 23:46

    header(“Content-Type: image/gif”); not work in mariadb, but using fopen and fwrite work flawlessly.. However put the header in different file using readfile($fullpath); work good. Do u know how to make it readable as header in the same file gif.php?!! any advices will be appreciated.. Thanks before for ur attention..!

    @Lacan

  • Lacan | 12 June 2016 - 00:57

    All Right… After some trials i’ve noticed that the error is in the undefined constant00 etc, so just add this before ther error showing:
    error_reporting(0);
    And it’s work nice… Thank for the tutorial.. But i come to think how to process the reverse to extract the Animated GIF with
    GIFDecoder.class.php after some search i found that clas but not know how to use that Class coz it’s hard to find the tutorial. Hope this web blog could find the solution ! Thanks u for the nice tutorial.

  • Edison | 19 July 2016 - 07:37

    Thanks for the tutorial. How can I make the transitions crossfade?

  • DebasishDas1 | 5 October 2016 - 16:20

    GIFEncoder V2.05: 0 Source is not a GIF image!

Laat een reactie achter