0

0

php生成gif动画的实现代码

php中文网

php中文网

发布时间:2016-07-25 08:59:07

|

1408人浏览过

|

来源于php中文网

原创

  1. /**
  2. * 生成gif动画的代码
  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
  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 global_color_table_sizecode;
  76. if($this->buffer[4] & 0x80) {
  77. $this->getbytes((3 * $this->global_color_table_size));
  78. for($i = 0; $i 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
  117. $this->getbytes(3 * $size);
  118. for($i = 0; $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
  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
  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
  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
  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
  283. if($s{$i} != $b{$i}) {
  284. return false;
  285. }
  286. }
  287. return true;
  288. }
  289. function getbytes($l) {
  290. for($i = 0; $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
  304. $this->fout .= pack('C*', $s[$i]);
  305. }
  306. }
  307. function getAnimation() {
  308. return $this->fout;
  309. }
  310. }
  311. ?>
复制代码


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

797

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

272

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

144

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

25

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

92

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

53

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

717

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

64

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

0基础快速上手自动化测试
0基础快速上手自动化测试

共8课时 | 0.4万人学习

前端基础进阶-移动Web
前端基础进阶-移动Web

共187课时 | 23.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号