GIF.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. // | GIF.class.php 2013-03-09
  12. // +----------------------------------------------------------------------
  13. namespace Think\Image\Driver;
  14. class GIF
  15. {
  16. /**
  17. * GIF帧列表
  18. * @var array
  19. */
  20. private $frames = array();
  21. /**
  22. * 每帧等待时间列表
  23. * @var array
  24. */
  25. private $delays = array();
  26. /**
  27. * 构造方法,用于解码GIF图片
  28. * @param string $src GIF图片数据
  29. * @param string $mod 图片数据类型
  30. */
  31. public function __construct($src = null, $mod = 'url')
  32. {
  33. if (!is_null($src)) {
  34. if ('url' == $mod && is_file($src)) {
  35. $src = file_get_contents($src);
  36. }
  37. /* 解码GIF图片 */
  38. try {
  39. $de = new GIFDecoder($src);
  40. $this->frames = $de->GIFGetFrames();
  41. $this->delays = $de->GIFGetDelays();
  42. } catch (\Exception $e) {
  43. E("解码GIF图片出错");
  44. }
  45. }
  46. }
  47. /**
  48. * 设置或获取当前帧的数据
  49. * @param string $stream 二进制数据流
  50. * @return boolean 获取到的数据
  51. */
  52. public function image($stream = null)
  53. {
  54. if (is_null($stream)) {
  55. $current = current($this->frames);
  56. return false === $current ? reset($this->frames) : $current;
  57. } else {
  58. $this->frames[key($this->frames)] = $stream;
  59. }
  60. }
  61. /**
  62. * 将当前帧移动到下一帧
  63. * @return string 当前帧数据
  64. */
  65. public function nextImage()
  66. {
  67. return next($this->frames);
  68. }
  69. /**
  70. * 编码并保存当前GIF图片
  71. * @param string $gifname 图片名称
  72. */
  73. public function save($gifname)
  74. {
  75. $gif = new GIFEncoder($this->frames, $this->delays, 0, 2, 0, 0, 0, 'bin');
  76. file_put_contents($gifname, $gif->GetAnimation());
  77. }
  78. }
  79. /*
  80. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  81. ::
  82. :: GIFEncoder Version 2.0 by László Zsidi, http://gifs.hu
  83. ::
  84. :: This class is a rewritten 'GifMerge.class.php' version.
  85. ::
  86. :: Modification:
  87. :: - Simplified and easy code,
  88. :: - Ultra fast encoding,
  89. :: - Built-in errors,
  90. :: - Stable working
  91. ::
  92. ::
  93. :: Updated at 2007. 02. 13. '00.05.AM'
  94. ::
  95. ::
  96. ::
  97. :: Try on-line GIFBuilder Form demo based on GIFEncoder.
  98. ::
  99. :: http://gifs.hu/phpclasses/demos/GifBuilder/
  100. ::
  101. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  102. */
  103. class GIFEncoder
  104. {
  105. private $GIF = "GIF89a"; /* GIF header 6 bytes */
  106. private $VER = "GIFEncoder V2.05"; /* Encoder version */
  107. private $BUF = array();
  108. private $LOP = 0;
  109. private $DIS = 2;
  110. private $COL = -1;
  111. private $IMG = -1;
  112. private $ERR = array(
  113. 'ERR00' => "Does not supported function for only one image!",
  114. 'ERR01' => "Source is not a GIF image!",
  115. 'ERR02' => "Unintelligible flag ",
  116. 'ERR03' => "Does not make animation from animated GIF source",
  117. );
  118. /*
  119. :::::::::::::::::::::::::::::::::::::::::::::::::::
  120. ::
  121. :: GIFEncoder...
  122. ::
  123. */
  124. public function __construct($GIF_src, $GIF_dly, $GIF_lop, $GIF_dis, $GIF_red, $GIF_grn, $GIF_blu, $GIF_mod)
  125. {
  126. if (!is_array($GIF_src) && !is_array($GIF_dly)) {
  127. printf("%s: %s", $this->VER, $this->ERR['ERR00']);
  128. exit(0);
  129. }
  130. $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
  131. $this->DIS = ($GIF_dis > -1) ? (($GIF_dis < 3) ? $GIF_dis : 3) : 2;
  132. $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ?
  133. ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
  134. for ($i = 0; $i < count($GIF_src); $i++) {
  135. if (strToLower($GIF_mod) == "url") {
  136. $this->BUF[] = fread(fopen($GIF_src[$i], "rb"), filesize($GIF_src[$i]));
  137. } else if (strToLower($GIF_mod) == "bin") {
  138. $this->BUF[] = $GIF_src[$i];
  139. } else {
  140. printf("%s: %s ( %s )!", $this->VER, $this->ERR['ERR02'], $GIF_mod);
  141. exit(0);
  142. }
  143. if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF[$i], 0, 6) != "GIF89a") {
  144. printf("%s: %d %s", $this->VER, $i, $this->ERR['ERR01']);
  145. exit(0);
  146. }
  147. for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07))), $k = true; $k; $j++) {
  148. switch ($this->BUF[$i]{ $j}) {
  149. case "!":
  150. if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE") {
  151. printf("%s: %s ( %s source )!", $this->VER, $this->ERR['ERR03'], ($i + 1));
  152. exit(0);
  153. }
  154. break;
  155. case ";":
  156. $k = false;
  157. break;
  158. }
  159. }
  160. }
  161. $this->GIFAddHeader();
  162. for ($i = 0; $i < count($this->BUF); $i++) {
  163. $this->GIFAddFrames($i, $GIF_dly[$i]);
  164. }
  165. $this->GIFAddFooter();
  166. }
  167. /*
  168. :::::::::::::::::::::::::::::::::::::::::::::::::::
  169. ::
  170. :: GIFAddHeader...
  171. ::
  172. */
  173. private function GIFAddHeader()
  174. {
  175. $cmap = 0;
  176. if (ord($this->BUF[0]{10}) & 0x80) {
  177. $cmap = 3 * (2 << (ord($this->BUF[0]{10}) & 0x07));
  178. $this->GIF .= substr($this->BUF[0], 6, 7);
  179. $this->GIF .= substr($this->BUF[0], 13, $cmap);
  180. $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . $this->GIFWord($this->LOP) . "\0";
  181. }
  182. }
  183. /*
  184. :::::::::::::::::::::::::::::::::::::::::::::::::::
  185. ::
  186. :: GIFAddFrames...
  187. ::
  188. */
  189. private function GIFAddFrames($i, $d)
  190. {
  191. $Locals_str = 13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07));
  192. $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;
  193. $Locals_tmp = substr($this->BUF[$i], $Locals_str, $Locals_end);
  194. $Global_len = 2 << (ord($this->BUF[0]{10}) & 0x07);
  195. $Locals_len = 2 << (ord($this->BUF[$i]{10}) & 0x07);
  196. $Global_rgb = substr($this->BUF[0], 13,
  197. 3 * (2 << (ord($this->BUF[0]{10}) & 0x07)));
  198. $Locals_rgb = substr($this->BUF[$i], 13,
  199. 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07)));
  200. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) .
  201. chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
  202. if ($this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80) {
  203. for ($j = 0; $j < (2 << (ord($this->BUF[$i]{10}) & 0x07)); $j++) {
  204. if (
  205. ord($Locals_rgb{3 * $j + 0}) == (($this->COL >> 16) & 0xFF) &&
  206. ord($Locals_rgb{3 * $j + 1}) == (($this->COL >> 8) & 0xFF) &&
  207. ord($Locals_rgb{3 * $j + 2}) == (($this->COL >> 0) & 0xFF)
  208. ) {
  209. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) .
  210. chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
  211. break;
  212. }
  213. }
  214. }
  215. switch ($Locals_tmp{0}) {
  216. case "!":
  217. $Locals_img = substr($Locals_tmp, 8, 10);
  218. $Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
  219. break;
  220. case ",":
  221. $Locals_img = substr($Locals_tmp, 0, 10);
  222. $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
  223. break;
  224. }
  225. if (ord($this->BUF[$i]{10}) & 0x80 && $this->IMG > -1) {
  226. if ($Global_len == $Locals_len) {
  227. if ($this->GIFBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
  228. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_tmp);
  229. } else {
  230. $byte = ord($Locals_img{9});
  231. $byte |= 0x80;
  232. $byte &= 0xF8;
  233. $byte |= (ord($this->BUF[0]{10}) & 0x07);
  234. $Locals_img{9} = chr($byte);
  235. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  236. }
  237. } else {
  238. $byte = ord($Locals_img{9});
  239. $byte |= 0x80;
  240. $byte &= 0xF8;
  241. $byte |= (ord($this->BUF[$i]{10}) & 0x07);
  242. $Locals_img{9} = chr($byte);
  243. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  244. }
  245. } else {
  246. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_tmp);
  247. }
  248. $this->IMG = 1;
  249. }
  250. /*
  251. :::::::::::::::::::::::::::::::::::::::::::::::::::
  252. ::
  253. :: GIFAddFooter...
  254. ::
  255. */
  256. private function GIFAddFooter()
  257. {
  258. $this->GIF .= ";";
  259. }
  260. /*
  261. :::::::::::::::::::::::::::::::::::::::::::::::::::
  262. ::
  263. :: GIFBlockCompare...
  264. ::
  265. */
  266. private function GIFBlockCompare($GlobalBlock, $LocalBlock, $Len)
  267. {
  268. for ($i = 0; $i < $Len; $i++) {
  269. if (
  270. $GlobalBlock{3 * $i + 0} != $LocalBlock{3 * $i + 0} ||
  271. $GlobalBlock{3 * $i + 1} != $LocalBlock{3 * $i + 1} ||
  272. $GlobalBlock{3 * $i + 2} != $LocalBlock{3 * $i + 2}
  273. ) {
  274. return (0);
  275. }
  276. }
  277. return (1);
  278. }
  279. /*
  280. :::::::::::::::::::::::::::::::::::::::::::::::::::
  281. ::
  282. :: GIFWord...
  283. ::
  284. */
  285. private function GIFWord($int)
  286. {
  287. return (chr($int & 0xFF) . chr(($int >> 8) & 0xFF));
  288. }
  289. /*
  290. :::::::::::::::::::::::::::::::::::::::::::::::::::
  291. ::
  292. :: GetAnimation...
  293. ::
  294. */
  295. public function GetAnimation()
  296. {
  297. return ($this->GIF);
  298. }
  299. }
  300. /*
  301. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  302. ::
  303. :: GIFDecoder Version 2.0 by László Zsidi, http://gifs.hu
  304. ::
  305. :: Created at 2007. 02. 01. '07.47.AM'
  306. ::
  307. ::
  308. ::
  309. ::
  310. :: Try on-line GIFBuilder Form demo based on GIFDecoder.
  311. ::
  312. :: http://gifs.hu/phpclasses/demos/GifBuilder/
  313. ::
  314. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  315. */
  316. class GIFDecoder
  317. {
  318. private $GIF_buffer = array();
  319. private $GIF_arrays = array();
  320. private $GIF_delays = array();
  321. private $GIF_stream = "";
  322. private $GIF_string = "";
  323. private $GIF_bfseek = 0;
  324. private $GIF_screen = array();
  325. private $GIF_global = array();
  326. private $GIF_sorted;
  327. private $GIF_colorS;
  328. private $GIF_colorC;
  329. private $GIF_colorF;
  330. /*
  331. :::::::::::::::::::::::::::::::::::::::::::::::::::
  332. ::
  333. :: GIFDecoder ( $GIF_pointer )
  334. ::
  335. */
  336. public function __construct($GIF_pointer)
  337. {
  338. $this->GIF_stream = $GIF_pointer;
  339. $this->GIFGetByte(6); // GIF89a
  340. $this->GIFGetByte(7); // Logical Screen Descriptor
  341. $this->GIF_screen = $this->GIF_buffer;
  342. $this->GIF_colorF = $this->GIF_buffer[4] & 0x80 ? 1 : 0;
  343. $this->GIF_sorted = $this->GIF_buffer[4] & 0x08 ? 1 : 0;
  344. $this->GIF_colorC = $this->GIF_buffer[4] & 0x07;
  345. $this->GIF_colorS = 2 << $this->GIF_colorC;
  346. if (1 == $this->GIF_colorF) {
  347. $this->GIFGetByte(3 * $this->GIF_colorS);
  348. $this->GIF_global = $this->GIF_buffer;
  349. }
  350. /*
  351. *
  352. * 05.06.2007.
  353. * Made a little modification
  354. *
  355. *
  356. - for ( $cycle = 1; $cycle; ) {
  357. + if ( GIFDecoder::GIFGetByte ( 1 ) ) {
  358. - switch ( $this->GIF_buffer [ 0 ] ) {
  359. - case 0x21:
  360. - GIFDecoder::GIFReadExtensions ( );
  361. - break;
  362. - case 0x2C:
  363. - GIFDecoder::GIFReadDescriptor ( );
  364. - break;
  365. - case 0x3B:
  366. - $cycle = 0;
  367. - break;
  368. - }
  369. - }
  370. + else {
  371. + $cycle = 0;
  372. + }
  373. - }
  374. */
  375. for ($cycle = 1; $cycle;) {
  376. if ($this->GIFGetByte(1)) {
  377. switch ($this->GIF_buffer[0]) {
  378. case 0x21:
  379. $this->GIFReadExtensions();
  380. break;
  381. case 0x2C:
  382. $this->GIFReadDescriptor();
  383. break;
  384. case 0x3B:
  385. $cycle = 0;
  386. break;
  387. }
  388. } else {
  389. $cycle = 0;
  390. }
  391. }
  392. }
  393. /*
  394. :::::::::::::::::::::::::::::::::::::::::::::::::::
  395. ::
  396. :: GIFReadExtension ( )
  397. ::
  398. */
  399. private function GIFReadExtensions()
  400. {
  401. $this->GIFGetByte(1);
  402. for (;;) {
  403. $this->GIFGetByte(1);
  404. if (($u = $this->GIF_buffer[0]) == 0x00) {
  405. break;
  406. }
  407. $this->GIFGetByte($u);
  408. /*
  409. * 07.05.2007.
  410. * Implemented a new line for a new function
  411. * to determine the originaly delays between
  412. * frames.
  413. *
  414. */
  415. if (4 == $u) {
  416. $this->GIF_delays[] = ($this->GIF_buffer[1] | $this->GIF_buffer[2] << 8);
  417. }
  418. }
  419. }
  420. /*
  421. :::::::::::::::::::::::::::::::::::::::::::::::::::
  422. ::
  423. :: GIFReadExtension ( )
  424. ::
  425. */
  426. private function GIFReadDescriptor()
  427. {
  428. $GIF_screen = array();
  429. $this->GIFGetByte(9);
  430. $GIF_screen = $this->GIF_buffer;
  431. $GIF_colorF = $this->GIF_buffer[8] & 0x80 ? 1 : 0;
  432. if ($GIF_colorF) {
  433. $GIF_code = $this->GIF_buffer[8] & 0x07;
  434. $GIF_sort = $this->GIF_buffer[8] & 0x20 ? 1 : 0;
  435. } else {
  436. $GIF_code = $this->GIF_colorC;
  437. $GIF_sort = $this->GIF_sorted;
  438. }
  439. $GIF_size = 2 << $GIF_code;
  440. $this->GIF_screen[4] &= 0x70;
  441. $this->GIF_screen[4] |= 0x80;
  442. $this->GIF_screen[4] |= $GIF_code;
  443. if ($GIF_sort) {
  444. $this->GIF_screen[4] |= 0x08;
  445. }
  446. $this->GIF_string = "GIF87a";
  447. $this->GIFPutByte($this->GIF_screen);
  448. if (1 == $GIF_colorF) {
  449. $this->GIFGetByte(3 * $GIF_size);
  450. $this->GIFPutByte($this->GIF_buffer);
  451. } else {
  452. $this->GIFPutByte($this->GIF_global);
  453. }
  454. $this->GIF_string .= chr(0x2C);
  455. $GIF_screen[8] &= 0x40;
  456. $this->GIFPutByte($GIF_screen);
  457. $this->GIFGetByte(1);
  458. $this->GIFPutByte($this->GIF_buffer);
  459. for (;;) {
  460. $this->GIFGetByte(1);
  461. $this->GIFPutByte($this->GIF_buffer);
  462. if (($u = $this->GIF_buffer[0]) == 0x00) {
  463. break;
  464. }
  465. $this->GIFGetByte($u);
  466. $this->GIFPutByte($this->GIF_buffer);
  467. }
  468. $this->GIF_string .= chr(0x3B);
  469. /*
  470. Add frames into $GIF_stream array...
  471. */
  472. $this->GIF_arrays[] = $this->GIF_string;
  473. }
  474. /*
  475. :::::::::::::::::::::::::::::::::::::::::::::::::::
  476. ::
  477. :: GIFGetByte ( $len )
  478. ::
  479. */
  480. /*
  481. *
  482. * 05.06.2007.
  483. * Made a little modification
  484. *
  485. *
  486. - function GIFGetByte ( $len ) {
  487. - $this->GIF_buffer = Array ( );
  488. -
  489. - for ( $i = 0; $i < $len; $i++ ) {
  490. + if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) {
  491. + return 0;
  492. + }
  493. - $this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } );
  494. - }
  495. + return 1;
  496. - }
  497. */
  498. private function GIFGetByte($len)
  499. {
  500. $this->GIF_buffer = array();
  501. for ($i = 0; $i < $len; $i++) {
  502. if ($this->GIF_bfseek > strlen($this->GIF_stream)) {
  503. return 0;
  504. }
  505. $this->GIF_buffer[] = ord($this->GIF_stream{$this->GIF_bfseek++});
  506. }
  507. return 1;
  508. }
  509. /*
  510. :::::::::::::::::::::::::::::::::::::::::::::::::::
  511. ::
  512. :: GIFPutByte ( $bytes )
  513. ::
  514. */
  515. private function GIFPutByte($bytes)
  516. {
  517. for ($i = 0; $i < count($bytes); $i++) {
  518. $this->GIF_string .= chr($bytes[$i]);
  519. }
  520. }
  521. /*
  522. :::::::::::::::::::::::::::::::::::::::::::::::::::
  523. ::
  524. :: PUBLIC FUNCTIONS
  525. ::
  526. ::
  527. :: GIFGetFrames ( )
  528. ::
  529. */
  530. public function GIFGetFrames()
  531. {
  532. return ($this->GIF_arrays);
  533. }
  534. /*
  535. :::::::::::::::::::::::::::::::::::::::::::::::::::
  536. ::
  537. :: GIFGetDelays ( )
  538. ::
  539. */
  540. public function GIFGetDelays()
  541. {
  542. return ($this->GIF_delays);
  543. }
  544. }