Resize an animated GIF file without destroying the animation

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

How to do it using PHP?

+45
php image imagemagick animated-gif
Apr 05 '09 at 7:02
source share
13 answers

If you have access to imagemagick, 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 do not have access to the system ()

NOTE. This can create a larger file size, although the image is smaller due to coalescence, which significantly deoptimizes the image.

UPDATE: If you do not have access to ImageMagick, you can use a combination of the following steps to resize the animated gif (provided that you have access to GD):

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

If you earn, please share with the world!

+58
Apr 05 '09 at 7:09
source share

Try GDEnhancer (use ImageCraft ). He needs only the GD library, and it supports gif animation

+11
Aug 28 '13 at 9:26
source share

You need to decompose the gif into frames, a thumbnail and reassemble.
Take a look at ImageMagick: http://php.net/imagick and this tutorial: http://www.phpro.org/examples/Thumbnail-From-Animated-GIF.html

+9
Apr 05 '09 at 7:08
source share

I tried many examples of modifying animated GIFs using the Imagick PHP module, but none of them worked for me. Then, after some debugging time, I discovered the actual problem: the animation was lost when saving the image to disk, $animation->writeImage($file_dst); or $animation->writeImages($file_dst, true);

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

Hope this helps someone.

+5
Mar 13 '13 at 17:55
source share

In the example at http://www.php.net/manual/en/imagick.coalesceimages.php the size of your gif will be resized, while maintaining the time of your frame. Most of the other examples do not do something.

Other examples rebuild gifs as long as this allows you to change the frames of the image.

+3
Feb 22 '12 at 6:20
source share

If you have ImageMagick installed, you can use one call to convert :

 system("convert big.gif -coalesce -repage 0x0 -resize 200x100 -layers Optimize small.gif"); 
+3
Apr 20 '15 at 13:34
source share

I think I have it in my bag.

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

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

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

KNOWN ISSUES:

  • Support for transparency - this will be easy to add to this solution, but since I do not have it, I stop here.

  • Frame Rate - For an unknown reason, the GifEncoder class cannot accept the frame rate indicated. I will need to study this later.

  • I found one gif file from my test suite that somehow differed in the dimensional frames in it and this animation failed to work correctly. Some more debugging for this.

+2
Mar 22 '11 at 13:52
source share

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

and then run the script name as "resize_animator.php", create an html file to load and let the script enjoy.

.. 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 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 how the work happens in these folders. Not necessarily, but I use it to view functionality.

+1
Apr 7 '09 at 10:35
source share

All answers except ImageMagick do not work for me. The scripts in the answers before this are full of errors.

Even installing ImageMagick was a difficult task, so here is my experience.

Here's how to install ImageMagick on windows 7 and xampp 1.7.4.
Note: select 64 bit (for win7) and check the "Add to system path" checkbox.

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

I lost hours of scripting on these posts, and ImageMagick and this tutorial were successful in minutes.

And one more note: my web server has ImageMagick by default, so I think most servers have it.

+1
Sep 23 '11 at 10:00
source share

If you do not have Imagemagick on your server, you can try the following:

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

The class resizes GIF animations using GD. First, it analyzes the frames, and then resizes them, after which it again compiles them into a single file without losing delay time, deletion methods, color tables, etc.

Try it if you find an error or want to suggest some optimizations, etc., you can use the class forum or leave a comment on the page of your website. And I will answer it as soon as possible.

+1
Mar 11 '12 at 18:53
source share

GIF Animation Resizer is a simple classmate tool that does the trick.

Note. . It uses a temporary folder to record individual frames. Although these are timestamps for frames, you may need to create a unique folder if you intend to use it on a server where several users will resize GIFs at the same time.

+1
Feb 27 '15 at 10:45
source share

Imagecraft is a robust GD PHP library and extension that supports GIF animation, edits and composes images at several levels and supports watermarking.

0
Jan 20 '17 at 5:23
source share

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

}

0
May 9 '17 at
source share



All Articles