//
you're reading...

Featured

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.

Discussion

6 Responses to “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?

    Posted by colmorex | Wednesday, August 6, 2014, 11:07
    • 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 preserve some transparent pixels on the GIF. That’s why you get some decrease on quality after resizing.

      Posted by Taha Paksu | Friday, August 8, 2014, 08:23
  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 this could be updated to encompass this change?

    Posted by mark | Wednesday, September 10, 2014, 18:11

Trackbacks/Pingbacks

  1. [...] eklediÄŸim PHP Animated Gif Resizer class’ım Åžubat 2012 innovasyon ödülü için yarışıyor. DesteÄŸinizi esirgemeyin. [...]

    Yine yarışmadayım! | tahapaksu - Thursday, March 1, 2012

Leave a Reply