PHP Classes
elePHPant
Icontem

GIFEncoder.class.php

Recommend this page to a friend!

      GIF images into animated GIF with native PHP class  >  All threads  >  GIFEncoder.class.php  >  (Un) Subscribe thread alerts  
Subject:GIFEncoder.class.php
Summary:GIFEncoder.class.php problem
Messages:8
Author:Curtis LeBlanc
Date:2007-11-10 22:53:32
Update:2007-11-18 10:51:30
 

 


  1. GIFEncoder.class.php   Reply   Report abuse  
Curtis LeBlanc - 2007-11-10 22:53:32
Hello everyone, I'm having trouble using GIFEncoder.class.php I'm trying to make an image of the current date fading in and out. I got the gd code done, but I added the GIFEncoder.class.php code and It stoped working. GIFEncoder.class.php is saying that my images aren't gifs, but I know they are. I looked at the gifs and noticed the gifs you used in the example were GIF89a's and mine are GIF87a's, and I think that might be the problem. Could someone help me?
This is my code:
<?php
$color = 0;
for ( $l=0; $l<11; $l++)
{

$im = @imagecreate(400, 24)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, $color, $color, $color);
$font = imageloadfont("font.gdf");
imagestring($im, $font, 4, 0, date("F j, Y"), $text_color);
imagegif($im, "frames/images".$l.".gif");
imagedestroy($im);
$color=$color+25.5;
}
include "GIFEncoder.class.php";
/*
Build a frames array from sources...
*/
if ( $dh = opendir ( "frames/" ) ) {
while ( false !== ( $dat = readdir ( $dh ) ) ) {
if ( $dat != "." && $dat != ".." ) {
$frames [ ] = "frames/$dat";
$framed [ ] = 5;
}
}
closedir ( $dh );
}
/*
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'
);
*/
$gif = new GIFEncoder (
$frames,
$framed,
0,
2,
0, 0, 0,
"url"
);



FWrite ( FOpen ( "myanimation.gif", "wb" ), $gif->GetAnimation ( ) );
?>

  2. Re: GIFEncoder.class.php   Reply   Report abuse  
Curtis LeBlanc - 2007-11-11 19:16:28 - In reply to message 1 from Curtis LeBlanc
I just got it working by using:
$frames = Array("frames/images0.gif", "frames/images1.gif","frames/images2.gif","frames/images3.gif",
"frames/images4.gif","frames/images5.gif","frames/images6.gif",
"frames/images7.gif","frames/images8.gif","frames/images9.gif",
"frames/images10.gif","frames/images11.gif","frames/images12.gif",
"frames/images13.gif","frames/images14.gif","frames/images15.gif",
"frames/images16.gif","frames/images17.gif","frames/images18.gif",
"frames/images19.gif","frames/images20.gif");
$framed = Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);

But now I'm having another problem after the gif has played once, and goes
to play again there is a white flicker, even thought I only have one blank white frame and it's in the middle. At first I thought this was normal, but your example gif never flickers. how can I fix this???

  3. Re: GIFEncoder.class.php   Reply   Report abuse  
Curtis LeBlanc - 2007-11-11 21:43:07 - In reply to message 2 from Curtis LeBlanc
I just edited the code so that the image is just big enough to hold the text, and I also made it so that the text fades in and out not just in, and some other stuff.
Here is the new code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$color = 0;
for ( $l=0; $l<11; $l++)
{

$font = imageloadfont("font.gdf");
$im = @imagecreate(round((strlen(date("F j, Y"))*imagefontwidth($font)))+2, round(imagefontheight($font))+2)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, $color, $color, $color);

imagestring($im, $font, 1, 1, date("F j, Y"), $text_color);

imagegif($im, "frames/images".$l.".gif");
imagedestroy($im);
$color=$color+25.5;
$frames[$l]="frames/images".$l.".gif";
$framed[$l]=1;
}
$color = 255-25.5;

for ( $l=11; $l<21; $l++)
{

$font = imageloadfont("font.gdf");
$im = @imagecreate(round((strlen(date("F j, Y"))*imagefontwidth($font)))+2, round(imagefontheight($font))+2)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, $color, $color, $color);

imagestring($im, $font, 1, 1, date("F j, Y"), $text_color);

imagegif($im, "frames/images".$l.".gif");
imagedestroy($im);
$color=$color-25.5;
$frames[$l]="frames/images".$l.".gif";
$framed[$l]=1;
}
include "GIFEncoder.class.php";

$gif = new GIFEncoder (
$frames,
$framed,
0,
0,
0, 0, 0,
"url"
);



FWrite ( FOpen ( "myanimation.gif", "wb" ), $gif->GetAnimation ( ) );
?>

<img src="myanimation.gif">

It just thought it might be good if you could see the newest code encase it helps you find the problem.

  4. Re: GIFEncoder.class.php   Reply   Report abuse  
László Zsidi - 2007-11-12 18:21:38 - In reply to message 3 from Curtis LeBlanc
Hi!

Be sure, that your stored frames are exists and when you used these frames the addresses are correct!?
If you use a non-existent frame then the execution of gifencoder has break and appear an error message, say that your source is not a valid gif image.
Other ways of your conception, that will be used to pass the modified frames into the gifencoder class by the ob_get_contents ( ) function directly without storing on your webserver; as a binary content.

// Within the main cycle
ob_start();
imageGif($modified_image_resource);
$frames[] = ob_get_contents();
$framed[] = 10;
imageDestroy($modified_image_resource);
ob_end_clean();

Notice:
Did not forget that use the "bin" flag instead of "url" flag when construct the gifencoder class!

  5. Re: GIFEncoder.class.php   Reply   Report abuse  
Curtis LeBlanc - 2007-11-14 20:49:45 - In reply to message 1 from Curtis LeBlanc
I looked at the animation in gimp and it looked like it was makeing the color 0,0,0 as white or tranprent in the first frame so I changed it to use 25.5 as the color to start at. It started working after that, I thought i would tell you in case it was a problem with GIFEncoder.class.php. Before the frames got put together with GIFEncoder.class.php the first one was black not white and looked fine so i know the problem isn't with the gd library. But i could have just did something wrong because i'm new at gd and php.

  6. Re: GIFEncoder.class.php   Reply   Report abuse  
László Zsidi - 2007-11-16 14:05:58 - In reply to message 5 from Curtis LeBlanc
Hi!

This is a popular problem, but must be tell you some additional informations about GIF format.
The GIF image can be used to store and to display 256 difference colors maximum, therefore if you want to use a truecolor image ( has 16 million colors ) such as jpeg or png, shall be reduced the used color numbers into 256 colors without bigger deteorientation of quality.
Can be used to convert truecolor images into images has 256 colors by a quantizer routines or programs, or by GD library.
Example:

$truecolor_image = imageCreateFromJpeg ( 'jpeg_image.jpg' );
$non_truecolor_image = imageCreate ( imageSx ( $truecolor_image ), imageSy ( $truecolor_image ) );
imageFill ( 0, 0, imageColorAllocate ( 255, 255, 255, $non_truecolor_image ), $non_truecolor_image );
imageCopy ( $non_truecolor_image, $truecolor_image, 0, 0, 0, 0, imageSx ( $truecolor_image ), imageSy ( $truecolor_image ) );
imageDestroy ( $truecolor_image );

So, befall that color has been declared as white ( 255, 255, 255 ) after quantzing has been modified as white with these RGB components: 252, 254, 252
Determination of the transparent color in an image has reduced colors into 256 it's a very difficult way...

Easy way to determine the real color components at XY coords by GD library. "ImageColorAt ( $x, $y, $non_truecolor_image_resource );"

  7. Re: GIFEncoder.class.php   Reply   Report abuse  
László Zsidi - 2007-11-18 10:42:13 - In reply to message 1 from Curtis LeBlanc
Simple way to determine source images from a staticaly path:

<?php
define ( PATH, "mydirectory/stored_images/" );
$paths = Array ( );
$masks = Array ( "gif", "jpg", "peg", "png" );

function getExt ( $filename ) {
return strtolower ( substr ( $filename, strlen ( $filename ) - 3, 3 ) );
}

if ( $dh = opendir(PATH)){
while ( $file = readdir ( $dh ) ) {
if ( ( $file != "." ) && ( $file != ".." ) ) {
if ( in_array ( getExt ( $file ), $masks ) ) {
$paths = PATH . $file;
}
}
}
closedir ( $dh );
}
?>

  8. Re: GIFEncoder.class.php   Reply   Report abuse  
László Zsidi - 2007-11-18 10:51:30 - In reply to message 7 from László Zsidi
Made a little modification in my code above "$paths [ ] = PATH . $file;" instead of "$paths = PATH . $file;"