Encoder.php 7.2 KB

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