PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/A2/GIF/Encoder.php

https://bitbucket.org/mediastuttgart/image-resize
PHP | 284 lines | 181 code | 50 blank | 53 comment | 52 complexity | 784cedc1893b1d3661386f3e0699fdd9 MD5 | raw file
  1. <?php
  2. /*
  3. * based on GIFEncoder Version 3.0 by Lรกszlรณ Zsidi
  4. * http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html
  5. * http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html
  6. * http://www.gifs.hu
  7. *
  8. *
  9. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  10. *
  11. * This file is released under the terms of the MIT license. You can find the
  12. * complete text in the attached LICENSE file or online at:
  13. *
  14. * http://www.opensource.org/licenses/mit-license.php
  15. *
  16. */
  17. class A2_GIF_Encoder {
  18. var $GIF = "GIF89a"; // GIF header 6 bytes
  19. var $VER = "GIFEncoder V3.00"; // Encoder version
  20. var $BUF = array();
  21. var $OFS = array();
  22. var $SIG = 0;
  23. var $LOP = 0;
  24. var $DIS = 2;
  25. var $COL = -1;
  26. var $IMG = -1;
  27. var $ERR = array(
  28. 'ERR00'=>"Does support animated GIF images only!",
  29. 'ERR01'=>"Source is not a GIF image!",
  30. 'ERR02'=>"Unintelligible flag",
  31. 'ERR03'=>"Does not make animation from animated GIF source",
  32. );
  33. function __construct($GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
  34. $GIF_red, $GIF_grn, $GIF_blu,
  35. $GIF_ofs, $GIF_mod) {
  36. if (!is_array($GIF_src) && !is_array($GIF_dly)) {
  37. printf ("%s: %s", $this->VER, $this->ERR ['ERR00']);
  38. exit (0);
  39. }
  40. $this->LOP = $GIF_lop === false ? false : (($GIF_lop > -1) ? $GIF_lop : 0);
  41. $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ?
  42. ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
  43. #var_dump($GIF_red, $GIF_grn, $GIF_blu, $this->COL);die;
  44. for ($i = 0; $i < count ($GIF_src); $i++) {
  45. if (strToLower ($GIF_mod) == "url") {
  46. $this->BUF [] = fread (fopen ($GIF_src [$i], "rb"), filesize ($GIF_src [$i]));
  47. }
  48. else if (strToLower ($GIF_mod) == "bin") {
  49. $this->BUF [] = $GIF_src [$i];
  50. }
  51. else {
  52. printf ("%s: %s (%s)!", $this->VER, $this->ERR ['ERR02'], $GIF_mod);
  53. exit (0);
  54. }
  55. if (substr ($this->BUF [$i], 0, 6) != "GIF87a" && substr ($this->BUF [$i], 0, 6) != "GIF89a") {
  56. printf ("%s: %d %s", $this->VER, $i, $this->ERR ['ERR01']);
  57. exit (0);
  58. }
  59. for ($j = $this->getLocalsStrLength($i), $k = TRUE; $k; $j++) {
  60. switch ($this->BUF [$i] { $j }) {
  61. case "!":
  62. if ((substr ($this->BUF [$i], ($j + 3), 8)) == "NETSCAPE") {
  63. printf ("%s: %s (%s source)!", $this->VER, $this->ERR ['ERR03'], ($i + 1));
  64. exit (0);
  65. }
  66. break;
  67. case ";":
  68. $k = FALSE;
  69. break;
  70. }
  71. }
  72. }
  73. if (!is_array($GIF_ofs)) $GIF_ofs = array();
  74. for ($i = 0; $i < count ($this->BUF); $i++) {
  75. if (!isset($GIF_dis[$i])) $GIF_dis[$i] = $this->DIS;
  76. else $GIF_dis[$i] = ($GIF_dis[$i] > -1) ? (($GIF_dis[$i] < 3) ? $GIF_dis[$i] : 3) : 2;
  77. if (!isset($GIF_ofs[$i])) $GIF_ofs[$i] = 0;
  78. }
  79. if (is_array($GIF_ofs) && count ($GIF_ofs) > 1) {
  80. $this->SIG = 1;
  81. $this->OFS = $GIF_ofs;
  82. }
  83. $this->addHeader();
  84. for ($i = 0; $i < count($this->BUF); $i++) {
  85. $this->addFrames ($i, $GIF_dly [$i], $GIF_dis[$i]);
  86. }
  87. // die;
  88. $this->addFooter();
  89. }
  90. private function addHeader() {
  91. $cmap = 0;
  92. if (ord ($this->BUF[0][10]) & 0x80) {
  93. $cmap = $this->getColorTableLength(0);
  94. $this->GIF .= substr($this->BUF[0], 6, 7);
  95. $this->GIF .= substr($this->BUF[0], 13, $cmap);
  96. if ($this->LOP !== false) {
  97. $this->GIF .= "!\377\13NETSCAPE2.0\3\1".$this->intToWord($this->LOP)."\0";
  98. }
  99. }
  100. }
  101. /**
  102. *
  103. * @param int $delay delay in seconds to next image
  104. * @param int $disposal disposal method type (0-7)
  105. * @param int $transpColorIndex color index for transparency
  106. * @return string in hex code
  107. */
  108. private function getGraphicalControlExtension($delay, $disposal, $transpColorIndex = null) {
  109. $transpFlag = 0;
  110. if (!is_int($transpColorIndex) || $transpColorIndex < 0) $transpColorIndex = 0;
  111. else $transpFlag = 1;
  112. return "\x21" // extension introducer (always 21)
  113. . "\xF9" // graphic control label (F9 means graphic control extension)
  114. . "\x04" // block size (fixed value 4)
  115. . chr( // packed fields
  116. ($disposal << 2) // disposal method
  117. + $transpFlag // transparency flag
  118. )
  119. . chr(($delay >> 0) & 0xFF) // delay time
  120. . chr(($delay >> 8) & 0xFF) // delay time
  121. . chr($transpColorIndex) // transparent color index
  122. . "\x0"; // block terminator (always zero)
  123. }
  124. private function getColorLength($imageNumber) {
  125. return 2 << (ord($this->BUF[$imageNumber][10]) & 0x07);
  126. }
  127. private function getColorTableLength($imageNumber) {
  128. return 3 * $this->getColorLength($imageNumber);
  129. }
  130. private function getLocalsStrLength($imageNumber) {
  131. return 13 + $this->getColorTableLength($imageNumber);
  132. }
  133. private function getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp, $local=true) {
  134. if ($this->SIG == 1) {
  135. $Locals_img[1] = chr( $this->OFS[$i][0] & 0xFF);
  136. $Locals_img[2] = chr(($this->OFS[$i][0] & 0xFF00) >> 8);
  137. $Locals_img[3] = chr( $this->OFS[$i][1] & 0xFF);
  138. $Locals_img[4] = chr(($this->OFS[$i][1] & 0xFF00) >> 8);
  139. }
  140. $byte = ord($Locals_img[9]);
  141. $byte |= 0x80;
  142. $byte &= 0xF8;
  143. if ($local) $byte |= (ord($this->BUF[$i][10]) & 0x07);
  144. else $byte |= (ord($this->BUF[0] [10]) & 0x07);
  145. $Locals_img[9] = chr($byte);
  146. return $Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp;
  147. }
  148. private function addFrames($i, $d, $disposal) {
  149. $Locals_str = $this->getLocalsStrLength($i);
  150. $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;
  151. $Locals_tmp = substr($this->BUF[$i], $Locals_str, $Locals_end);
  152. $Global_len = $this->getColorLength(0);
  153. $Locals_len = $this->getColorLength($i);
  154. $Global_rgb = substr($this->BUF[0], 13, $this->getColorTableLength(0));
  155. $Locals_rgb = substr($this->BUF[$i], 13, $this->getColorTableLength($i));
  156. // first frame
  157. $transpColorIndex = ord(substr($this->BUF[$i], $Locals_str+6, 1));
  158. $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, $transpColorIndex);
  159. // if ($this->COL > -1 && ord($this->BUF[$i][10]) & 0x80) {
  160. // print 'maximum color index: '.$this->getColorLength($i).'<br>';
  161. // for ($j = 0; $j < $this->getColorLength($i); $j++) {
  162. //
  163. // if (ord($Locals_rgb[3 * $j + 0]) == (($this->COL >> 16) & 0xFF)
  164. // && ord($Locals_rgb[3 * $j + 1]) == (($this->COL >> 8) & 0xFF)
  165. // && ord($Locals_rgb[3 * $j + 2]) == (($this->COL >> 0) & 0xFF)) {
  166. //
  167. // $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, $j);
  168. // break;
  169. // }
  170. // }
  171. // // hack to get index 1 transparent
  172. // if ($i > 0) $Locals_ext = $this->getGraphicalControlExtension($d, $disposal, 1);
  173. // }
  174. switch ($Locals_tmp[0]) {
  175. case "!":
  176. $Locals_img = substr ($Locals_tmp, 8, 10);
  177. $Locals_tmp = substr ($Locals_tmp, 18, strlen ($Locals_tmp) - 18);
  178. break;
  179. case ",":
  180. $Locals_img = substr ($Locals_tmp, 0, 10);
  181. $Locals_tmp = substr ($Locals_tmp, 10, strlen ($Locals_tmp) - 10);
  182. break;
  183. }
  184. if (ord($this->BUF[$i][10]) & 0x80 && $this->IMG > -1) {
  185. // global and local color table have same length
  186. if ($Global_len == $Locals_len) {
  187. // global and local color table are equal
  188. if ($this->blockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
  189. $this->GIF .= $Locals_ext.$Locals_img.$Locals_tmp;
  190. }
  191. else {
  192. $this->GIF .= $this->getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp, false);
  193. }
  194. }
  195. // special local color table
  196. else {
  197. $this->GIF .= $this->getXYPadding($i, $Locals_ext, $Locals_img, $Locals_rgb, $Locals_tmp);
  198. }
  199. }
  200. else {
  201. $this->GIF .= $Locals_ext.$Locals_img.$Locals_tmp;
  202. }
  203. $this->IMG = 1;
  204. // print '<br />';
  205. // print '<br />';
  206. }
  207. private function addFooter() {
  208. $this->GIF .= ";";
  209. }
  210. /**
  211. * compare two color tables
  212. *
  213. * @param array $GlobalBlock
  214. * @param array $LocalBlock
  215. * @param int $Len
  216. * @return boolean true if tables match, else false
  217. */
  218. private function blockCompare($GlobalBlock, $LocalBlock, $Len) {
  219. for ($i = 0; $i < $Len; $i++) {
  220. if ($GlobalBlock[3 * $i + 0] != $LocalBlock[3 * $i + 0]
  221. || $GlobalBlock[3 * $i + 1] != $LocalBlock[3 * $i + 1]
  222. || $GlobalBlock[3 * $i + 2] != $LocalBlock[3 * $i + 2]) {
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. private function intToWord($int) {
  229. return chr($int & 0xFF).chr(($int >> 8) & 0xFF);
  230. }
  231. public function getAnimation() {
  232. return $this->GIF;
  233. }
  234. }