Decoder.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Decoder
  13. {
  14. public $GIF_buffer = [];
  15. public $GIF_arrays = [];
  16. public $GIF_delays = [];
  17. public $GIF_stream = "";
  18. public $GIF_string = "";
  19. public $GIF_bfseek = 0;
  20. public $GIF_screen = [];
  21. public $GIF_global = [];
  22. public $GIF_sorted;
  23. public $GIF_colorS;
  24. public $GIF_colorC;
  25. public $GIF_colorF;
  26. /*
  27. :::::::::::::::::::::::::::::::::::::::::::::::::::
  28. ::
  29. :: GIFDecoder ( $GIF_pointer )
  30. ::
  31. */
  32. public function __construct($GIF_pointer)
  33. {
  34. $this->GIF_stream = $GIF_pointer;
  35. $this->getByte(6); // GIF89a
  36. $this->getByte(7); // Logical Screen Descriptor
  37. $this->GIF_screen = $this->GIF_buffer;
  38. $this->GIF_colorF = $this->GIF_buffer[4] & 0x80 ? 1 : 0;
  39. $this->GIF_sorted = $this->GIF_buffer[4] & 0x08 ? 1 : 0;
  40. $this->GIF_colorC = $this->GIF_buffer[4] & 0x07;
  41. $this->GIF_colorS = 2 << $this->GIF_colorC;
  42. if (1 == $this->GIF_colorF) {
  43. $this->getByte(3 * $this->GIF_colorS);
  44. $this->GIF_global = $this->GIF_buffer;
  45. }
  46. for ($cycle = 1; $cycle;) {
  47. if ($this->getByte(1)) {
  48. switch ($this->GIF_buffer[0]) {
  49. case 0x21:
  50. $this->readExtensions();
  51. break;
  52. case 0x2C:
  53. $this->readDescriptor();
  54. break;
  55. case 0x3B:
  56. $cycle = 0;
  57. break;
  58. }
  59. } else {
  60. $cycle = 0;
  61. }
  62. }
  63. }
  64. /*
  65. :::::::::::::::::::::::::::::::::::::::::::::::::::
  66. ::
  67. :: GIFReadExtension ( )
  68. ::
  69. */
  70. public function readExtensions()
  71. {
  72. $this->getByte(1);
  73. for (; ;) {
  74. $this->getByte(1);
  75. if (($u = $this->GIF_buffer[0]) == 0x00) {
  76. break;
  77. }
  78. $this->getByte($u);
  79. /*
  80. * 07.05.2007.
  81. * Implemented a new line for a new function
  82. * to determine the originaly delays between
  83. * frames.
  84. *
  85. */
  86. if (4 == $u) {
  87. $this->GIF_delays[] = ($this->GIF_buffer[1] | $this->GIF_buffer[2] << 8);
  88. }
  89. }
  90. }
  91. /*
  92. :::::::::::::::::::::::::::::::::::::::::::::::::::
  93. ::
  94. :: GIFReadExtension ( )
  95. ::
  96. */
  97. public function readDescriptor()
  98. {
  99. $this->getByte(9);
  100. $GIF_screen = $this->GIF_buffer;
  101. $GIF_colorF = $this->GIF_buffer[8] & 0x80 ? 1 : 0;
  102. if ($GIF_colorF) {
  103. $GIF_code = $this->GIF_buffer[8] & 0x07;
  104. $GIF_sort = $this->GIF_buffer[8] & 0x20 ? 1 : 0;
  105. } else {
  106. $GIF_code = $this->GIF_colorC;
  107. $GIF_sort = $this->GIF_sorted;
  108. }
  109. $GIF_size = 2 << $GIF_code;
  110. $this->GIF_screen[4] &= 0x70;
  111. $this->GIF_screen[4] |= 0x80;
  112. $this->GIF_screen[4] |= $GIF_code;
  113. if ($GIF_sort) {
  114. $this->GIF_screen[4] |= 0x08;
  115. }
  116. $this->GIF_string = "GIF87a";
  117. $this->putByte($this->GIF_screen);
  118. if (1 == $GIF_colorF) {
  119. $this->getByte(3 * $GIF_size);
  120. $this->putByte($this->GIF_buffer);
  121. } else {
  122. $this->putByte($this->GIF_global);
  123. }
  124. $this->GIF_string .= chr(0x2C);
  125. $GIF_screen[8] &= 0x40;
  126. $this->putByte($GIF_screen);
  127. $this->getByte(1);
  128. $this->putByte($this->GIF_buffer);
  129. for (; ;) {
  130. $this->getByte(1);
  131. $this->putByte($this->GIF_buffer);
  132. if (($u = $this->GIF_buffer[0]) == 0x00) {
  133. break;
  134. }
  135. $this->getByte($u);
  136. $this->putByte($this->GIF_buffer);
  137. }
  138. $this->GIF_string .= chr(0x3B);
  139. /*
  140. Add frames into $GIF_stream array...
  141. */
  142. $this->GIF_arrays[] = $this->GIF_string;
  143. }
  144. /*
  145. :::::::::::::::::::::::::::::::::::::::::::::::::::
  146. ::
  147. :: GIFGetByte ( $len )
  148. ::
  149. */
  150. public function getByte($len)
  151. {
  152. $this->GIF_buffer = [];
  153. for ($i = 0; $i < $len; $i++) {
  154. if ($this->GIF_bfseek > strlen($this->GIF_stream)) {
  155. return 0;
  156. }
  157. $this->GIF_buffer[] = ord($this->GIF_stream{$this->GIF_bfseek++});
  158. }
  159. return 1;
  160. }
  161. /*
  162. :::::::::::::::::::::::::::::::::::::::::::::::::::
  163. ::
  164. :: GIFPutByte ( $bytes )
  165. ::
  166. */
  167. public function putByte($bytes)
  168. {
  169. for ($i = 0; $i < count($bytes); $i++) {
  170. $this->GIF_string .= chr($bytes[$i]);
  171. }
  172. }
  173. /*
  174. :::::::::::::::::::::::::::::::::::::::::::::::::::
  175. ::
  176. :: PUBLIC FUNCTIONS
  177. ::
  178. ::
  179. :: GIFGetFrames ( )
  180. ::
  181. */
  182. public function getFrames()
  183. {
  184. return ($this->GIF_arrays);
  185. }
  186. /*
  187. :::::::::::::::::::::::::::::::::::::::::::::::::::
  188. ::
  189. :: GIFGetDelays ( )
  190. ::
  191. */
  192. public function getDelays()
  193. {
  194. return ($this->GIF_delays);
  195. }
  196. }