gif.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////////////////////////
  3. // 2009-12-22 Adapted for mPDF 4.2
  4. ///////////////////////////////////////////////////////////////////////////////////////////////////
  5. // GIF Util - (C) 2003 Yamasoft (S/C)
  6. // http://www.yamasoft.com
  7. // All Rights Reserved
  8. // This file can be freely copied, distributed, modified, updated by anyone under the only
  9. // condition to leave the original address (Yamasoft, http://www.yamasoft.com) and this header.
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. ///////////////////////////////////////////////////////////////////////////////////////////////////
  12. // 2009-12-22 Adapted INB
  13. // Functions calling functionname($x, $len = 0) were not working on PHP5.1.5 as pass by reference
  14. // All edited to $len = 0; then call function.
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////
  17. class CGIFLZW
  18. {
  19. var $MAX_LZW_BITS;
  20. var $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode;
  21. var $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte;
  22. ///////////////////////////////////////////////////////////////////////////
  23. // CONSTRUCTOR
  24. function CGIFLZW()
  25. {
  26. $this->MAX_LZW_BITS = 12;
  27. unSet($this->Next);
  28. unSet($this->Vals);
  29. unSet($this->Stack);
  30. unSet($this->Buf);
  31. $this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1);
  32. $this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1);
  33. $this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1);
  34. $this->Buf = range(0, 279);
  35. }
  36. ///////////////////////////////////////////////////////////////////////////
  37. function deCompress($data, &$datLen)
  38. {
  39. $stLen = strlen($data);
  40. $datLen = 0;
  41. $ret = "";
  42. $dp = 0; // data pointer
  43. // INITIALIZATION
  44. $this->LZWCommandInit($data, $dp);
  45. while(($iIndex = $this->LZWCommand($data, $dp)) >= 0) {
  46. $ret .= chr($iIndex);
  47. }
  48. $datLen = $dp;
  49. if($iIndex != -2) {
  50. return false;
  51. }
  52. return $ret;
  53. }
  54. ///////////////////////////////////////////////////////////////////////////
  55. function LZWCommandInit(&$data, &$dp)
  56. {
  57. $this->SetCodeSize = ord($data[0]);
  58. $dp += 1;
  59. $this->CodeSize = $this->SetCodeSize + 1;
  60. $this->ClearCode = 1 << $this->SetCodeSize;
  61. $this->EndCode = $this->ClearCode + 1;
  62. $this->MaxCode = $this->ClearCode + 2;
  63. $this->MaxCodeSize = $this->ClearCode << 1;
  64. $this->GetCodeInit($data, $dp);
  65. $this->Fresh = 1;
  66. for($i = 0; $i < $this->ClearCode; $i++) {
  67. $this->Next[$i] = 0;
  68. $this->Vals[$i] = $i;
  69. }
  70. for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
  71. $this->Next[$i] = 0;
  72. $this->Vals[$i] = 0;
  73. }
  74. $this->sp = 0;
  75. return 1;
  76. }
  77. function LZWCommand(&$data, &$dp)
  78. {
  79. if($this->Fresh) {
  80. $this->Fresh = 0;
  81. do {
  82. $this->FirstCode = $this->GetCode($data, $dp);
  83. $this->OldCode = $this->FirstCode;
  84. }
  85. while($this->FirstCode == $this->ClearCode);
  86. return $this->FirstCode;
  87. }
  88. if($this->sp > 0) {
  89. $this->sp--;
  90. return $this->Stack[$this->sp];
  91. }
  92. while(($Code = $this->GetCode($data, $dp)) >= 0) {
  93. if($Code == $this->ClearCode) {
  94. for($i = 0; $i < $this->ClearCode; $i++) {
  95. $this->Next[$i] = 0;
  96. $this->Vals[$i] = $i;
  97. }
  98. for(; $i < (1 << $this->MAX_LZW_BITS); $i++) {
  99. $this->Next[$i] = 0;
  100. $this->Vals[$i] = 0;
  101. }
  102. $this->CodeSize = $this->SetCodeSize + 1;
  103. $this->MaxCodeSize = $this->ClearCode << 1;
  104. $this->MaxCode = $this->ClearCode + 2;
  105. $this->sp = 0;
  106. $this->FirstCode = $this->GetCode($data, $dp);
  107. $this->OldCode = $this->FirstCode;
  108. return $this->FirstCode;
  109. }
  110. if($Code == $this->EndCode) {
  111. return -2;
  112. }
  113. $InCode = $Code;
  114. if($Code >= $this->MaxCode) {
  115. $this->Stack[$this->sp++] = $this->FirstCode;
  116. $Code = $this->OldCode;
  117. }
  118. while($Code >= $this->ClearCode) {
  119. $this->Stack[$this->sp++] = $this->Vals[$Code];
  120. if($Code == $this->Next[$Code]) // Circular table entry, big GIF Error!
  121. return -1;
  122. $Code = $this->Next[$Code];
  123. }
  124. $this->FirstCode = $this->Vals[$Code];
  125. $this->Stack[$this->sp++] = $this->FirstCode;
  126. if(($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) {
  127. $this->Next[$Code] = $this->OldCode;
  128. $this->Vals[$Code] = $this->FirstCode;
  129. $this->MaxCode++;
  130. if(($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) {
  131. $this->MaxCodeSize *= 2;
  132. $this->CodeSize++;
  133. }
  134. }
  135. $this->OldCode = $InCode;
  136. if($this->sp > 0) {
  137. $this->sp--;
  138. return $this->Stack[$this->sp];
  139. }
  140. }
  141. return $Code;
  142. }
  143. ///////////////////////////////////////////////////////////////////////////
  144. function GetCodeInit(&$data, &$dp)
  145. {
  146. $this->CurBit = 0;
  147. $this->LastBit = 0;
  148. $this->Done = 0;
  149. $this->LastByte = 2;
  150. return 1;
  151. }
  152. function GetCode(&$data, &$dp)
  153. {
  154. if(($this->CurBit + $this->CodeSize) >= $this->LastBit) {
  155. if($this->Done) {
  156. if($this->CurBit >= $this->LastBit) {
  157. // Ran off the end of my bits
  158. return 0;
  159. }
  160. return -1;
  161. }
  162. $this->Buf[0] = $this->Buf[$this->LastByte - 2];
  163. $this->Buf[1] = $this->Buf[$this->LastByte - 1];
  164. $Count = ord($data[$dp]);
  165. $dp += 1;
  166. if($Count) {
  167. for($i = 0; $i < $Count; $i++) {
  168. $this->Buf[2 + $i] = ord($data[$dp+$i]);
  169. }
  170. $dp += $Count;
  171. }
  172. else {
  173. $this->Done = 1;
  174. }
  175. $this->LastByte = 2 + $Count;
  176. $this->CurBit = ($this->CurBit - $this->LastBit) + 16;
  177. $this->LastBit = (2 + $Count) << 3;
  178. }
  179. $iRet = 0;
  180. for($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) {
  181. $iRet |= (($this->Buf[intval($i / 8)] & (1 << ($i % 8))) != 0) << $j;
  182. }
  183. $this->CurBit += $this->CodeSize;
  184. return $iRet;
  185. }
  186. }
  187. ///////////////////////////////////////////////////////////////////////////////////////////////////
  188. class CGIFCOLORTABLE
  189. {
  190. var $m_nColors;
  191. var $m_arColors;
  192. ///////////////////////////////////////////////////////////////////////////
  193. // CONSTRUCTOR
  194. function CGIFCOLORTABLE()
  195. {
  196. unSet($this->m_nColors);
  197. unSet($this->m_arColors);
  198. }
  199. ///////////////////////////////////////////////////////////////////////////
  200. function load($lpData, $num)
  201. {
  202. $this->m_nColors = 0;
  203. $this->m_arColors = array();
  204. for($i = 0; $i < $num; $i++) {
  205. $rgb = substr($lpData, $i * 3, 3);
  206. if(strlen($rgb) < 3) {
  207. return false;
  208. }
  209. $this->m_arColors[] = (ord($rgb[2]) << 16) + (ord($rgb[1]) << 8) + ord($rgb[0]);
  210. $this->m_nColors++;
  211. }
  212. return true;
  213. }
  214. ///////////////////////////////////////////////////////////////////////////
  215. function toString()
  216. {
  217. $ret = "";
  218. for($i = 0; $i < $this->m_nColors; $i++) {
  219. $ret .=
  220. chr(($this->m_arColors[$i] & 0x000000FF)) . // R
  221. chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
  222. chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B
  223. }
  224. return $ret;
  225. }
  226. ///////////////////////////////////////////////////////////////////////////
  227. function colorIndex($rgb)
  228. {
  229. $rgb = intval($rgb) & 0xFFFFFF;
  230. $r1 = ($rgb & 0x0000FF);
  231. $g1 = ($rgb & 0x00FF00) >> 8;
  232. $b1 = ($rgb & 0xFF0000) >> 16;
  233. $idx = -1;
  234. for($i = 0; $i < $this->m_nColors; $i++) {
  235. $r2 = ($this->m_arColors[$i] & 0x000000FF);
  236. $g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8;
  237. $b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16;
  238. $d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
  239. if(($idx == -1) || ($d < $dif)) {
  240. $idx = $i;
  241. $dif = $d;
  242. }
  243. }
  244. return $idx;
  245. }
  246. }
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////
  248. class CGIFFILEHEADER
  249. {
  250. var $m_lpVer;
  251. var $m_nWidth;
  252. var $m_nHeight;
  253. var $m_bGlobalClr;
  254. var $m_nColorRes;
  255. var $m_bSorted;
  256. var $m_nTableSize;
  257. var $m_nBgColor;
  258. var $m_nPixelRatio;
  259. var $m_colorTable;
  260. ///////////////////////////////////////////////////////////////////////////
  261. // CONSTRUCTOR
  262. function CGIFFILEHEADER()
  263. {
  264. unSet($this->m_lpVer);
  265. unSet($this->m_nWidth);
  266. unSet($this->m_nHeight);
  267. unSet($this->m_bGlobalClr);
  268. unSet($this->m_nColorRes);
  269. unSet($this->m_bSorted);
  270. unSet($this->m_nTableSize);
  271. unSet($this->m_nBgColor);
  272. unSet($this->m_nPixelRatio);
  273. unSet($this->m_colorTable);
  274. }
  275. ///////////////////////////////////////////////////////////////////////////
  276. function load($lpData, &$hdrLen)
  277. {
  278. $hdrLen = 0;
  279. $this->m_lpVer = substr($lpData, 0, 6);
  280. if(($this->m_lpVer <> "GIF87a") && ($this->m_lpVer <> "GIF89a")) {
  281. return false;
  282. }
  283. $this->m_nWidth = $this->w2i(substr($lpData, 6, 2));
  284. $this->m_nHeight = $this->w2i(substr($lpData, 8, 2));
  285. if(!$this->m_nWidth || !$this->m_nHeight) {
  286. return false;
  287. }
  288. $b = ord(substr($lpData, 10, 1));
  289. $this->m_bGlobalClr = ($b & 0x80) ? true : false;
  290. $this->m_nColorRes = ($b & 0x70) >> 4;
  291. $this->m_bSorted = ($b & 0x08) ? true : false;
  292. $this->m_nTableSize = 2 << ($b & 0x07);
  293. $this->m_nBgColor = ord(substr($lpData, 11, 1));
  294. $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
  295. $hdrLen = 13;
  296. if($this->m_bGlobalClr) {
  297. $this->m_colorTable = new CGIFCOLORTABLE();
  298. if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
  299. return false;
  300. }
  301. $hdrLen += 3 * $this->m_nTableSize;
  302. }
  303. return true;
  304. }
  305. ///////////////////////////////////////////////////////////////////////////
  306. function w2i($str)
  307. {
  308. return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
  309. }
  310. }
  311. ///////////////////////////////////////////////////////////////////////////////////////////////////
  312. class CGIFIMAGEHEADER
  313. {
  314. var $m_nLeft;
  315. var $m_nTop;
  316. var $m_nWidth;
  317. var $m_nHeight;
  318. var $m_bLocalClr;
  319. var $m_bInterlace;
  320. var $m_bSorted;
  321. var $m_nTableSize;
  322. var $m_colorTable;
  323. ///////////////////////////////////////////////////////////////////////////
  324. // CONSTRUCTOR
  325. function CGIFIMAGEHEADER()
  326. {
  327. unSet($this->m_nLeft);
  328. unSet($this->m_nTop);
  329. unSet($this->m_nWidth);
  330. unSet($this->m_nHeight);
  331. unSet($this->m_bLocalClr);
  332. unSet($this->m_bInterlace);
  333. unSet($this->m_bSorted);
  334. unSet($this->m_nTableSize);
  335. unSet($this->m_colorTable);
  336. }
  337. ///////////////////////////////////////////////////////////////////////////
  338. function load($lpData, &$hdrLen)
  339. {
  340. $hdrLen = 0;
  341. $this->m_nLeft = $this->w2i(substr($lpData, 0, 2));
  342. $this->m_nTop = $this->w2i(substr($lpData, 2, 2));
  343. $this->m_nWidth = $this->w2i(substr($lpData, 4, 2));
  344. $this->m_nHeight = $this->w2i(substr($lpData, 6, 2));
  345. if(!$this->m_nWidth || !$this->m_nHeight) {
  346. return false;
  347. }
  348. $b = ord($lpData{8});
  349. $this->m_bLocalClr = ($b & 0x80) ? true : false;
  350. $this->m_bInterlace = ($b & 0x40) ? true : false;
  351. $this->m_bSorted = ($b & 0x20) ? true : false;
  352. $this->m_nTableSize = 2 << ($b & 0x07);
  353. $hdrLen = 9;
  354. if($this->m_bLocalClr) {
  355. $this->m_colorTable = new CGIFCOLORTABLE();
  356. if(!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
  357. return false;
  358. }
  359. $hdrLen += 3 * $this->m_nTableSize;
  360. }
  361. return true;
  362. }
  363. ///////////////////////////////////////////////////////////////////////////
  364. function w2i($str)
  365. {
  366. return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
  367. }
  368. }
  369. ///////////////////////////////////////////////////////////////////////////////////////////////////
  370. class CGIFIMAGE
  371. {
  372. var $m_disp;
  373. var $m_bUser;
  374. var $m_bTrans;
  375. var $m_nDelay;
  376. var $m_nTrans;
  377. var $m_lpComm;
  378. var $m_gih;
  379. var $m_data;
  380. var $m_lzw;
  381. ///////////////////////////////////////////////////////////////////////////
  382. function CGIFIMAGE()
  383. {
  384. unSet($this->m_disp);
  385. unSet($this->m_bUser);
  386. unSet($this->m_bTrans);
  387. unSet($this->m_nDelay);
  388. unSet($this->m_nTrans);
  389. unSet($this->m_lpComm);
  390. unSet($this->m_data);
  391. $this->m_gih = new CGIFIMAGEHEADER();
  392. $this->m_lzw = new CGIFLZW();
  393. }
  394. ///////////////////////////////////////////////////////////////////////////
  395. function load($data, &$datLen)
  396. {
  397. $datLen = 0;
  398. while(true) {
  399. $b = ord($data[0]);
  400. $data = substr($data, 1);
  401. $datLen++;
  402. switch($b) {
  403. case 0x21: // Extension
  404. $len = 0;
  405. if(!$this->skipExt($data, $len)) {
  406. return false;
  407. }
  408. $datLen += $len;
  409. break;
  410. case 0x2C: // Image
  411. // LOAD HEADER & COLOR TABLE
  412. $len = 0;
  413. if(!$this->m_gih->load($data, $len)) {
  414. return false;
  415. }
  416. $data = substr($data, $len);
  417. $datLen += $len;
  418. // ALLOC BUFFER
  419. $len = 0;
  420. if(!($this->m_data = $this->m_lzw->deCompress($data, $len))) {
  421. return false;
  422. }
  423. $data = substr($data, $len);
  424. $datLen += $len;
  425. if($this->m_gih->m_bInterlace) {
  426. $this->deInterlace();
  427. }
  428. return true;
  429. case 0x3B: // EOF
  430. default:
  431. return false;
  432. }
  433. }
  434. return false;
  435. }
  436. ///////////////////////////////////////////////////////////////////////////
  437. function skipExt(&$data, &$extLen)
  438. {
  439. $extLen = 0;
  440. $b = ord($data[0]);
  441. $data = substr($data, 1);
  442. $extLen++;
  443. switch($b) {
  444. case 0xF9: // Graphic Control
  445. $b = ord($data[1]);
  446. $this->m_disp = ($b & 0x1C) >> 2;
  447. $this->m_bUser = ($b & 0x02) ? true : false;
  448. $this->m_bTrans = ($b & 0x01) ? true : false;
  449. $this->m_nDelay = $this->w2i(substr($data, 2, 2));
  450. $this->m_nTrans = ord($data[4]);
  451. break;
  452. case 0xFE: // Comment
  453. $this->m_lpComm = substr($data, 1, ord($data[0]));
  454. break;
  455. case 0x01: // Plain text
  456. break;
  457. case 0xFF: // Application
  458. break;
  459. }
  460. // SKIP DEFAULT AS DEFS MAY CHANGE
  461. $b = ord($data[0]);
  462. $data = substr($data, 1);
  463. $extLen++;
  464. while($b > 0) {
  465. $data = substr($data, $b);
  466. $extLen += $b;
  467. $b = ord($data[0]);
  468. $data = substr($data, 1);
  469. $extLen++;
  470. }
  471. return true;
  472. }
  473. ///////////////////////////////////////////////////////////////////////////
  474. function w2i($str)
  475. {
  476. return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
  477. }
  478. ///////////////////////////////////////////////////////////////////////////
  479. function deInterlace()
  480. {
  481. $data = $this->m_data;
  482. for($i = 0; $i < 4; $i++) {
  483. switch($i) {
  484. case 0:
  485. $s = 8;
  486. $y = 0;
  487. break;
  488. case 1:
  489. $s = 8;
  490. $y = 4;
  491. break;
  492. case 2:
  493. $s = 4;
  494. $y = 2;
  495. break;
  496. case 3:
  497. $s = 2;
  498. $y = 1;
  499. break;
  500. }
  501. for(; $y < $this->m_gih->m_nHeight; $y += $s) {
  502. $lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
  503. $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
  504. $data =
  505. substr($data, 0, $y * $this->m_gih->m_nWidth) .
  506. $lne .
  507. substr($data, ($y + 1) * $this->m_gih->m_nWidth);
  508. }
  509. }
  510. $this->m_data = $data;
  511. }
  512. }
  513. ///////////////////////////////////////////////////////////////////////////////////////////////////
  514. class CGIF
  515. {
  516. var $m_gfh;
  517. var $m_lpData;
  518. var $m_img;
  519. var $m_bLoaded;
  520. ///////////////////////////////////////////////////////////////////////////
  521. // CONSTRUCTOR
  522. function CGIF()
  523. {
  524. $this->m_gfh = new CGIFFILEHEADER();
  525. $this->m_img = new CGIFIMAGE();
  526. $this->m_lpData = "";
  527. $this->m_bLoaded = false;
  528. }
  529. ///////////////////////////////////////////////////////////////////////////
  530. function ClearData() {
  531. $this->m_lpData = '';
  532. unSet($this->m_img->m_data);
  533. unSet($this->m_img->m_lzw->Next);
  534. unSet($this->m_img->m_lzw->Vals);
  535. unSet($this->m_img->m_lzw->Stack);
  536. unSet($this->m_img->m_lzw->Buf);
  537. }
  538. function loadFile(&$data, $iIndex)
  539. {
  540. if($iIndex < 0) {
  541. return false;
  542. }
  543. $this->m_lpData = $data;
  544. // GET FILE HEADER
  545. $len = 0;
  546. if(!$this->m_gfh->load($this->m_lpData, $len)) {
  547. return false;
  548. }
  549. $this->m_lpData = substr($this->m_lpData, $len);
  550. do {
  551. $imgLen = 0;
  552. if(!$this->m_img->load($this->m_lpData, $imgLen)) {
  553. return false;
  554. }
  555. $this->m_lpData = substr($this->m_lpData, $imgLen);
  556. }
  557. while($iIndex-- > 0);
  558. $this->m_bLoaded = true;
  559. return true;
  560. }
  561. }
  562. ///////////////////////////////////////////////////////////////////////////////////////////////////
  563. ?>