gif.php 13 KB

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