PHP Classes

PHP GIF Resizer: Resize images and animations based on the GIF89a

Recommend this page to a friend!
  Info   View files Example   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-03 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 312 All time: 7,276 This week: 104Up
Version License PHP version Categories
gif-resizer 1.0.5GNU Lesser Genera...5.3PHP 5, Graphics, Video
Description 

Author

This package can resize images and animations based on the GIF89a.

It takes the path of a given GIF image or animation file and creates a resized version with a specified width, height or ratio.

The resized image is saved to another file with a given file name.

Picture of Asbjorn Grandt
Name: Asbjorn Grandt <contact>
Classes: 10 packages by
Country: Denmark Denmark
Innovation award
Innovation award
Nominee: 4x

Example

<?php
error_reporting
(E_ALL | E_STRICT);
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 1);

include
"../vendor/autoload.php";

use
grandt\ResizeGif\ResizeGif;

// Rotating_earth and SmallFullColourGIF are from the GIF Wikipedia page.
//// $srcFile = 'resources/Rotating_earth_(large).gif';
$srcFile = 'resources/SmallFullColourGIF.gif';
// $srcFile = 'resources/frame_diff_ir2t.gif';
$dstFile1 = 'resources/test1.gif';
$dstFile2 = 'resources/test2.gif';
$dstFile3 = 'resources/test3.gif';
echo
"<!doctype html><html><head><title>ResizeGif.Test1</title></head><body>
<p>Files are used for in and output, as you really don't want to
dynamically resize animated gifs every time they are used.</p><pre>\n"
;

showStats($srcFile);

echo
"Resizing to half size\n";
ResizeGif::ResizeByRatio($srcFile, $dstFile1, 0.5);
showStats($dstFile1);

echo
"Resizing to 2x\n";
ResizeGif::ResizeByRatio($srcFile, $dstFile2, 2);
showStats($dstFile2);

echo
"Resizing to 200px wide\n";
ResizeGif::ResizeToWidth($srcFile, $dstFile3, 200);
showStats($dstFile3);

echo
"</pre></body></html>\n";

/**
 * @param string $file
 * @throws Exception
 */
function showStats($file) {
   
$size = ResizeGif::getSize($file);
    echo
"File: $file:\n";
    echo
" - width.: " . $size['width'] . "\n";
    echo
" - height: " . $size['height'] . "\n";
    echo
"<img src='$file' />\n";
    echo
"\n";
}


Details

Resize animated Gif files

This package aims to implement a proper resizing of gif files encompassing the GIF89a specification.

Introduction

Most, if not all other publicly available gif resize packages fails with optimized gif files, those where only parts of the file are updated in subsequent frames. See these as a background image, with sprites moving about. The resulting gif will retain its aspect ratio.

The package is a bit pedantic in its approach. It was made as much for me to learn what Gifs were and how they work, as it was to solve specific problem.

Usage

The package needs to write to a file, the reasons for not just return a string is twofold. One being memory usage, the other is that you really don't want to be dynamically resizing often used gif files every time they are used.

Import

Add this requirement to your composer.json file:

    "grandt/phpresizegif": ">=1.0.3"

Composer

If you already have Composer installed, skip this part.

Packagist, the main composer repository has a neat and very short guide.

Or you can look at the guide at the Composer site.

The easiest for first time users, is to have the composer installed in the same directory as your composer.json file, though there are better options.

Run this from the command line:

php -r "readfile('https://getcomposer.org/installer');" | php

This will check your PHP installation, and download the composer.phar, which is the composer binary. This file is not needed on the server though.

Once composer is installed you can create the composer.json file to import this package.

{
    "require": {
        "grandt/phpresizegif": ">=1.0.3"
    }
}

Followed by telling Composer to install the dependencies.

php composer.phar install

this will download and place all dependencies defined in your composer.json file in the vendor directory.

Finally, you include the autoload.php file in the new vendor directory.

<?php
    require 'vendor/autoload.php';
    .
    .
    .

Initialization

include "../vendor/autoload.php";
use grandt\ResizeGif\ResizeGif;

$srcFile = "[path to original gif file]";
$dstFile = "[path to resized file]";

ResizeGif::ResizeToWidth($srcFile, $dstFile, 100);

To make a 100 pixel wide thumbnail

ResizeGif::ResizeToWidth($srcFile, $dstFile, 100);

To make a 100 pixel high thumbnail

ResizeGif::ResizeToHeight($srcFile, $dstFile, 100);

To double the size of the gif.

ResizeGif::ResizeByRatio($srcFile, $dstFile, 2.0);

To half the size of the gif.

ResizeGif::ResizeByRatio($srcFile, $dstFile, 0.5);

  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (4 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login HTML file GNU Lesser General...are Foundation.html Doc. Documentation
Accessible without login Plain text file README.md Doc. Auxiliary data
Accessible without login Plain text file REVISION.TXT Data Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageResizeGif (3 files, 3 directories)

  Files folder image Files  /  src  /  ResizeGif  
File Role Description
Files folder imageDebug (1 file)
Files folder imageFiles (3 files)
Files folder imageStructure (8 files)
  Plain text file CreateGif.php Class V1.0.2: Added: CreateGif. CreateGif is a simple Gif Animation creation script.
  Plain text file ExtractGif.php Class Gif frame extraction
  Plain text file ResizeGif.php Class Class source

  Files folder image Files  /  src  /  ResizeGif  /  Debug  
File Role Description
  Plain text file DebugGif.php Class Class source

  Files folder image Files  /  src  /  ResizeGif  /  Files  
File Role Description
  Plain text file DataHandler.php Class Class source
  Plain text file FileHandler.php Class Class source
  Plain text file ImageHandler.php Class Class source

  Files folder image Files  /  src  /  ResizeGif  /  Structure  
File Role Description
  Plain text file AbstractExtensionBlock.php Class Class source
  Plain text file ApplicationExtension.php Class Class source
  Plain text file CommentExtension.php Class Class source
  Plain text file GraphicControlExtension.php Class Class source
  Plain text file Header.php Class Class source
  Plain text file ImageDescriptor.php Class Class source
  Plain text file LogicalScreenDescriptor.php Class Class source
  Plain text file PlainTextExtension.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageresources (1 file)
  Accessible without login Plain text file CreateGif.Test1.php Example V1.0.2: Added: CreateGif. CreateGif is a simple Gif Animation creation script.
  Accessible without login Plain text file ExtractGif.Test1.php Example Gif frame extraction example
  Accessible without login Plain text file ResizeGif.Test1.php Example Example script
  Accessible without login Plain text file ResizeGif.Test2.php Example Example script

  Files folder image Files  /  tests  /  resources  
File Role Description
  Accessible without login Image file frame_diff_ir2t.gif Icon Example gif file.

 Version Control Unique User Downloads Download Rankings  
 100%
Total:312
This week:0
All time:7,276
This week:104Up