Home  >  Article  >  Backend Development  >  Implementation code for php to generate gif animation

Implementation code for php to generate gif animation

WBOY
WBOYOriginal
2016-07-25 08:59:071025browse
  1. /**
  2. * Code to generate gif animation
  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 $trans1 = 255;
  23. var $trans2 = 255;
  24. var $trans3 = 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, $t1, $t2, $t3, $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($t1 > -1 && $t2 > -1 && $t3 > -1) {
  46. $this->trans1 = $t1;
  47. $this->trans2 = $t2;
  48. $this->trans3 = $t3;
  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[10] = 0x45;
  156. $ns[11] = 0x32;
  157. $ns[12] = 0x2e;
  158. $ns[13] = 0x30;
  159. $ns[14] = 0x03;
  160. $ns[15] = 0x01;
  161. $ns[16] = $this->loop & 255;
  162. $ns[17] = $this->loop >> 8;
  163. $ns[18] = 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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn