How to Resize animated images with animation effects?
by barkkathulla[ Edit ] 2013-03-07 20:26:46
How to Resize animated cards with animation effect?
But needs implementation like to get exact frame work and exact background and foreground effect...In this work it depends on image frame conversion.
For this purpose needs are,
*GD library concepts
*GIF Decoder
*GIFEncoder
$animation ="...image path....;
$gifDecoder = new GIFDecoder ( fread ( fopen ( $animation, "rb" ), filesize ($animation) ) );
$delay = $gifDecoder->GIFGetDelays ( ); // get the delay between each frame
//Required size formate
$i = 1;
foreach ( $gifDecoder -> GIFGetFrames ( ) as $frame ) {
fwrite ( fopen ( "resizeGif/tmp_reduced/image$i.gif" , "wb" ), $frame );
$img = "resizeGif/tmp_reduced/image$i.gif"; // File image location
$newfilename = "resizeGif/tmp_reduced/image$i.gif"; // New file name
$w = 956;
$h = 600;
resize($img, $w, $h, $newfilename);
$i++;
}
function resize($img, $w, $h, $newfilename) {
//Loading GD extension
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get size
$imgInfo = getimagesize($img);
switch ($imgInfo[2]) {
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
//IF lesser size do not create
if ($imgInfo[0] <= $w && $imgInfo[1] $h/$imgInfo[1]) {
$nWidth = $w;
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
}else{
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
$nHeight = $h;
}
}
$nWidth = round($nWidth);
$nHeight = round($nHeight);
$newImg = imagecreatetruecolor($nWidth, $nHeight);
// Check Image transparent type
if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
switch ($imgInfo[2]) {
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename); break;
case 3: imagepng($newImg,$newfilename); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
return $newfilename;
}
//Build a frames array from sources
$i = 1;
if ( $dh = opendir ( "resizeGif/tmp_thumbnail/" ) ) {
while ( false !== ( $dat = readdir ( $dh ) ) ) {
if ( $dat != "." && $dat != ".." ) {
$framesThumb [ ] = "resizeGif/tmp_thumbnail/image$i.gif";
$i++;
}
}
closedir ( $dh );
}
$gifThumb = new GIFEncoder (
$framesThumb,
$delay,
0,
2,
0, 0, 0,
"url"
);
//storage path
$fpThumb = fopen('....path with image name and extension...', 'w');
fwrite($fpThumb, $gifThumb->GetAnimation());
fclose($fpThumb);