From a5d476f67dd85ce06d39c2e125b17a961ae99910 Mon Sep 17 00:00:00 2001 From: Renato Formato Date: Wed, 3 Jun 2015 22:14:40 +0200 Subject: [PATCH] fix gifs with smaller frames for which a solid color background instead of a transparent one was added --- src/GifFrameExtractor/GifFrameExtractor.php | 42 +++++++++++---------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/GifFrameExtractor/GifFrameExtractor.php b/src/GifFrameExtractor/GifFrameExtractor.php index 3ecac89..f1386bb 100644 --- a/src/GifFrameExtractor/GifFrameExtractor.php +++ b/src/GifFrameExtractor/GifFrameExtractor.php @@ -1,6 +1,6 @@ reset(); @@ -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;