PHP generated GIF animation implementation code

Source: Internet
Author: User
Tags unpack
  1. /**
  2. * Generate code for GIF animations
  3. * Edit bbs.it-home.org
  4. */
  5. Class Gifmerge {
  6. var $ver = ' 1.1 ';
  7. var $dly = 50;
  8. var $mod = ' C_file ';
  9. var $first = true;
  10. var $use _loop = false;
  11. var $transparent = false;
  12. var $use _global_in = false;
  13. var $x = 0;
  14. var $y = 0;
  15. var $ch = 0;
  16. var $fin = 0;
  17. var $fout = ';
  18. var $loop = 0;
  19. var $delay = 0;
  20. var $width = 0;
  21. var $height = 0;
  22. var $trans 1 = 255;
  23. var $trans 2 = 255;
  24. var $trans 3 = 255;
  25. var $disposal = 2;
  26. var $out _color_table_size = 0;
  27. var $local _color_table_flag = 0;
  28. var $global _color_table_size = 0;
  29. var $out _color_table_sizecode = 0;
  30. var $global _color_table_sizecode= 0;
  31. var $gif = Array (0x47, 0x49, 0x46);
  32. var $buffer = array ();
  33. var $local _in = Array ();
  34. var $global _in = Array ();
  35. var $global _out = Array ();
  36. var $logical _screen_descriptor = Array ();
  37. function Gifmerge ($images, $t 1, $t 2, $t 3, $loop, $DL, $xpos, $ypos, $model) {
  38. if ($model) {
  39. $this->mod = $model;
  40. }
  41. if ($loop >-1) {
  42. $this->loop = Floor ($loop-1);
  43. $this->use_loop = true;
  44. }
  45. if ($t 1 > 1 && $t 2 > 1 && $t 3 >-1) {
  46. $this->trans1 = $t 1;
  47. $this->trans2 = $t 2;
  48. $this->trans3 = $t 3;
  49. $this->transparent = true;
  50. }
  51. for ($i = 0; $i < count ($images); $i + +) {
  52. $DL [$i]? $this->delay = $dl [$i]: $this->delay = $this->dly;
  53. $xpos [$i]? $this->x = $xpos [$i]: $this->x = 0;
  54. $ypos [$i]? $this->y = $ypos [$i]: $this->y = 0;
  55. $this->start_gifmerge_process ($images [$i]);
  56. }
  57. $this->fout. = "/x3b";
  58. }
  59. function Start_gifmerge_process ($fp) {
  60. if ($this->mod = = ' C_file ') {
  61. if (! $this->fin = fopen ($fp, ' RB ')) {
  62. Return
  63. }
  64. } elseif ($this->mod = = ' C_memory ') {
  65. $this->ch = 0;
  66. $this->fin = $fp;
  67. }
  68. $this->getbytes (6);
  69. if (! $this->arrcmp ($this->buffer, $this->gif, 3)) {
  70. Return
  71. }
  72. $this->getbytes (7);
  73. if ($this->first) $this->logical_screen_descriptor = $this->buffer;
  74. $this->global_color_table_sizecode = $this->buffer[4] & 0x07;
  75. $this->global_color_table_size = 2 << $this->global_color_table_sizecode;
  76. if ($this->buffer[4] & 0x80) {
  77. $this->getbytes ((3 * $this->global_color_table_size));
  78. for ($i = 0; $i < ((3 * $this->global_color_table_size); $i + +) {
  79. $this->global_in[$i] = $this->buffer[$i];
  80. }
  81. if ($this->out_color_table_size = = 0) {
  82. $this->out_color_table_size = $this->global_color_table_size;
  83. $out _color_table_sizecode = $this->global_color_table_sizecode;
  84. $this->global_out = $this->global_in;
  85. }
  86. if ($this->global_color_table_size! = $this->out_color_table_size | | $this->arrcmp ($this->global_out, $ This->global_in, (3 * $this->global_color_table_size))) {
  87. $this->use_global_in = true;
  88. }
  89. }
  90. for ($loop = true; $loop;) {
  91. $this->getbytes (1);
  92. Switch ($this->buffer[0]) {
  93. Case 0X21:
  94. $this->read_extension ();
  95. Break
  96. Case 0X2C:
  97. $this->read_image_descriptor ();
  98. Break
  99. Case 0X3B:
  100. $loop = false;
  101. Break
  102. Default
  103. $loop = false;
  104. }
  105. }
  106. if ($this->mod = = ' C_file ') {
  107. Fclose ($this->fin);
  108. }
  109. }
  110. function Read_image_descriptor () {
  111. $this->getbytes (9);
  112. $head = $this->buffer;
  113. $this->local_color_table_flag = ($this->buffer[8] & 0x80)? True:false;
  114. if ($this->local_color_table_flag) {
  115. $sizecode = $this->buffer[8] & 0x07;
  116. $size = 2 << $sizecode;
  117. $this->getbytes (3 * $size);
  118. for ($i = 0; $i < (3 * $size); $i + +) {
  119. $this->local_in[$i] = $this->buffer[$i];
  120. }
  121. if ($this->out_color_table_size = = 0) {
  122. $this->out_color_table_size = $size;
  123. $out _color_table_sizecode = $sizecode;
  124. for ($i = 0; $i < (3 * $size); $i + +)
  125. {
  126. $this->global_out[$i] = $this->local_in[$i];
  127. }
  128. }
  129. }
  130. if ($this->first) {
  131. $this->first = false;
  132. $this->fout. = "/x47/x49/x46/x38/x39/x61";
  133. if ($this->width && $this->height) {
  134. $this->logical_screen_descriptor[0] = $this->width & 0xFF;
  135. $this->logical_screen_descriptor[1] = ($this->width & 0xff00) >> 8;
  136. $this->logical_screen_descriptor[2] = $this->height & 0xFF;
  137. $this->logical_screen_descriptor[3] = ($this->height & 0xff00) >> 8;
  138. }
  139. $this->logical_screen_descriptor[4] |= 0x80;
  140. $this->logical_screen_descriptor[5] &= 0xF0;
  141. $this->logical_screen_descriptor[6] |= $this->out_color_table_sizecode;
  142. $this->putbytes ($this->logical_screen_descriptor, 7);
  143. $this->putbytes ($this->global_out, ($this->out_color_table_size * 3));
  144. if ($this->use_loop) {
  145. $ns [0] = 0x21;
  146. $ns [1] = 0xFF;
  147. $ns [2] = 0x0B;
  148. $ns [3] = 0x4e;
  149. $ns [4] = 0x45;
  150. $ns [5] = 0x54;
  151. $ns [6] = 0x53;
  152. $ns [7] = 0x43;
  153. $ns [8] = 0x41;
  154. $ns [9] = 0x50;
  155. $ns [Ten] = 0x45;
  156. $ns [One] = 0x32;
  157. $ns [n] = 0x2e;
  158. $ns [] = 0x30;
  159. $ns [] = 0x03;
  160. $ns [+] = 0x01;
  161. $ns [+] = $this->loop & 255;
  162. $ns [] = $this->loop >> 8;
  163. $ns [] = 0x00;
  164. $this->putbytes ($ns, 19);
  165. }
  166. }
  167. if ($this->use_global_in) {
  168. $outtable = $this->global_in;
  169. $outsize = $this->global_color_table_size;
  170. $outsizecode = $this->global_color_table_sizecode;
  171. } else {
  172. $outtable = $this->global_out;
  173. $outsize = $this->out_color_table_size;
  174. }
  175. if ($this->local_color_table_flag) {
  176. if ($size = = $this->out_color_table_size &&! $this->arrcmp ($this->local_in, $this->global_out, $ Size)) {
  177. $outtable = $global _out;
  178. $outsize = $this->out_color_table_size;
  179. } else {
  180. $outtable = $this->local_in;
  181. $outsize = $size;
  182. $outsizecode = $sizecode;
  183. }
  184. }
  185. $use _trans = false;
  186. if ($this->transparent) {
  187. for ($i = 0; $i < $outsize; $i + +) {
  188. if ($outtable [3 * $i] = = $this->trans1 && $outtable [3 * $i + 1] = = $this->trans2 && $outtable [3 * $ i + 2] = = $this->trans3) {
  189. Break
  190. }
  191. }
  192. if ($i < $outsize) {
  193. $transindex = $i;
  194. $use _trans = true;
  195. }
  196. }
  197. if ($this->delay | | $use _trans) {
  198. $this->buffer[0] = 0x21;
  199. $this->buffer[1] = 0xf9;
  200. $this->buffer[2] = 0x04;
  201. $this->buffer[3] = ($this->disposal << 2) + ($use _trans? 1:0);
  202. $this->buffer[4] = $this->delay & 0xff;
  203. $this->buffer[5] = ($this->delay & 0xff00) >> 8;
  204. $this->buffer[6] = $use _trans? $transindex: 0;
  205. $this->buffer[7] = 0x00;
  206. $this->putbytes ($this->buffer,8);
  207. }
  208. $this->buffer[0] = 0x2c;
  209. $this->putbytes ($this->buffer,1);
  210. $head [0] = $this->x & 0xff;
  211. $head [1] = ($this->x & 0xff00) >> 8;
  212. $head [2] = $this->y & 0xff;
  213. $head [3] = ($this->y & 0xff00) >> 8;
  214. $head [8] &= 0x40;
  215. if ($outtable! = $this->global_out) {
  216. $head [8] |= 0x80;
  217. $head [8] |= $outsizecode;
  218. }
  219. $this->putbytes ($head, 9);
  220. if ($outtable! = $this->global_out) {
  221. $this->putbytes ($outtable, (3 * $outsize));
  222. }
  223. $this->getbytes (1);
  224. $this->putbytes ($this->buffer,1);
  225. for (;;) {
  226. $this->getbytes (1);
  227. $this->putbytes ($this->buffer,1);
  228. if ($u = $this->buffer[0]) = = 0) {
  229. Break
  230. }
  231. $this->getbytes ($u);
  232. $this->putbytes ($this->buffer, $u);
  233. }
  234. }
  235. function Read_extension () {
  236. $this->getbytes (1);
  237. Switch ($this->buffer[0]) {
  238. Case 0XF9:
  239. $this->getbytes (6);
  240. Break
  241. Case 0XFE:
  242. for (;;) {
  243. $this->getbytes (1);
  244. if ($u = $this->buffer[0]) = = 0) {
  245. Break
  246. }
  247. $this->getbytes ($u);
  248. }
  249. Break
  250. Case 0X01:
  251. $this->getbytes (13);
  252. for (;;) {
  253. $this->getbytes (0);
  254. if ($u = $this->buffer[0]) = = 0) {
  255. Break
  256. }
  257. $this->getbytes ($u);
  258. }
  259. Break
  260. Case 0xFF:
  261. $this->getbytes (9);
  262. $this->getbytes (3);
  263. for (;;) {
  264. $this->getbytes (1);
  265. if (! $this->buffer[0]) {
  266. Break
  267. }
  268. $this->getbytes ($this->buffer[0]);
  269. }
  270. Break
  271. Default
  272. for (;;) {
  273. $this->getbytes (1);
  274. if (! $this->buffer[0]) {
  275. Break
  276. }
  277. $this->getbytes ($this->buffer[0]);
  278. }
  279. }
  280. }
  281. function arrcmp ($b, $s, $l) {
  282. for ($i = 0; $i < $l; $i + +) {
  283. if ($s {$i}! = $b {$i}) {
  284. return false;
  285. }
  286. }
  287. return true;
  288. }
  289. function GetBytes ($l) {
  290. for ($i = 0; $i < $l; $i + +) {
  291. if ($this->mod = = ' C_file ') {
  292. $bin = Unpack (' c* ', fread ($this->fin, 1));
  293. $this->buffer[$i] = $bin [1];
  294. } elseif ($this->mod = = ' C_memory ') {
  295. $bin = Unpack (' c* ', substr ($this->fin, $this->ch, 1));
  296. $this->buffer[$i] = $bin [1];
  297. $this->ch++;
  298. }
  299. }
  300. return $this->buffer;
  301. }
  302. function putbytes ($s, $l) {
  303. for ($i = 0; $i < $l; $i + +) {
  304. $this->fout. = Pack (' c* ', $s [$i]);
  305. }
  306. }
  307. function Getanimation () {
  308. return $this->fout;
  309. }
  310. }
  311. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.