Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix gifs with smaller frames #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/GifFrameExtractor/GifFrameExtractor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace GifFrameExtractor;
namespace GifFrameExtractor;

/**
* Extract the frames (and their duration) of a GIF
Expand Down Expand Up @@ -122,7 +122,7 @@ class GifFrameExtractor
public function extract($filename, $originalFrames = false)
{
if (!self::isAnimatedGif($filename)) {
throw new \Exception('The GIF image you are trying to explode is not animated !');
throw new \Exception('The GIF image you are trying to explode is not animated !');
}

$this->reset();
Expand All @@ -141,30 +141,32 @@ public function extract($filename, $originalFrames = false)
if ($i > 0) {

$prevImg = $this->frames[$i - 1]['image'];
$sprite = imagecreate($this->gifMaxWidth, $this->gifMaxHeight);
imagesavealpha($sprite, true);

} else {
$transparent = imagecolortransparent($prevImg);

$prevImg = $img;
}

$sprite = imagecreate($this->gifMaxWidth, $this->gifMaxHeight);
imagesavealpha($sprite, true);

$transparent = imagecolortransparent($prevImg);

if ($transparent > -1 && imagecolorstotal($prevImg) > $transparent) {
if ($transparent > -1 && imagecolorstotal($prevImg) > $transparent) {

$actualTrans = imagecolorsforindex($prevImg, $transparent);
imagecolortransparent($sprite, imagecolorallocate($sprite, $actualTrans['red'], $actualTrans['green'], $actualTrans['blue']));
}

$actualTrans = imagecolorsforindex($prevImg, $transparent);
imagecolortransparent($sprite, imagecolorallocate($sprite, $actualTrans['red'], $actualTrans['green'], $actualTrans['blue']));
}

if ((int) $this->frameSources[$i]['disposal_method'] == 1 && $i > 0) {
if ((int) $this->frameSources[$i]['disposal_method'] == 1 && $i > 0) {

imagecopy($sprite, $prevImg, 0, 0, 0, 0, $this->gifMaxWidth, $this->gifMaxHeight);
}
$w = imagesx($img);
$h = imagesy($img);
imagecopyresampled($sprite, $img, $this->frameSources[$i]["offset_left"], $this->frameSources[$i]["offset_top"], 0, 0, $w, $h, $w, $h);
$img = $sprite;

} else {

imagecopy($sprite, $prevImg, 0, 0, 0, 0, $this->gifMaxWidth, $this->gifMaxHeight);
$prevImg = $img;
}

imagecopyresampled($sprite, $img, $this->frameSources[$i]["offset_left"], $this->frameSources[$i]["offset_top"], 0, 0, $this->gifMaxWidth, $this->gifMaxHeight, $this->gifMaxWidth, $this->gifMaxHeight);
$img = $sprite;

}

$this->frameImages[$i] = $this->frames[$i]['image'] = $img;
Expand Down