Encoder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace addons\sythumb\library\imgclass\image\gif;
  12. class Encoder
  13. {
  14. public $GIF = "GIF89a"; /* GIF header 6 bytes */
  15. public $VER = "GIFEncoder V2.05"; /* Encoder version */
  16. public $BUF = [];
  17. public $LOP = 0;
  18. public $DIS = 2;
  19. public $COL = -1;
  20. public $IMG = -1;
  21. public $ERR = [
  22. 'ERR00' => "Does not supported function for only one image!",
  23. 'ERR01' => "Source is not a GIF image!",
  24. 'ERR02' => "Unintelligible flag ",
  25. 'ERR03' => "Does not make animation from animated GIF source",
  26. ];
  27. /*
  28. :::::::::::::::::::::::::::::::::::::::::::::::::::
  29. ::
  30. :: GIFEncoder...
  31. ::
  32. */
  33. public function __construct(
  34. $GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
  35. $GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
  36. )
  37. {
  38. if (!is_array($GIF_src)) {
  39. printf("%s: %s", $this->VER, $this->ERR['ERR00']);
  40. exit(0);
  41. }
  42. $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
  43. $this->DIS = ($GIF_dis > -1) ? (($GIF_dis < 3) ? $GIF_dis : 3) : 2;
  44. $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ?
  45. ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
  46. for ($i = 0; $i < count($GIF_src); $i++) {
  47. if (strtolower($GIF_mod) == "url") {
  48. $this->BUF[] = fread(fopen($GIF_src[$i], "rb"), filesize($GIF_src[$i]));
  49. } else if (strtolower($GIF_mod) == "bin") {
  50. $this->BUF[] = $GIF_src[$i];
  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 = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07))), $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. $this->addHeader();
  74. for ($i = 0; $i < count($this->BUF); $i++) {
  75. $this->addFrames($i, $GIF_dly[$i]);
  76. }
  77. $this->addFooter();
  78. }
  79. /*
  80. :::::::::::::::::::::::::::::::::::::::::::::::::::
  81. ::
  82. :: GIFAddHeader...
  83. ::
  84. */
  85. public function addHeader()
  86. {
  87. if (ord($this->BUF[0]{10}) & 0x80) {
  88. $cmap = 3 * (2 << (ord($this->BUF[0]{10}) & 0x07));
  89. $this->GIF .= substr($this->BUF[0], 6, 7);
  90. $this->GIF .= substr($this->BUF[0], 13, $cmap);
  91. $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . $this->word($this->LOP) . "\0";
  92. }
  93. }
  94. /*
  95. :::::::::::::::::::::::::::::::::::::::::::::::::::
  96. ::
  97. :: GIFAddFrames...
  98. ::
  99. */
  100. public function addFrames($i, $d)
  101. {
  102. $Locals_img = '';
  103. $Locals_str = 13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07));
  104. $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;
  105. $Locals_tmp = substr($this->BUF[$i], $Locals_str, $Locals_end);
  106. $Global_len = 2 << (ord($this->BUF[0]{10}) & 0x07);
  107. $Locals_len = 2 << (ord($this->BUF[$i]{10}) & 0x07);
  108. $Global_rgb = substr($this->BUF[0], 13,
  109. 3 * (2 << (ord($this->BUF[0]{10}) & 0x07)));
  110. $Locals_rgb = substr($this->BUF[$i], 13,
  111. 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07)));
  112. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) .
  113. chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
  114. if ($this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80) {
  115. for ($j = 0; $j < (2 << (ord($this->BUF[$i]{10}) & 0x07)); $j++) {
  116. if (
  117. ord($Locals_rgb{3 * $j + 0}) == (($this->COL >> 16) & 0xFF) &&
  118. ord($Locals_rgb{3 * $j + 1}) == (($this->COL >> 8) & 0xFF) &&
  119. ord($Locals_rgb{3 * $j + 2}) == (($this->COL >> 0) & 0xFF)
  120. ) {
  121. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) .
  122. chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
  123. break;
  124. }
  125. }
  126. }
  127. switch ($Locals_tmp{0}) {
  128. case "!":
  129. /**
  130. * @var string $Locals_img ;
  131. */
  132. $Locals_img = substr($Locals_tmp, 8, 10);
  133. $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
  134. break;
  135. case ",":
  136. $Locals_img = substr($Locals_tmp, 0, 10);
  137. $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
  138. break;
  139. }
  140. if (ord($this->BUF[$i]{10}) & 0x80 && $this->IMG > -1) {
  141. if ($Global_len == $Locals_len) {
  142. if ($this->blockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
  143. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_tmp);
  144. } else {
  145. $byte = ord($Locals_img{9});
  146. $byte |= 0x80;
  147. $byte &= 0xF8;
  148. $byte |= (ord($this->BUF[0]{10}) & 0x07);
  149. $Locals_img{9} = chr($byte);
  150. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  151. }
  152. } else {
  153. $byte = ord($Locals_img{9});
  154. $byte |= 0x80;
  155. $byte &= 0xF8;
  156. $byte |= (ord($this->BUF[$i]{10}) & 0x07);
  157. $Locals_img{9} = chr($byte);
  158. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  159. }
  160. } else {
  161. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_tmp);
  162. }
  163. $this->IMG = 1;
  164. }
  165. /*
  166. :::::::::::::::::::::::::::::::::::::::::::::::::::
  167. ::
  168. :: GIFAddFooter...
  169. ::
  170. */
  171. public function addFooter()
  172. {
  173. $this->GIF .= ";";
  174. }
  175. /*
  176. :::::::::::::::::::::::::::::::::::::::::::::::::::
  177. ::
  178. :: GIFBlockCompare...
  179. ::
  180. */
  181. public function blockCompare($GlobalBlock, $LocalBlock, $Len)
  182. {
  183. for ($i = 0; $i < $Len; $i++) {
  184. if (
  185. $GlobalBlock{3 * $i + 0} != $LocalBlock{3 * $i + 0} ||
  186. $GlobalBlock{3 * $i + 1} != $LocalBlock{3 * $i + 1} ||
  187. $GlobalBlock{3 * $i + 2} != $LocalBlock{3 * $i + 2}
  188. ) {
  189. return (0);
  190. }
  191. }
  192. return (1);
  193. }
  194. /*
  195. :::::::::::::::::::::::::::::::::::::::::::::::::::
  196. ::
  197. :: GIFWord...
  198. ::
  199. */
  200. public function word($int)
  201. {
  202. return (chr($int & 0xFF) . chr(($int >> 8) & 0xFF));
  203. }
  204. /*
  205. :::::::::::::::::::::::::::::::::::::::::::::::::::
  206. ::
  207. :: GetAnimation...
  208. ::
  209. */
  210. public function getAnimation()
  211. {
  212. return ($this->GIF);
  213. }
  214. }