Welcome to the Cumulus Support forum.

Latest Cumulus MX V4 release 4.4.2 (build 4085) - 12 March 2025

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

If you are posting a new Topic about an error or if you need help PLEASE read this first viewtopic.php?p=164080#p164080

PHP and animated gifs

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

PHP and animated gifs

Post by beteljuice »

PHP created animated gifs :o

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)
banner2short.gif
(30 frames variant)
banner2long.gif
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
GIFEncoder.class.zip
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 ..)
When in the GD environment (eg. creating a banner) although what you have created is a binary, it hasn't had a format type applied to it until you use imagegif(....

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 :cry:

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 ........... :clap:
You do not have the required permissions to view the files attached to this post.
Last edited by beteljuice on Sun 20 Mar 2011 11:37 am, edited 3 times in total.
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
daj
Posts: 2041
Joined: Tue 29 Jul 2008 8:00 pm
Weather Station: WH1081
Operating System: Pi & MX
Location: SW Scotland
Contact:

Re: PHP and animated gifs

Post by daj »

Personally I hate banners when I am trying to read a forum, but that's just me!

Nice article, some good stuff in there. I can see it being useful in other areas.

Thanks Beteljuice :clap:
David
kippfordweather.uk
Cumulus MX & Raspberry Pi
Post Reply