Featured · PHP · Programlama

PHP Animated Gif Resizer

There are many sites that use avatars and sometimes they use animated GIF files. But when it comes to resizing them, it really becomes a pain when your server doesn’t support ImageMagick. So I decided to write an animated GIF resizer based on GD Library (Because GD doesn’t support it directly either.)

It works like this:

  1. Extract frames from GIF animations into a temporary folder.
  2. Resizes all the frames
  3. Combines them into one file

Using this class is really simple. Here is the code

<?php
include_once "gifresizer.php";
$gr = new gifresizer;
$gr->temp_dir = "frames"; //note that it doesn't end with a "/"
$gr->resize("gif/1.gif","resized/1.gif",200,300);
 
// $gr->temp_dir = {a folder with a write permission (777)}; // The temporary folder which frames will be extracted to.
// $gr->resize({file to be resized}, {new file to be created}, {new width}, {new height});
?>

Here’s an example:

 

Original GIF file :


Resized Image:


Here you can download the class, documentation and examples.

PHPClasses.org class main page

 

Note: I’m expecting some feedback. Here or at phpclasses.org. Doesn’t matter. If you are using it, tell me how it works.

6 thoughts on “PHP Animated Gif Resizer

  1. Thanks an keep up the good work.
    Anyway, I’ve tried to resize a gif from larger to smaller (from 720×90 to 320×40) but it lose quality and the resized gif is grainy.
    Is it possible to preserve the original frame’s quality?

    1. This script uses PHP GD’s image resize function, for your request to be happen, it should use image resample function.
      But if I use that for resizing, the palette changes on resample, and I can not prese

  2. Slight issue in that when I resize a gif using this it works fine. When I resize a gif which only loops once, this library makes it loop indefinitely. From what I could see in the class, it does not identify whether the GIF should loop. Any idea if thi

    1. It’s OK thanks, I’ve rewitten some of it so that this now works. If you are interested in seeing what I did for this, drop me an email and I’ll show you what I’ve done to extend the class.

      1. Hi, happy to hear that you’ve solved the problem. I didn’t have the chance to check all the GIF variations, so it still may be containing bugs. I really don’t have the time to tinker with this class, but when I have enough time, I’ll check them all.

Leave a Reply