Riad Hossain - 2009-04-10 11:09:40
Hi,
No doubt that the GIFEncoder and Decoder both class is excellent and make easy our task.That’s why first I give u a big thanks. I create a animated gif resizer using your two class.
Code is below. But face 2 problems:
1. If mother file frame delay have variance in delay time then output file don’t have the delay variance. Output animation file use same delay time as declared in the code. How I get the original delay time and use the time to build the resized animation file.
2. The GIFDecoder.class.php class cannot decode some animation files properly. Some time it show garbage output frames .As a result I get garbage resized animation.
I collected your class files from
1. Decode Class: http://phpclasses.elib.com/browse/package/3234.html
2. Encode Class:http://phpclasses.betablue.net/browse/package/3163.html
<?php
require "GIFDecoder.class.php";
include "GIFEncoder.class.php";
//decode the test.gif file
$fp = fread ( fopen ( "test.gif", "rgb" ), filesize ( "test.gif" ) );
if ( $fp )
{
$gif = new GIFDecoder ( $fp );
$arr = $gif->GIFGetFrames ( );
$dly = $gif->GIFGetDelays ( );
for ( $i = 0; $i < count ( $arr ); $i++ )
{
fwrite ( fopen ( ( $i < 10 ? "frame_output/$i$i_frame.gif" : "frame_output/$i_frame.gif" ), "wb" ), $arr [ $i ] );
}
for ( $i = 0; $i < count ( $dly ); $i++ )
{
sprintf ( "Delay of %d frame is %d ms<br>", $i, ( $dly [ $i ] * 10 /* milliseconds */ ) );
}
}
// Take the frames from "frame_output" folder and after resizing put it into "resized_frame_output" folder.
function resize_frames($newwidth,$newheight)
{
$dir=opendir("frame_output/");
$i=0;
while($imgfile=readdir($dir))
{
if ($imgfile != "." && $imgfile!="..")
{
$imgarray[$i]=$imgfile; $uploadedfile = "frame_output/".$imgarray[$i];
$src = imagecreatefromgif($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "resized_frame_output/".$imgarray[$i];
imagegif($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
$i++;
}
}
closedir($dir);
// Using GIFEncoder.class.php ,take the resized output from
// "resized_frame_output" folder and put the output animated files into
// "resized_animated_gif_output" folder.
if ( $dh = opendir ( "resized_frame_output/" ) )
{
while ( false !== ( $dat = readdir ( $dh ) ) )
{
if ( $dat != "." && $dat != ".." )
{
$frames [ ] = "resized_frame_output/$dat";
$framed [ ] = 10;
}
}
closedir ( $dh );
}
$gif = new GIFEncoder ( $frames, $framed, 0, 2, 0, 0, 0,"url" );
$data = $gif->GetAnimation ( );
$x='x';
$output_image_name=$newwidth.$x.$newheight;
fwrite ( fopen ( "resized_animated_gif_output/$output_image_name.gif", "wb" ), $data );
//sleep for 1 second
usleep(1000000);
//remove resized frames from folder
$dir = 'resized_frame_output/';
foreach(glob($dir.'*.*') as $v)
{
unlink($v);
}
}// end of function resize_frames
resize_frames(110,110);
resize_frames(120,160);
resize_frames(120,80);
resize_frames(128,96);
//Intentionally use the delay function to view the result of output frames before delete
usleep(5000000);
//remove resized frames from folder
$dir = 'frame_output/';
foreach(glob($dir.'*.*') as $v)
{
unlink($v);
}
echo "<center><h1>Your Animation processing is compleated.</h1></center>";
?>
<center><img src="test.gif" height="300" width="350"></center>