sybio / gifframeextractor Goto Github PK
View Code? Open in Web Editor NEWGifFrameExtractor is a PHP class that separates all the frames (and their duration) of an animated GIF
GifFrameExtractor is a PHP class that separates all the frames (and their duration) of an animated GIF
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.
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.
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.
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!
Any chance this class can have a way to save the manipulated frames back out into an animated GIF again?
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!
I'm not even sure this repo is maintained anymore. I sure hope it is, since it could be very useful.
Can anyone please explain why this GIF returns a getTotalDuration()
of 0
? GifFrameExtractor recognizes it has frames, 117 of them, but it returns a frameDuration of 0 for each, which adds up to 0. -_-
This isn't exactly helpful. I suspect it has something to do with 'Delay Time' vs. 'Unclamped Delay Time', as inspecting it with Preview on Mac shows the former to have a value of 0.1, and the latter to be 0.
Help, ideas?
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?
Fixed it... :)
This is a bug reported in ImageWorkshop library but concerning this repository.
Created by @Kekke88 :
Hello, I've been trying to add an image upon a GIF image using your libraries.
However, it turns the gif image into some weird blue color.
I have tried to debug but I can't find what's wrong, I tried using getResult with a background color but it still didn't help.I printed the frames as images and it seems that image #2 is the first image to get weird colors.
Here is the picture before:
http://i.imgur.com/M3bM7yP.gifand after:
http://i.imgur.com/lyMu2P0.gifRegards
Code sample :
<?php
include 'GifFrameExtractor.php';
include 'GifCreator.php';
include 'ImageWorkshop.php';
$gifPath = 'teac.gif';
if(GifFrameExtractor::isAnimatedGif($gifPath)) {
$gfe = new GifFrameExtractor();
$frames = $gfe->extract($gifPath);
$watermarkLayer = ImageWorkshop::initFromPath('wm.png');
$retouchedFrames = array();
//$it = 0;
foreach ($frames as $frame) {
$frameLayer = ImageWorkshop::initFromResourceVar($frame['image']);
$frameLayer->resizeInPixel($frameLayer->width, $frameLayer->height, false);
$frameLayer->addLayerOnTop($watermarkLayer, 20, 20, 'LB');
$image = $frameLayer->getResult();
// Creating all the GIF Images
//imagejpeg($image, 'test' . $it . '.jpg', 95);
$retouchedFrames[] = $frameLayer->getResult();
//$it++;
}
// Then we re-generate the GIF
$gc = new GifCreator();
$gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
// And now save it !
file_put_contents('newgif.gif', $gc->getGif());
}
?>
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
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();
}
@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(); }
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.