This tutorial will show you how to make a dynamic ANIMATED gif image. Most tutorials out there don’t show you how to make it animated, so I made this for you guys. For an example, look at my sig.

This tutorial was written by FatalisDK

You will need:
PHP with GDLib enabled
GIFEncoder.class.php http://rapidshare.com/files/97436745/GIFEncoder.class.rar.html
Mirror: http://rapidshare.com/files/97437184/GIFEncoder.class.rar.html

<?php

// We need this class to animate our images
require("GIFEncoder.class.php");

// Number of frames our image will have
$frameCount = 3;
 
// Turn on output buffering, so we capture the output of imagegif() instead of sending to client
ob_start();

// Create 3 frames
for ( $i = 0; $i < $frameCount; $i++ )
{
   // Use GDLib to make an image of 500 by 15 pixels
   $img = imagecreate(500, 15);
   
   // Allocate the background color (we will make this transparent later)
   $bg = imagecolorallocate($img, 255, 255, 255);

   // Allocate the foreground (text) color
   $fg = imagecolorallocate($img, 0, 0, 166);
   
   // Do different stuff depending what frame we’re on
   switch ( $i )
   {
   case 0:
   {
      $message = "This is our first frame";
      break;
   }
   case 1:
   {
      $message = "Second frame!";
      break;
   }
   case 2:
   {
      $message = "Final frame.";
      break;
   }
   }

   // Place our message at horizontal pixel 2, vertical pixel 1 with our $fg color
   // imagestring  ( resource $image  , int $font  , int $x  , int $y  , string $string  , int $color  )
   imagestring($img, 2, 2, 1, $message, $fg);
   
   // Create the image data
   imagegif($img);
   imagedestroy($img);
   // Get the image data from the output buffer and store in our frames array
   $imageFrames[$i] = ob_get_contents();

   // Set the delay from this frame to the next frame
   $frameDelay[$i] = 100;
   
   ob_clean();
}

/*
        GIFEncoder constructor:
        =======================

        image_stream = new GIFEncoder    (
                            URL or Binary data    ‘Sources’
                            int                    ‘Delay times’
                            int                    ‘Animation loops’
                            int                    ‘Disposal’
                            int                    ‘Transparent red, green, blue colors’
                            int                    ‘Source type’
                        );
*/

// Now we combine all of our images in the array to make an animated image
$gif = new GIFEncoder(
   $imageFrames,
        $frameDelay,
        0,
        2,
        255, 255, 255, // Make our background color transparent
        "bin"
);

// Make sure the client doesn’t cache the image
header("Expires: Mon, 5 Jun 1999 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: image/gif");

// And finally, send the animated gif to the client
echo $gif->GetAnimation();

?>

Here is what the above code would look like
 Tutorial  How to make a dynamic animated gif sig with php FREE Rapidshare Links for Download

Some forums only allow actual image extensions, so you may need to use mod_rewrite in an .htaccess file to change .php to .gif

# File: .htaccess - Dir: Same as your php script
RewriteEngine On
RewriteRule ^sig\.gif$ sig.php

I used these resources to learn how to do this:
http://mgccl.com/topics/animation
http://www.phpclasses.org/browse/package/3163.html
Link checked on Fri Mar 07, 2008 9:06 pm [WBB_Linkchecker_Bot]


This entry was posted on Saturday, March 8th, 2008 at 3:50 am and is filed under General. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment