/GifMagic/Encoder.php
PHP | 258 lines | 168 code | 48 blank | 42 comment | 37 complexity | 076374f8d9b3410f59aea6ca3df3c09f MD5 | raw file
- <?php
- /*
- * Copyright (c) 2011, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- *
- * based on GIFEncoder Version 3.0 by Lรกszlรณ Zsidi
- * http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
- * http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html
- * http://www.gifs.hu
- */
- /**
- * @author robert.koppsieker@webvariants.de
- */
- class GifMagic_Encoder {
- private $result = 'GIF89a'; // GIF header 6 bytes
- private $buffers = array();
- private $OFS = array();
- private $SIG = 0;
- private $LOP = 0;
- private $DIS = 2;
- private $COL = -1;
- private $IMG = -1;
- private $ERR = array(
- 'ERR01' => 'Source is not a GIF image!',
- 'ERR02' => 'Unintelligible flag',
- 'ERR03' => 'Does not make animation from animated GIF source',
- );
- public function __construct(array $sources, array $result_dly, $loop, $disposables,
- $red, $green, $blue, $offsets, $sourceMode) {
- $this->LOP = $loop === false ? false : (($loop > -1) ? $loop : 0);
- $this->COL = ($red > -1 && $green > -1 && $blue > -1) ?
- ($red | ($green << 8) | ($blue << 16)) : -1;
- $sourceMode = strtolower($sourceMode);
- if (!in_array($sourceMode, array('url', 'bin'))) {
- throw new GifMagic_Exception($this->ERR['ERR02']." ($sourceMode)", 2);
- }
- for ($i = 0; $i < count ($sources); $i++) {
- if ($sourceMode === 'url') {
- $this->buffers[] = file_get_contents($sources[$i]);
- }
- else {
- $this->buffers[] = $sources [$i];
- }
- if (strlen($this->buffers[$i]) < 6) {
- throw new GifMagic_Exception('Invalid file given (file smaller than GIF signature).');
- }
- $sig = substr($this->buffers[$i], 0, 6);
- if ($sig !== 'GIF87a' && $sig !== 'GIF89a') {
- throw new GifMagic_Exception($this->ERR['ERR01'], 1);
- }
- for ($j = $this->getLocalsStrLength($i), $k = true; $k; $j++) {
- switch ($this->buffers[$i][$j]) {
- case '!':
- if ((substr($this->buffers[$i], ($j + 3), 8)) === 'NETSCAPE') {
- throw new GifMagic_Exception(sprintf('%s (%d source)', $this->ERR['ERR03'], $i+1), 3);
- }
- break;
- case ';':
- $k = false;
- break;
- }
- }
- }
- if (!is_array($offsets)) {
- $offsets = array();
- }
- for ($i = 0; $i < count($this->buffers); $i++) {
- if (!isset($disposables[$i])) $disposables[$i] = $this->DIS;
- else $disposables[$i] = ($disposables[$i] > -1) ? (($disposables[$i] < 3) ? $disposables[$i] : 3) : 2;
- if (!isset($offsets[$i])) $offsets[$i] = 0;
- }
- if (count($offsets) > 1) {
- $this->SIG = 1;
- $this->OFS = $offsets;
- }
- $this->addHeader();
- for ($i = 0; $i < count($this->buffers); $i++) {
- $this->addFrames($i, $result_dly [$i], $disposables[$i]);
- }
- $this->addFooter();
- }
- private function addHeader() {
- $cmap = 0;
- if (ord($this->buffers[0][10]) & 0x80) {
- $cmap = $this->getColorTableLength(0);
- $this->result .= substr($this->buffers[0], 6, 7);
- $this->result .= substr($this->buffers[0], 13, $cmap);
- if ($this->LOP !== false) {
- $this->result .= "!\377\13NETSCAPE2.0\3\1".$this->intToWord($this->LOP)."\0";
- }
- }
- }
- /**
- *
- * @param int $delay delay in seconds to next image
- * @param int $disposal disposal method type (0-7)
- * @param int $transpColorIndex color index for transparency
- * @return string in hex code
- */
- private function getGraphicalControlExtension($delay, $disposal, $transpColorIndex = null) {
- $transpFlag = 0;
- if (!is_int($transpColorIndex) || $transpColorIndex < 0) $transpColorIndex = 0;
- else $transpFlag = 1;
- return "\x21" // extension introducer (always 21)
- . "\xF9" // graphic control label (F9 means graphic control extension)
- . "\x04" // block size (fixed value 4)
- . chr( // packed fields
- ($disposal << 2) // disposal method
- + $transpFlag // transparency flag
- )
- . chr(($delay >> 0) & 0xFF) // delay time
- . chr(($delay >> 8) & 0xFF) // delay time
- . chr($transpColorIndex) // transparent color index
- . "\x0"; // block terminator (always zero)
- }
- private function getColorLength($imageNumber) {
- return 2 << (ord($this->buffers[$imageNumber][10]) & 0x07);
- }
- private function getColorTableLength($imageNumber) {
- return 3 * $this->getColorLength($imageNumber);
- }
- private function getLocalsStrLength($imageNumber) {
- return 13 + $this->getColorTableLength($imageNumber);
- }
- private function getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp, $local=true) {
- if ($this->SIG == 1) {
- $Locals_img[1] = chr( $this->OFS[$i][0] & 0xFF);
- $Locals_img[2] = chr(($this->OFS[$i][0] & 0xFF00) >> 8);
- $Locals_img[3] = chr( $this->OFS[$i][1] & 0xFF);
- $Locals_img[4] = chr(($this->OFS[$i][1] & 0xFF00) >> 8);
- }
- $byte = ord($Locals_img[9]);
- $byte |= 0x80;
- $byte &= 0xF8;
- if ($local) $byte |= (ord($this->buffers[$i][10]) & 0x07);
- else $byte |= (ord($this->buffers[0] [10]) & 0x07);
- $Locals_img[9] = chr($byte);
- return $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
- }
- private function addFrames($i, $d, $disposal) {
- $Locals_str = $this->getLocalsStrLength($i);
- $Locals_end = strlen($this->buffers[$i]) - $Locals_str - 1;
- $Locals_tmp = substr($this->buffers[$i], $Locals_str, $Locals_end);
- $Global_len = $this->getColorLength(0);
- $Locals_len = $this->getColorLength($i);
- $Global_rgb = substr($this->buffers[0], 13, $this->getColorTableLength(0));
- $Locals_rgb = substr($this->buffers[$i], 13, $this->getColorTableLength($i));
- // first frame
- $transpColorIndex = ord(substr($this->buffers[$i], $Locals_str+6, 1));
- $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, $transpColorIndex);
- // if ($this->COL > -1 && ord($this->buffers[$i][10]) & 0x80) {
- // print 'maximum color index: '.$this->getColorLength($i).'<br>';
- // for ($j = 0; $j < $this->getColorLength($i); $j++) {
- //
- // if (ord($Locals_rgb[3 * $j + 0]) == (($this->COL >> 16) & 0xFF)
- // && ord($Locals_rgb[3 * $j + 1]) == (($this->COL >> 8) & 0xFF)
- // && ord($Locals_rgb[3 * $j + 2]) == (($this->COL >> 0) & 0xFF)) {
- //
- // $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, $j);
- // break;
- // }
- // }
- // // hack to get index 1 transparent
- // if ($i > 0) $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, 1);
- // }
- switch ($Locals_tmp[0]) {
- case '!':
- $Locals_img = substr($Locals_tmp, 8, 10);
- $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
- break;
- case ',':
- $Locals_img = substr($Locals_tmp, 0, 10);
- $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
- break;
- }
- if (ord($this->buffers[$i][10]) & 0x80 && $this->IMG > -1) {
- // global and local color table have same length
- if ($Global_len == $Locals_len) {
- // global and local color table are equal
- if ($Global_rgb === $Locals_rgb) {
- $this->result .= $Locals_ext.$Locals_img.$Locals_tmp;
- }
- else {
- $this->result .= $this->getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp, false);
- }
- }
- // special local color table
- else {
- $this->result .= $this->getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp);
- }
- }
- else {
- $this->result .= $Locals_ext.$Locals_img.$Locals_tmp;
- }
- $this->IMG = 1;
- }
- private function addFooter() {
- $this->result .= ';';
- }
- private function intToWord($int) {
- return chr($int & 0xFF).chr(($int >> 8) & 0xFF);
- }
- public function getAnimation() {
- return $this->result;
- }
- }