protected function decode($name) { $action = "split"; $animation = __DIR__ . "/gifs/{$name}.gif"; $checksumPath = __DIR__ . "/gifs/{$name}.{$action}.json"; $dir = __DIR__ . "/output/{$action}/{$name}"; $frameCount = 0; $checksums = []; $hasChecksums = $this->loadChecksums($checksumPath, $checksums); $this->createDirOrClear($dir); $stream = new MemoryStream(); $stream->loadFromFile($animation); $decoder = new Decoder($stream); $decoder->decode(function (Frame $frame, $index) use(&$checksums, $hasChecksums, &$frameCount, $name, $dir) { $frameCount++; $paddedIndex = str_pad($index, 3, '0', STR_PAD_LEFT); $frame->getStream()->copyContentsToFile("{$dir}/frame{$paddedIndex}.gif"); $checksum = sha1($frame->getStream()->getContents()); if ($hasChecksums) { $this->assertEquals($checksum, $checksums[$index]); } $checksums[$index] = $checksum; }); file_put_contents("{$dir}/{$name}.{$action}.json", json_encode($checksums, JSON_PRETTY_PRINT)); }
protected function render($name) { $action = "render"; $animation = __DIR__ . "/gifs/{$name}.gif"; $checksumPath = __DIR__ . "/gifs/{$name}.{$action}.json"; $dir = __DIR__ . "/output/{$action}/{$name}"; $frameCount = 0; $checksums = []; $hasChecksums = $this->loadChecksums($checksumPath, $checksums); $this->createDirOrClear($dir); $stream = new MemoryStream(); $stream->loadFromFile($animation); $decoder = new Decoder($stream); $renderer = new Renderer($decoder); $renderer->start(function ($gd, $index) use(&$checksums, $hasChecksums, &$frameCount, $name, $dir) { $frameCount++; $paddedIndex = str_pad($index, 3, '0', STR_PAD_LEFT); $outputPath = "{$dir}/frame{$paddedIndex}.png"; imagepng($gd, $outputPath, 4, PNG_ALL_FILTERS); $checksum = sha1(file_get_contents($outputPath)); if ($hasChecksums) { $this->assertEquals($checksum, $checksums[$index]); } $checksums[$index] = $checksum; }); file_put_contents("{$dir}/{$name}.{$action}.json", json_encode($checksums, JSON_PRETTY_PRINT)); }
use GIFEndec\Color; use GIFEndec\Encoder; use GIFEndec\MemoryStream; use GIFEndec\Decoder; use GIFEndec\Frame; use GIFEndec\Renderer; $urlData = $_GET['url']; $uid = uniqid(); if (strpos($urlData, 'gif') === false) { die("Not a gif file!"); } $filenameIn = $urlData; $filenameOut = __DIR__ . '/img/' . $uid . '.gif'; $contentOrFalse = file_get_contents($filenameIn); $bytesOrFalse = file_put_contents($filenameOut, $contentOrFalse); $gifStream = new MemoryStream(); $gifStream->loadFromFile($filenameOut); $gifDecoder = new Decoder($gifStream); $gif = new Encoder(); $framearr = array(); $gifDecoder->decode(function (Frame $frame, $index) { global $framearr; $framearr[] = $frame; }); foreach (array_reverse($framearr) as $frame) { $gif->addFrame($frame); } $gif->addFooter(); $gif->getStream()->copyContentsToFile(__DIR__ . '/out/' . $uid . '.gif'); $returner = './out/' . $uid . '.gif'; $fp = fopen($returner, 'rb');