PHP and animated gifs
Posted: Sun 20 Mar 2011 4:42 am
PHP created animated gifs
Level: Intermediate => Advanced
First of all PHP has absolutely NO functions to create / edit / crunch animated gifs !
"Why would I want to create animated gifs with PHP ?"
Well, if the beteljuices weather station was in service, my banner would look something like:
(5 frames variant) (30 frames variant) Or if you wanted to 'rotate' some other graphic information without resorting to JavaScript / CSS.
The beteljuice is VERY surprised that 'Weather communities' and the like who love 'banners' with current information have not made the web awash with annoying animated gifs.
... so after years in the wilderness, the beteljuice has decided to spread the World Wide Word.
First of all you will need the GIFEncoder.class.php created by Laszlo Zsidi. (There is a version 3 available from sourceforge , which includes additional parameters for image offsets, you don't need them)
If you only want to stitch existing gifs into an animation, you don't even need the GD library installed.
OK, a few hints and observations:
If your intention is for the php file to be the equivalent of the 'picture' (rather than creating and saving to file)
Then you will need the header
header("Content-type: image/gif");
You want the class file ... so
require_once "GIFEncoder.class.php"; // or include "GIFEncoder.class.php";
Creating (but NOT saving or displaying yet) the animation:
$gif = new GIFEncoder ( frames[array], delay[array], lop, dis, red, green, blue, format );
example: $gif = new GIFEncoder ( $frames, $delay, 0, 2, -1, -1, -1, "url" );
So for this reason the beteljuice has found it easier to save each created frame and call from file using the "url" flag.
example:
imagegif($im, "w4.gif"); // save this as a gif to filename w4.gif
imagedestroy($im); // free the work image from memory
if you really want to work the created binary, you need to use the object into buffer method.
example: (some psuedo code)
ob_start();
imagegif($im); // normal output is suppressed
$frame[number] = ob_get_contents(); // $frame[] now has o/p of line above
$delay[number] = value in ms
ob_end_clean(); // flush contents of object buffer
imagedestroy($im);
To display the animation (in conjunction with the header)
echo $gif->GetAnimation();
exit;
NOTE: The animation has not been saved anywhere !
If you wanted to save the animation and NOT display it with this (url).php
remove the header line ... and ....
$data = $gif->GetAnimation();
fwrite(fopen("myanimation.gif", "wb"), $data);
exit;
There are so many ways to loop / branch when creating eg. a banner - so I'm not giving any kind of example code. It's up to you to play and experiment
THE SOURCE IMAGES MUST ALWAYS BE OF THE STATIC GIF TYPE
THE IMAGES SHOULD BE OF THE SAME WIDTH / HEIGHT
THERE MUST BE MORE THAN ONE IMAGE
Remember at the start - "PHP has absolutely NO functions to create / edit / crunch animated gifs"
Memorywise these animations can get quite large, so don't try to create a hundred frames or five frames the size of a house !
(See the file sizes of the attachments)
Spread the animation ...........
Level: Intermediate => Advanced
First of all PHP has absolutely NO functions to create / edit / crunch animated gifs !
"Why would I want to create animated gifs with PHP ?"
Well, if the beteljuices weather station was in service, my banner would look something like:
(5 frames variant) (30 frames variant) Or if you wanted to 'rotate' some other graphic information without resorting to JavaScript / CSS.
The beteljuice is VERY surprised that 'Weather communities' and the like who love 'banners' with current information have not made the web awash with annoying animated gifs.
... so after years in the wilderness, the beteljuice has decided to spread the World Wide Word.
First of all you will need the GIFEncoder.class.php created by Laszlo Zsidi. (There is a version 3 available from sourceforge , which includes additional parameters for image offsets, you don't need them)
If you only want to stitch existing gifs into an animation, you don't even need the GD library installed.
OK, a few hints and observations:
If your intention is for the php file to be the equivalent of the 'picture' (rather than creating and saving to file)
Then you will need the header
header("Content-type: image/gif");
You want the class file ... so
require_once "GIFEncoder.class.php"; // or include "GIFEncoder.class.php";
Creating (but NOT saving or displaying yet) the animation:
$gif = new GIFEncoder ( frames[array], delay[array], lop, dis, red, green, blue, format );
example: $gif = new GIFEncoder ( $frames, $delay, 0, 2, -1, -1, -1, "url" );
- $frames: an array containing the source url or source binary of each frame (picture) to be stitched
$delay: an array containing duration of each individual frame (ms)
lop: a flag which tell the animation how many times to loop, best set to 0 (infinite)
dis: I can't remember what this is short for, the author has never explained it's purpose and no one has found an obvious use for it - set to 2
red, green, blue: transparency values (0 - 255), don't even think about it - very rarely works because of the way php may marginally change the palettes of the individual frames, leave values set to -1 (no transparency)
format: "url" a (static) gif file location - or - "bin" the binary of a gif file (read below ..)
So for this reason the beteljuice has found it easier to save each created frame and call from file using the "url" flag.
example:
imagegif($im, "w4.gif"); // save this as a gif to filename w4.gif
imagedestroy($im); // free the work image from memory
if you really want to work the created binary, you need to use the object into buffer method.
example: (some psuedo code)
ob_start();
imagegif($im); // normal output is suppressed
$frame[number] = ob_get_contents(); // $frame[] now has o/p of line above
$delay[number] = value in ms
ob_end_clean(); // flush contents of object buffer
imagedestroy($im);
To display the animation (in conjunction with the header)
echo $gif->GetAnimation();
exit;
NOTE: The animation has not been saved anywhere !
If you wanted to save the animation and NOT display it with this (url).php
remove the header line ... and ....
$data = $gif->GetAnimation();
fwrite(fopen("myanimation.gif", "wb"), $data);
exit;
There are so many ways to loop / branch when creating eg. a banner - so I'm not giving any kind of example code. It's up to you to play and experiment
THE SOURCE IMAGES MUST ALWAYS BE OF THE STATIC GIF TYPE
THE IMAGES SHOULD BE OF THE SAME WIDTH / HEIGHT
THERE MUST BE MORE THAN ONE IMAGE
Remember at the start - "PHP has absolutely NO functions to create / edit / crunch animated gifs"
Memorywise these animations can get quite large, so don't try to create a hundred frames or five frames the size of a house !
(See the file sizes of the attachments)
Spread the animation ...........