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 decode(Sample $sample) { $frameCount = 0; $checksums = []; $dir = $sample->emptyRawFramesDir(); $stream = new FileStream($sample->localPath()); $decoder = new Decoder($stream); $decoder->decode(function (FrameDecodedEvent $event) use(&$checksums, &$frameCount, $dir, $sample) { $frameCount++; $stream = $event->decodedFrame->getStream(); //$paddedIndex = str_pad($event->frameIndex, 3, '0', STR_PAD_LEFT); //$stream->copyContentsToFile("$dir/frame{$paddedIndex}.gif"); $checksum = sha1($stream->getContents()); $this->assertEquals($checksum, $sample->getFrameRawSHA1($event->frameIndex)); $checksums[$event->frameIndex] = $checksum; }); file_put_contents("{$dir}/_sha1.json", json_encode($checksums, JSON_PRETTY_PRINT)); }
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'); header("Content-Type: image/gif"); header("Content-Length: " . filesize($returner));