/** * Resize the gif by a ratio. The height and width will be scaled in proportion, maintaining the aspect ratio. * Example: A ratio of 0.5 will half the size, a ratio of 2 will double it. * * @param string $srcFile * @param string $dstFile the destination file. This will get overwritten if it already exists. * @param float $ratio * * @throws Exception */ public static function ResizeByRatio($srcFile, $dstFile, $ratio) { $fh = new FileHandler(); $fhW = new FileHandler(); try { $fh->openFile($srcFile); } catch (Exception $e) { throw $e; } $header = new Header($fh); if ($header->signature !== "GIF" && $header->version !== "87a" && $header->version !== "89a") { $fh->closeFile(); throw new Exception("Not a gif file."); } try { if (is_file($dstFile)) { unlink($dstFile); } $fhW->openFile($dstFile, true); } catch (Exception $e) { throw $e; } $fhW->writeData("GIF89a"); $lsd = new LogicalScreenDescriptor($fh); $lsd->resize($ratio); $lsd->encodeToFile($fhW); self::processGifStream($fh, $fhW, $ratio, $lsd); $fh->closeFile(); $fhW->closeFile(); }
/** * @param FileHandler $fh */ public function encodeToFile($fh) { $fh->writeData($this->encode()); }
/** * @param string $dstPath * @param int $frameCount * @param LogicalScreenDescriptor $lsd * @param AbstractExtensionBlock $aeb * * @throws Exception */ private static function writeFrame($dstPath, &$frameCount, $lsd, $aeb) { $frameCount++; if ($dstPath !== null) { $dstFileName = $dstPath . str_pad(strval($frameCount), 3, "0", STR_PAD_LEFT) . ".gif"; $fhW = new FileHandler(); $fhW->openFile($dstFileName, true); $fhW->writeData("GIF89a"); $lsd->encodeToFile($fhW); $aeb->encodeToFile($fhW); $fhW->writeData(";"); $fhW->closeFile(); } }
/** * @param $ratio * @param LogicalScreenDescriptor $lsd * * @throws Exception */ public function resize($ratio, $lsd) { $scrLp = $this->screenLeftPos; $scrTp = $this->screenTopPos; $tFileS = tempnam("BewareOfGeeksBearingGifs", "grS"); $tFileD = tempnam("BewareOfGeeksBearingGifs", "grD"); $fhT = new FileHandler(); $fhT->openFile($tFileS, true); $fhT->writeData($this->generateGif($lsd)); $fhT->closeFile(); ImageHandler::resizeGif($tFileS, $tFileD, $ratio, true); $fhT->openFile($tFileD, false); new Header($fhT); $nLsd = new LogicalScreenDescriptor($fhT); $nId = self::getFirstImageDescriptor($fhT, $this->parentGCE); $fhT->closeFile(); unset($tFileD); unset($tFileS); $this->screenLeftPos = (int) round($scrLp * $ratio); $this->screenTopPos = (int) round($scrTp * $ratio); $this->screenWidth = $nId->screenWidth; $this->screenHeight = $nId->screenHeight; $this->reserved = $nId->reserved; $this->interlaceFlag = $nId->interlaceFlag; $this->sortFlag = $nLsd->sortFlag; $this->colorTableFlag = $nLsd->colorTableFlag; $this->colorTableSize = $nLsd->colorTableSize; $this->colorTable = $nLsd->colorTable; $this->lzwMinCodeSize = $nId->lzwMinCodeSize; $this->dataSubBlocks = $nId->dataSubBlocks; }