PHP Classes

Resize animation files

Recommend this page to a friend!

      GIF images into animated GIF with native PHP class  >  All threads  >  Resize animation files  >  (Un) Subscribe thread alerts  
Subject:Resize animation files
Summary:Resize animation files without destroying animation
Messages:5
Author:Riad Hossain
Date:2009-04-10 11:09:40
Update:2009-06-20 09:22:10
 

  1. Resize animation files   Reply   Report abuse  
Picture of Riad Hossain 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>

  2. Re: Resize animation files   Reply   Report abuse  
Picture of László Zsidi László Zsidi - 2009-04-11 06:48:33 - In reply to message 1 from Riad Hossain
you should use the original dly array for building your new animation.

  3. Re: Resize animation files   Reply   Report abuse  
Picture of Riad Hossain Riad Hossain - 2009-04-12 03:15:07 - In reply to message 2 from László Zsidi
Thanks for your reply,
Could u pls help me, how I use the original dly array when the image is going to Encode as a animated gif [show in CODE] . Some of the animated files are not decoded properly. Give me a solution about the broken frames. Also, your e-mail : support@gifs.hu is not working . Waiting for your response.
Thanks again

  4. Re: Resize animation files   Reply   Report abuse  
Picture of László Zsidi László Zsidi - 2009-04-12 04:26:00 - In reply to message 3 from Riad Hossain
gifs.hu domain and email aliases are closed.
you can contact with me at zsidi.laszlo@upcmail.hu.
i would like to ask you, if you can please send me your example code with image sources and with the php process file.

  5. Re: Resize animation files   Reply   Report abuse  
Picture of Kaushal Parekh Kaushal Parekh - 2009-06-20 09:22:10 - In reply to message 4 from László Zsidi
Some of the animated files are not decoded properly.Animation will be broken

Any one has idea?????????

Thanks