Decoder.php 5.1 KB

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