GifFrameExtractor is a PHP class that separates all the frames (and their duration) of an animated GIF

Overview

================================

GifFrameExtractor

================================

GifFrameExtractor is a PHP class that separates all the frames (and their duration) of an animated GIF

For what ?

The class helps you to separate all the frames of an animated GIF, for example to watermark them and then to generate a new watermarked and animated GIF.

Usage

GifFrameExtractor is really easy to use:

1 - Extraction:

$gifFilePath = 'path/images/picture.gif';

if (GifFrameExtractor::isAnimatedGif($gifFilePath)) { // check this is an animated GIF
    
    $gfe = new GifFrameExtractor();
    $gfe->extract($gifFilePath);
    
    // Do something with extracted frames ...
}

2 - Getting the frames and their duration:

foreach ($gfe->getFrames() as $frame) {
    
    // The frame resource image var
    $img = $frame['image'];
    
    // The frame duration
    $duration = $frame['duration'];
}

You can also get separately an array of images and an array of durations:

$frameImages = $gfe->getFrameImages();
$frameDurations = $gfe->getFrameDurations();

And obtain usefull informations:

$totalDuration = $gfe->getTotalDuration(); // Total duration of the animated GIF
$frameNumber = $gfe->getFrameNumber(); // Number of extracted frames
var_dump($gfe->getFrameDimensions()); // An array containing the dimension of each extracted frame
var_dump($gfe->getFramePositions()); // An array containing the original positions of each extracted frame inside the GIF

Option:

You can choose if you want to get the original frames (with transparency background) or frames pasted on the first one with the second parameter of extract() method:

$gfe->extract('path/images/picture.gif', true); // Can get transparency orignal frames

This option is false by default.

About

The class reuses some part of code of "PHP GIF Animation Resizer" by Taha PAKSU (thanks to him).

Comments
  • Add method to fetch the number of times a gif loops.

    Add method to fetch the number of times a gif loops.

    Can I request that a method be added which tells you the number of times a gif loops? I'm extracting frames then resizing them and then using your other library (GifCreator) to recompile the frames back into an animated gif.

    For the moment I'm setting it to loop indefinitely, but ideally I should specify the number of loops as specified in the source file.

    If there's a simple vanilla PHP way of getting the loops then I'm all ears!

    opened by hellopablo 6
  • Frame Durations

    Frame Durations

    It seems that the frame durations are missing a place. When I get the frame duration of a 10000ms frame, it returns only 1000. When I get the frame duration of a 200ms frame, it returns only 20. Unless I'm mistaken.

    opened by logger24 3
  • about GifFrameExtractor not found problem

    about GifFrameExtractor not found problem

    hello, I use your code,but it show this problem.

    Uncaught Error: Class 'GifFrameExtractor' not found in C:\xampp\htdocs\gites\a.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gites\a.php on line 5

    my GifFrameExtractor.php and gif image and my demo.php is in same folder.
    I dont know why it can not found?
    this is my code
    ` <?php include("GifFrameExtractor.php");

     $gifFilePath = 'a_g.gif';
     if (GifFrameExtractor::isAnimatedGif($gifFilePath)) { // check this is an animated GIF
    
    $gfe = new GifFrameExtractor();
    $gfe->extract($gifFilePath);
    $totalDuration = $gfe->getTotalDuration();
     echo $totalDuration;
    // Do something with extracted frames ...
    }
    
    ?>`
    

    please help me ,thanks

    opened by cool3690 1
  • Memory leak/issue

    Memory leak/issue

    There appears to be an issue with memory usage in this script:

    Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 906 bytes) in gifframeextractor.php on line 149

    The gif being processed: http://yum.gifsicle.com/0ed0dc3.gif

    The gif is only about 2.5Mb so not sure why it is trying to allocate over 64MB.

    Thanks!

    opened by whydna 0
  • Is it possible to get loop count from GifFrameExtractor?

    Is it possible to get loop count from GifFrameExtractor?

    GifCreator has an option to specify the loop count of the created GIF; is it possible to obtain this value for a GIF that has been read by GifFrameExtractor?

    opened by jcogs-design 1
  • Image combining doesn't work properly, fixes described

    Image combining doesn't work properly, fixes described

    I downloaded this code because I wanted to be able to extract frames from GIFs, but I found a few problems applying it to some GIFs that used the "Leave" method for image disposal, where it would combine a frame with the previous frame using transparency.

    There were two problems --

    The first is that it copied the image to the composite sprite using the maximum height and width, like so, imagecopyresampled($sprite, $img, $this->frameSources[$i]["offset_left"], $this->frameSources[$i]["offset_top"], 0, 0, $this->gifMaxWidth, $this->gifMaxHeight, $this->gifMaxWidth, $this->gifMaxHeight);

    However, if the second image was smaller in dimensions than the first, this would effectively black out anything past the dimensions of the first. The compositing would need to be done like this instead, imagecopyresampled($sprite, $img, $this->frameSources[$i]["offset_left"], $this->frameSources[$i]["offset_top"], 0, 0, $this->frameSources[$i]["width"], $this->frameSources[$i]["height"], $this->frameSources[$i]["width"], $this->frameSources[$i]["height"]);

    Of course, that would only be for frames with the "Leave" method; otherwise it makes sense to black it out if the dimensions are kept to the maximum height and width. Though I'm not sure that's how animated GIFs work.

    Second, it used the current index to determine the disposal method, $this->frameSources[$i]['disposal_method']

    While the disposal method applies to the previous frame, so needs to be taken from the previous image data. So it needs to be addressed like this, $this->frameSources[$i-1]['disposal_method']

    The project obviously looks like it has a lot left unimplemented and it looks like @Sybio has been gone for a while, but I'm leaving this documented here for anyone that might find interest in it.

    opened by sickoftalking 1
  • getFramePositions() often zero when they should not be

    getFramePositions() often zero when they should not be

    For about 70% of the transparent GIFs I test, getFramePositions() is returning all zeros. These are transparent GIFs with lots of alpha padding. My understanding is when there is alpha pixel padding getFramePositions() should NOT return zero. Is this correct? Thanks.

    opened by FuzzyWobble 1
  • This GIF complete destroys php instance

    This GIF complete destroys php instance

    In GifFrameExtractor at line 220. Something inside here is causing the entire php instance to crap out, the only way out is to restart PHP. I tried to set a time out but even that doesn't work something is screwy in this file!

            while (!$this->checkByte(0x3b) && !$this->checkEOF()) {
    
                $this->getCommentData(1);
                $this->parseGraphicsExtension(2);
                $this->getFrameString(2);
                $this->getApplicationData();
            }
    

    q0sqvwl

    opened by newtonianb 1
  • Unable to unlink() the source file after extraction

    Unable to unlink() the source file after extraction

    @Sybio I was unable to delete the source file after extraction. I was getting permission error and that only occur when i use $gifExtract->extract($rawSource);

    So i found out it was a permission issue. So i tried

    @chmod($rawSource, 465);
    @unlink($rawSource);
    

    But what if the code is running on a shared host with chmod restriction. So I look into the GifFrameExtractor.php file and I found out the file wasn't closed after processing.

    So I made a slit tweak by adding $this->closeFile(); and it fix the problem.

    private function parseFramesInfo($filename)
        {
            $this->openFile($filename);
            $this->parseGifHeader();
            $this->parseGraphicsExtension(0);
            $this->getApplicationData();
            $this->getApplicationData();
            $this->getFrameString(0);
            $this->parseGraphicsExtension(1);
            $this->getCommentData();
            $this->getApplicationData();
            $this->getFrameString(1);
            
            while (!$this->checkByte(0x3b) && !$this->checkEOF()) {
                
                $this->getCommentData(1);
                $this->parseGraphicsExtension(2);
                $this->getFrameString(2);
                $this->getApplicationData();
            }
            $this->closeFile();
        }
    
    opened by SilasOlatayo 0
Owner
Clément Guillemain
Clément Guillemain
Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.

Image Optimizer This library is handy and very easy to use optimizer for image files. It uses optipng, pngquant, jpegoptim, svgo and few more librarie

Piotr Śliwa 879 Dec 30, 2022
Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.

NO LONGER MAINTAINED!!! Image Cache v. 1.0.0 Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file,

Erik Nielsen 455 Dec 30, 2022
The Gregwar\Image class purpose is to provide a simple object-oriented images handling and caching API

Gregwar's Image class The Gregwar\Image class purpose is to provide a simple object-oriented images handling and caching API. Installation With compos

Grégoire Passault 958 Dec 29, 2022
This is a class of php QR Code, This library helps you generate QR codes in a jiffy.

This is a class of php QR Code, This library helps you generate QR codes in a jiffy.

null 59 Oct 5, 2022
PHP Exif Library - library for reading and writing Exif headers in JPEG and TIFF files using PHP.

PEL: PHP Exif Library README file for PEL: PHP Exif Library. A library with support for reading and writing Exif headers in JPEG and TIFF images using

null 264 Dec 4, 2022
A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

Lychee A great looking and easy-to-use photo-management-system. Since the 1st of April 2018 this project has moved to it's own Organisation (https://g

Tobias Reich 6.2k Dec 31, 2022
A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

Lychee Organisation 2.3k Jan 1, 2023
The Tinify API allows you to compress and optimize WebP, JPEG and PNG images.

The Tinify API allows you to compress and optimize WebP, JPEG and PNG images. It is designed as a REST service. The client libraries in various languages make it very easy to interact with the Tinify API.

Devscast 10 May 13, 2022
A PHP GD + TwitterOAuth demo to dynamically generate Twitter header images and upload them via the API.

A PHP GD + TwitterOAuth demo to dynamically generate Twitter header images and upload them via the API. This enables you to build cool little tricks, like showing your latest followers or sponsors, latest content creted, a qrcode to something, a progress bar for some goal, and whathever you can think of.

Erika Heidi 172 Jan 5, 2023
Grabs the dominant color or a representative color palette from an image. Uses PHP and GD, Imagick or Gmagick.

Color Thief PHP A PHP class for grabbing the color palette from an image. Uses PHP and GD or Imagick libraries to make it happen. It's a PHP port of t

Kevin Subileau 610 Dec 28, 2022
PHP library to resize, scale and crop images.

PHP library to resize, scale and crop images.

Gumlet 1.1k Jan 3, 2023
PHP library to easily edit image with GD extension. Resize, crop, merge, draw, and many more options !

PHP Image Editor PHP library to easily edit image with GD extension. Resize, crop, merge, draw, and many more options ! ✨ Supporting ⭐ Star this repos

Franck Alary 17 Nov 13, 2022
image sharing site made in PHP just for fun and freetime

2bart image sharing site made in PHP just for fun and freetime To-do list: upload system [DONE] ✔️ views system [DONE] ✔️ image list system [DONE] ✔️

goom 1 Oct 22, 2021
Auto Image & file upload, resize and crop for Laravel eloquent model using Intervention image

Laravel ImageUp The qcod/laravel-imageup is a trait which gives you auto upload, resize and crop for image feature with tons of customization. Install

QCode.in 708 Dec 22, 2022
Plug n play avatar, turn name, email, and any other string into beautiful avatar (or gravatar), effortless.

laravolt/avatar Display unique avatar for any user based on their (initials) name. Preview Installation This package originally built for Laravel, but

Laravolt 1.7k Jan 3, 2023
Laragram is a simple instagram "clone" built with Laravel and Tailwind CSS

Laragram is a simple instagram "clone" built with Laravel and Tailwind CSS that gives to the users the ability to create or edit their own profiles (including profile image and description), upload images and share them among friends

null 0 Jul 24, 2021
⚡ Dynamically generated, customizable SVG that gives the appearance of typing and deleting text. Typing SVGs can be used as a bio on your Github profile readme or repository.

⚡ Dynamically generated, customizable SVG that gives the appearance of typing and deleting text. Typing SVGs can be used as a bio on your Github profile readme or repository.

Jonah Lawrence 2k Jan 9, 2023
A simple page view counter that store data as text and shows data as a PNG image

Image Counter A simple page view counter that store data as text and shows the counter as a PNG image.

Victor Ribeiro 10 Apr 19, 2022
An easy and elegant way to generate gravatars 🖼

pH2Gravatar The simplest Gravatar PHP package An easy and elegant way to generate gravatar profile photos from email addresses ?? ⛏ Requirements PHP 7

♚ PH⑦ de Soria™♛ 6 Jul 12, 2022