Question

I need to resize an animated GIF file without destroying the animation.

How can I do it using PHP?

Was it helpful?

Solution

if you have imagemagick access, you can do this:

system("convert big.gif -coalesce coalesce.gif");
system("convert -size 200x100 coalesce.gif -resize 200x10 small.gif");

this is most likely possible with the imagemagick plugin if you don't have system() access

NOTE: this may create a larger filesize though a smaller dimensions image due to coalescing essentially deoptimizing the image.

UPDATE: If you don't have ImageMagick access, you should be able to use a combination of the following steps to resize an animated gif (assuming you have GD access):

  1. Detect if the image is an animated gif: Can I detect animated gifs using php and gd? (top answer)
  2. Split the animated gif into individual frames: http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html
  3. Resize the individual frames: http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
  4. Recomposite the frames into an animated gif again: http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

This is definitely much more intensive than the ImageMagick route, but it should be technically possible.

If you get it working, please share with the world!

OTHER TIPS

Try GDEnhancer (Use ImageCraft). It only need GD Library, and it keep gif animation

You would need to decompose the gif into frames, thumbnail and re-assemble.

Have a look at ImageMagick and this tutorial.

The example on http://www.php.net/manual/en/imagick.coalesceimages.php will resize your gif while retaining your frame timing. Something most of the other examples do not do.

Other examples rebuild the gif while this one allows you to modify the frames of the image.

I have tried numerous examples of resizing animated GIFs with Imagick PHP module, but none of them worked for me. Then after some debugging time at last I found the actual issue: the animation was lost upon saving image to disk, by $animation->writeImage($file_dst); or $animation->writeImages($file_dst, true);

I've changed it to file_put_contents($file_dst, $animation->getImagesBlob()); and most examples started working immediately.

Hope it helps someone.

If you have ImageMagick installed then you can use a single call to convert:

system("convert big.gif -coalesce -repage 0x0 -resize 200x100 -layers Optimize small.gif");

I think I've got this in the bag.

This solution is by no means perfect and contains some brute force here and there, but I was able to append my GD / PHP based image resizing script with enough functionality to support animations.

The solution is heavily based on the excellent freeware libraries by László Zsidi - http://www.phpclasses.org/browse/author/283569.html

You can check out a quick demo and download the sources from http://forssto.com/gifexample/ (direct link: http://forssto.com/gifexample/gifanimresize.zip )

KNOWN ISSUES:

  • Transparency support - this would be easy to append to this solution, but as I don't have an immediate need for this, I'm stopping here.

  • Frame rate - for some unknown reason the GifEncoder class fails to take into account the frame rates specified. I'll need to look into this later.

  • I did find one gif file from my set of tests that somehow had different sized frames in it and that animation failed to work correctly. Still some debugging to do then.

just creat 3 folder name 1.frame_output 2.images 3.resized_frame_output and download 2 encoder and decoder class from this below link 1.Download class "GIFDecoder.class.php" from http://phpclasses.elib.com/browse/package/3234.html 2.Download class "GIFEncoder.class.php" from http://phpclasses.betablue.net/browse/package/3163.html

and then run the script name as "resize_animator.php" , creat a upload html file & Let Enjoy the script.

..save this script as .....index.php.......

<html>
<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="resize_animator.php" method="post" enctype="multipart/form-data" >
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td align="center"><font face="Tahoma">SELECT ANIMATED FILE</font> 
<input type="file" name="uploadfile" size="20" accept="image/gif"/>
</td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="PROCESS ANIMATION" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

........................save and this script as resize_animator.php............

   <?php

   require "GIFDecoder.class.php";
   include "GIFEncoder.class.php";
   $file_name= $_FILES['uploadfile']['name'];
   $file_ext = substr($file_name, -4);
   $file_size=($_FILES["uploadfile"]["size"] /1024 );
   if($file_ext=='.gif')
    {
 if($file_size > 0 && $file_size < 2000 )
  {
    session_start ( );
        $uploaded_file = $_FILES['uploadfile']['tmp_name'];
        $fp=file_get_contents($uploaded_file);

        if ( $fp )
            {
                $_SESSION['delays'] = Array ( );
                $gif = new GIFDecoder ( $fp );
                $arr = $gif->GIFGetFrames ( );
                $_SESSION [ 'delays' ] = $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 ] );
                }
          }

        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);

                if ( $dh = opendir ( "resized_frame_output/" ) )
                {
                    while ( false !== ( $dat = readdir ( $dh ) ) )
                    {
                        if ( $dat != "." && $dat != ".." )
                        {
                            $frames [ ] = "resized_frame_output/$dat";
                        }
                    }
                    closedir ( $dh );
                }

            $gif = new GIFEncoder   ( $frames,$_SESSION [ 'delays' ],0, 2, 0, 0, 0,"url" );
            $data = $gif->GetAnimation ( );

            $x='x';
            $y='_';
            $uploaded_file_name= $_FILES['uploadfile']['name'];
            $actual_file_name = substr($uploaded_file_name, 0, -4);
            $file_extention = substr($uploaded_file_name, -4);
            $new_name=$actual_file_name.$y.$newwidth.$x.$newheight.$file_extention ;

            //$output_image_name=$newwidth.$x.$newheight;
            fwrite ( fopen ( "images/$new_name", "wb" ), $data );
            //remove resized frames from folder
            //sleep for 1 second
            // usleep(2000000);
            $dir = 'resized_frame_output/';
            foreach(glob($dir.'*.*') as $v)
                {
                 unlink($v);
                }
        }  // end of function resize_frames


            $gif = new GIFEncoder   ( $frames,$_SESSION [ 'delays' ],0, 2, 0, 0, 0,"url" );
            $data = $gif->GetAnimation ( );

            $x='x';
            $y='_';
            $z='_p';
            $uploaded_file_name= $_FILES['uploadfile']['name'];
            $actual_file_name = substr($uploaded_file_name, 0, -4);
            $file_extention = substr($uploaded_file_name, -4);
            $new_name=$actual_file_name.$y.$newwidth.$x.$newheight.$z.$file_extention ;

            //$output_image_name=$newwidth.$x.$newheight;
            fwrite ( fopen ( "images/$new_name", "wb" ), $data );
            //remove resized frames from folder
            //sleep for 1 second
             //usleep(2000000);
            $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);
            resize_frames(128,128);
            resize_frames(208,208);
            resize_frames(208,320);

            session_destroy();

            //usleep(200000);

            //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>";
      echo "<center><h2><a href=\"index.php\">BACK TO UPLOAD PAGE</h2></center>";
  }  //end of file size checker
else
 {
      echo "<center><h2>You Upload a unfit size image .Upload a file within 2000 KB</h2></center>";
      echo "<center><h2><a href=\"index.php\">BACK TO UPLOAD PAGE</h2></center>";
 }
   } //end of file extention checker
  else
  {
   echo "<center><h2>Uplaod a gif file!</h2></center>";
   echo "<center><h2><a href=\"index.php\">BACK TO UPLOAD PAGE</h2></center>";
  }
  ?>

.......................LETS ENJOY............

uncomment the usleep function to see the work happen into those folders.its not necessary but i use it to see the functionality.

If you dont have Imagemagick in your server you may want to try this :

http://www.phpclasses.org/package/7353-PHP-Resize-animations-in-files-of-the-GIF-format.html

The class is resizing GIF animations with GD. First parses the frames, then resizes them, after that it compiles them again into single file without losing its delay times,disposal methods, color tables etc.

Try and if you find a bug or want to suggest some optimizations etc. you can use the class' forum or leave a comment on the page in my website. And I'll answer that ASAP.

This all answers except trough ImageMagick didnt work for me. Scripts in answers before this are all full of errors.

Even installing ImageMagick was difficult to manage so here is my experience.

Here is how to install ImageMagick on windows 7 and xampp 1.7.4.
Note: Choose 64 bit (for win7), and when installing leave checked the option "Add to system path".

And then follow: http://www.creativearmory.com/tutorials/resize-animated-gifs-with-php-and-imagemagick

I have lost hours on scripts at this posts, and ImageMagick and this tutorial was sucessfull in minutes.

And one more note: my webserver have ImageMagick by default, so i guess most of servers have it too.

GIF Animation Resizer is a simple one-class tool that will do the trick.

Note: It uses a temporary folder to write out the separate frames. While it timestamps the frames, you may need to create a unique folder if you intend to use this on a server where multiple users will be resizing GIFs at the same time.

Imagecraft is a reliable PHP GD library and extension that keeps GIF animation, edit and compose images in multiple layers and supports watermark.

I used this function:

function gifResize($file_origin,$file_dest,$percent){       
   $crop_w = 0;
   $crop_h = 0;
   $crop_x = 0;
   $crop_y = 0;
   $image = new Imagick($file_origin);
   $originalWidth = $image->getImageWidth();
   $originalHeight = $image->getImageHeight();
   $size_w = ($originalWidth*$percent)/100;
   $size_h = ($originalHeight*$percent)/100;
   if(($size_w-$originalWidth)>($size_h-$originalHeight)){
       $s = $size_h/$originalHeight;
       $size_w = round($originalWidth*$s);
       $size_h = round($originalHeight*$s);
   }else{
       $s = $size_w/$originalWidth;
       $size_w = round($originalWidth*$s);
       $size_h = round($originalHeight*$s);
   }       
   $image = $image->coalesceImages();

   foreach ($image as $frame) {
       $frame->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
       $frame->thumbnailImage($size_h, $size_w);
       $frame->setImagePage($size_h, $size_w, 0, 0);
   }
   $imageContent = $image->getImagesBlob();
   $fp = fopen($file_dest,'w');
   fwrite($fp,$imageContent);
   fclose($fp);

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top