2
0

Class-Graphics.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. /**
  3. * Classes used for reading gif files (in case PHP's GD doesn't provide the
  4. * proper gif-functions).
  5. *
  6. * Gif Util copyright 2003 by Yamasoft (S/C). All rights reserved.
  7. * Do not remove this portion of the header, or use these functions except
  8. * from the original author. To get it, please navigate to:
  9. * http://www.yamasoft.com/php-gif.zip
  10. *
  11. * Simple Machines Forum (SMF)
  12. *
  13. * @package SMF
  14. * @author Simple Machines http://www.simplemachines.org
  15. * @copyright 2013 Simple Machines and individual contributors
  16. * @license http://www.simplemachines.org/about/smf/license.php BSD
  17. *
  18. * @version 2.1 Alpha 1
  19. */
  20. if (!defined('SMF'))
  21. die('No direct access...');
  22. /**
  23. * An implementation of the LZW compression algorithm
  24. */
  25. class gif_lzw_compression
  26. {
  27. public $MAX_LZW_BITS;
  28. public $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode;
  29. public $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte;
  30. public function __construct()
  31. {
  32. $this->MAX_LZW_BITS = 12;
  33. unset($this->Next, $this->Vals, $this->Stack, $this->Buf);
  34. $this->Next = range(0, (1 << $this->MAX_LZW_BITS) - 1);
  35. $this->Vals = range(0, (1 << $this->MAX_LZW_BITS) - 1);
  36. $this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1);
  37. $this->Buf = range(0, 279);
  38. }
  39. public function decompress($data, &$datLen)
  40. {
  41. $stLen = strlen($data);
  42. $datLen = 0;
  43. $ret = '';
  44. $this->LZWCommand($data, true);
  45. while (($iIndex = $this->LZWCommand($data, false)) >= 0)
  46. $ret .= chr($iIndex);
  47. $datLen = $stLen - strlen($data);
  48. if ($iIndex != -2)
  49. return false;
  50. return $ret;
  51. }
  52. public function LZWCommand(&$data, $bInit)
  53. {
  54. if ($bInit)
  55. {
  56. $this->SetCodeSize = ord($data[0]);
  57. $data = substr($data, 1);
  58. $this->CodeSize = $this->SetCodeSize + 1;
  59. $this->ClearCode = 1 << $this->SetCodeSize;
  60. $this->EndCode = $this->ClearCode + 1;
  61. $this->MaxCode = $this->ClearCode + 2;
  62. $this->MaxCodeSize = $this->ClearCode << 1;
  63. $this->GetCode($data, $bInit);
  64. $this->Fresh = 1;
  65. for ($i = 0; $i < $this->ClearCode; $i++)
  66. {
  67. $this->Next[$i] = 0;
  68. $this->Vals[$i] = $i;
  69. }
  70. for (; $i < (1 << $this->MAX_LZW_BITS); $i++)
  71. {
  72. $this->Next[$i] = 0;
  73. $this->Vals[$i] = 0;
  74. }
  75. $this->sp = 0;
  76. return 1;
  77. }
  78. if ($this->Fresh)
  79. {
  80. $this->Fresh = 0;
  81. do
  82. {
  83. $this->FirstCode = $this->GetCode($data, $bInit);
  84. $this->OldCode = $this->FirstCode;
  85. }
  86. while ($this->FirstCode == $this->ClearCode);
  87. return $this->FirstCode;
  88. }
  89. if ($this->sp > 0)
  90. {
  91. $this->sp--;
  92. return $this->Stack[$this->sp];
  93. }
  94. while (($Code = $this->GetCode($data, $bInit)) >= 0)
  95. {
  96. if ($Code == $this->ClearCode)
  97. {
  98. for ($i = 0; $i < $this->ClearCode; $i++)
  99. {
  100. $this->Next[$i] = 0;
  101. $this->Vals[$i] = $i;
  102. }
  103. for (; $i < (1 << $this->MAX_LZW_BITS); $i++)
  104. {
  105. $this->Next[$i] = 0;
  106. $this->Vals[$i] = 0;
  107. }
  108. $this->CodeSize = $this->SetCodeSize + 1;
  109. $this->MaxCodeSize = $this->ClearCode << 1;
  110. $this->MaxCode = $this->ClearCode + 2;
  111. $this->sp = 0;
  112. $this->FirstCode = $this->GetCode($data, $bInit);
  113. $this->OldCode = $this->FirstCode;
  114. return $this->FirstCode;
  115. }
  116. if ($Code == $this->EndCode)
  117. return -2;
  118. $InCode = $Code;
  119. if ($Code >= $this->MaxCode)
  120. {
  121. $this->Stack[$this->sp] = $this->FirstCode;
  122. $this->sp++;
  123. $Code = $this->OldCode;
  124. }
  125. while ($Code >= $this->ClearCode)
  126. {
  127. $this->Stack[$this->sp] = $this->Vals[$Code];
  128. $this->sp++;
  129. if ($Code == $this->Next[$Code]) // Circular table entry, big GIF Error!
  130. return -1;
  131. $Code = $this->Next[$Code];
  132. }
  133. $this->FirstCode = $this->Vals[$Code];
  134. $this->Stack[$this->sp] = $this->FirstCode;
  135. $this->sp++;
  136. if (($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS))
  137. {
  138. $this->Next[$Code] = $this->OldCode;
  139. $this->Vals[$Code] = $this->FirstCode;
  140. $this->MaxCode++;
  141. if (($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS)))
  142. {
  143. $this->MaxCodeSize *= 2;
  144. $this->CodeSize++;
  145. }
  146. }
  147. $this->OldCode = $InCode;
  148. if ($this->sp > 0)
  149. {
  150. $this->sp--;
  151. return $this->Stack[$this->sp];
  152. }
  153. }
  154. return $Code;
  155. }
  156. public function GetCode(&$data, $bInit)
  157. {
  158. if ($bInit)
  159. {
  160. $this->CurBit = 0;
  161. $this->LastBit = 0;
  162. $this->Done = 0;
  163. $this->LastByte = 2;
  164. return 1;
  165. }
  166. if (($this->CurBit + $this->CodeSize) >= $this->LastBit)
  167. {
  168. if ($this->Done)
  169. {
  170. // Ran off the end of my bits...
  171. if ($this->CurBit >= $this->LastBit)
  172. return 0;
  173. return -1;
  174. }
  175. $this->Buf[0] = $this->Buf[$this->LastByte - 2];
  176. $this->Buf[1] = $this->Buf[$this->LastByte - 1];
  177. $count = ord($data[0]);
  178. $data = substr($data, 1);
  179. if ($count)
  180. {
  181. for ($i = 0; $i < $count; $i++)
  182. $this->Buf[2 + $i] = ord($data{$i});
  183. $data = substr($data, $count);
  184. }
  185. else
  186. $this->Done = 1;
  187. $this->LastByte = 2 + $count;
  188. $this->CurBit = ($this->CurBit - $this->LastBit) + 16;
  189. $this->LastBit = (2 + $count) << 3;
  190. }
  191. $iRet = 0;
  192. for ($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++)
  193. $iRet |= (($this->Buf[intval($i / 8)] & (1 << ($i % 8))) != 0) << $j;
  194. $this->CurBit += $this->CodeSize;
  195. return $iRet;
  196. }
  197. }
  198. class gif_color_table
  199. {
  200. public $m_nColors;
  201. public $m_arColors;
  202. public function __construct()
  203. {
  204. unset($this->m_nColors, $this->m_arColors);
  205. }
  206. public function load($lpData, $num)
  207. {
  208. $this->m_nColors = 0;
  209. $this->m_arColors = array();
  210. for ($i = 0; $i < $num; $i++)
  211. {
  212. $rgb = substr($lpData, $i * 3, 3);
  213. if (strlen($rgb) < 3)
  214. return false;
  215. $this->m_arColors[] = (ord($rgb[2]) << 16) + (ord($rgb[1]) << 8) + ord($rgb[0]);
  216. $this->m_nColors++;
  217. }
  218. return true;
  219. }
  220. public function toString()
  221. {
  222. $ret = '';
  223. for ($i = 0; $i < $this->m_nColors; $i++)
  224. {
  225. $ret .=
  226. chr(($this->m_arColors[$i] & 0x000000FF)) . // R
  227. chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
  228. chr(($this->m_arColors[$i] & 0x00FF0000) >> 16); // B
  229. }
  230. return $ret;
  231. }
  232. public function colorIndex($rgb)
  233. {
  234. $rgb = intval($rgb) & 0xFFFFFF;
  235. $r1 = ($rgb & 0x0000FF);
  236. $g1 = ($rgb & 0x00FF00) >> 8;
  237. $b1 = ($rgb & 0xFF0000) >> 16;
  238. $idx = -1;
  239. for ($i = 0; $i < $this->m_nColors; $i++)
  240. {
  241. $r2 = ($this->m_arColors[$i] & 0x000000FF);
  242. $g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8;
  243. $b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16;
  244. $d = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
  245. if (($idx == -1) || ($d < $dif))
  246. {
  247. $idx = $i;
  248. $dif = $d;
  249. }
  250. }
  251. return $idx;
  252. }
  253. }
  254. class gif_file_header
  255. {
  256. public $m_lpVer, $m_nWidth, $m_nHeight, $m_bGlobalClr, $m_nColorRes;
  257. public $m_bSorted, $m_nTableSize, $m_nBgColor, $m_nPixelRatio;
  258. public $m_colorTable;
  259. public function __construct()
  260. {
  261. unset($this->m_lpVer, $this->m_nWidth, $this->m_nHeight, $this->m_bGlobalClr, $this->m_nColorRes);
  262. unset($this->m_bSorted, $this->m_nTableSize, $this->m_nBgColor, $this->m_nPixelRatio, $this->m_colorTable);
  263. }
  264. public function load($lpData, &$hdrLen)
  265. {
  266. $hdrLen = 0;
  267. $this->m_lpVer = substr($lpData, 0, 6);
  268. if (($this->m_lpVer != 'GIF87a') && ($this->m_lpVer != 'GIF89a'))
  269. return false;
  270. list ($this->m_nWidth, $this->m_nHeight) = array_values(unpack('v2', substr($lpData, 6, 4)));
  271. if (!$this->m_nWidth || !$this->m_nHeight)
  272. return false;
  273. $b = ord(substr($lpData, 10, 1));
  274. $this->m_bGlobalClr = ($b & 0x80) ? true : false;
  275. $this->m_nColorRes = ($b & 0x70) >> 4;
  276. $this->m_bSorted = ($b & 0x08) ? true : false;
  277. $this->m_nTableSize = 2 << ($b & 0x07);
  278. $this->m_nBgColor = ord(substr($lpData, 11, 1));
  279. $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
  280. $hdrLen = 13;
  281. if ($this->m_bGlobalClr)
  282. {
  283. $this->m_colorTable = new gif_color_table();
  284. if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize))
  285. return false;
  286. $hdrLen += 3 * $this->m_nTableSize;
  287. }
  288. return true;
  289. }
  290. }
  291. class gif_image_header
  292. {
  293. public $m_nLeft, $m_nTop, $m_nWidth, $m_nHeight, $m_bLocalClr;
  294. public $m_bInterlace, $m_bSorted, $m_nTableSize, $m_colorTable;
  295. public function __construct()
  296. {
  297. unset($this->m_nLeft, $this->m_nTop, $this->m_nWidth, $this->m_nHeight, $this->m_bLocalClr);
  298. unset($this->m_bInterlace, $this->m_bSorted, $this->m_nTableSize, $this->m_colorTable);
  299. }
  300. public function load($lpData, &$hdrLen)
  301. {
  302. $hdrLen = 0;
  303. // Get the width/height/etc. from the header.
  304. list ($this->m_nLeft, $this->m_nTop, $this->m_nWidth, $this->m_nHeight) = array_values(unpack('v4', substr($lpData, 0, 8)));
  305. if (!$this->m_nWidth || !$this->m_nHeight)
  306. return false;
  307. $b = ord($lpData[8]);
  308. $this->m_bLocalClr = ($b & 0x80) ? true : false;
  309. $this->m_bInterlace = ($b & 0x40) ? true : false;
  310. $this->m_bSorted = ($b & 0x20) ? true : false;
  311. $this->m_nTableSize = 2 << ($b & 0x07);
  312. $hdrLen = 9;
  313. if ($this->m_bLocalClr)
  314. {
  315. $this->m_colorTable = new gif_color_table();
  316. if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize))
  317. return false;
  318. $hdrLen += 3 * $this->m_nTableSize;
  319. }
  320. return true;
  321. }
  322. }
  323. class gif_image
  324. {
  325. public $m_disp, $m_bUser, $m_bTrans, $m_nDelay, $m_nTrans, $m_lpComm;
  326. public $m_gih, $m_data, $m_lzw;
  327. public function __construct()
  328. {
  329. unset($this->m_disp, $this->m_bUser, $this->m_nDelay, $this->m_nTrans, $this->m_lpComm, $this->m_data);
  330. $this->m_gih = new gif_image_header();
  331. $this->m_lzw = new gif_lzw_compression();
  332. }
  333. public function load($data, &$datLen)
  334. {
  335. $datLen = 0;
  336. while (true)
  337. {
  338. $b = ord($data[0]);
  339. $data = substr($data, 1);
  340. $datLen++;
  341. switch ($b)
  342. {
  343. // Extension...
  344. case 0x21:
  345. $len = 0;
  346. if (!$this->skipExt($data, $len))
  347. return false;
  348. $datLen += $len;
  349. break;
  350. // Image...
  351. case 0x2C:
  352. // Load the header and color table.
  353. $len = 0;
  354. if (!$this->m_gih->load($data, $len))
  355. return false;
  356. $data = substr($data, $len);
  357. $datLen += $len;
  358. // Decompress the data, and ride on home ;).
  359. $len = 0;
  360. if (!($this->m_data = $this->m_lzw->decompress($data, $len)))
  361. return false;
  362. $data = substr($data, $len);
  363. $datLen += $len;
  364. if ($this->m_gih->m_bInterlace)
  365. $this->deInterlace();
  366. return true;
  367. case 0x3B: // EOF
  368. default:
  369. return false;
  370. }
  371. }
  372. return false;
  373. }
  374. public function skipExt(&$data, &$extLen)
  375. {
  376. $extLen = 0;
  377. $b = ord($data[0]);
  378. $data = substr($data, 1);
  379. $extLen++;
  380. switch ($b)
  381. {
  382. // Graphic Control...
  383. case 0xF9:
  384. $b = ord($data[1]);
  385. $this->m_disp = ($b & 0x1C) >> 2;
  386. $this->m_bUser = ($b & 0x02) ? true : false;
  387. $this->m_bTrans = ($b & 0x01) ? true : false;
  388. list ($this->m_nDelay) = array_values(unpack('v', substr($data, 2, 2)));
  389. $this->m_nTrans = ord($data[4]);
  390. break;
  391. // Comment...
  392. case 0xFE:
  393. $this->m_lpComm = substr($data, 1, ord($data[0]));
  394. break;
  395. // Plain text...
  396. case 0x01:
  397. break;
  398. // Application...
  399. case 0xFF:
  400. break;
  401. }
  402. // Skip default as defs may change.
  403. $b = ord($data[0]);
  404. $data = substr($data, 1);
  405. $extLen++;
  406. while ($b > 0)
  407. {
  408. $data = substr($data, $b);
  409. $extLen += $b;
  410. $b = ord($data[0]);
  411. $data = substr($data, 1);
  412. $extLen++;
  413. }
  414. return true;
  415. }
  416. public function deInterlace()
  417. {
  418. $data = $this->m_data;
  419. for ($i = 0; $i < 4; $i++)
  420. {
  421. switch ($i)
  422. {
  423. case 0:
  424. $s = 8;
  425. $y = 0;
  426. break;
  427. case 1:
  428. $s = 8;
  429. $y = 4;
  430. break;
  431. case 2:
  432. $s = 4;
  433. $y = 2;
  434. break;
  435. case 3:
  436. $s = 2;
  437. $y = 1;
  438. break;
  439. }
  440. for (; $y < $this->m_gih->m_nHeight; $y += $s)
  441. {
  442. $lne = substr($this->m_data, 0, $this->m_gih->m_nWidth);
  443. $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
  444. $data =
  445. substr($data, 0, $y * $this->m_gih->m_nWidth) .
  446. $lne .
  447. substr($data, ($y + 1) * $this->m_gih->m_nWidth);
  448. }
  449. }
  450. $this->m_data = $data;
  451. }
  452. }
  453. class gif_file
  454. {
  455. public $header, $image, $data, $loaded;
  456. public function __construct()
  457. {
  458. $this->data = '';
  459. $this->loaded = false;
  460. $this->header = new gif_file_header();
  461. $this->image = new gif_image();
  462. }
  463. public function loadFile($filename, $iIndex)
  464. {
  465. if ($iIndex < 0)
  466. return false;
  467. $this->data = @file_get_contents($filename);
  468. if ($this->data === false)
  469. return false;
  470. // Tell the header to load up....
  471. $len = 0;
  472. if (!$this->header->load($this->data, $len))
  473. return false;
  474. $this->data = substr($this->data, $len);
  475. // Keep reading (at least once) so we get to the actual image we're looking for.
  476. for ($j = 0; $j <= $iIndex; $j++)
  477. {
  478. $imgLen = 0;
  479. if (!$this->image->load($this->data, $imgLen))
  480. return false;
  481. $this->data = substr($this->data, $imgLen);
  482. }
  483. $this->loaded = true;
  484. return true;
  485. }
  486. public function get_png_data($background_color)
  487. {
  488. if (!$this->loaded)
  489. return false;
  490. // Prepare the color table.
  491. if ($this->image->m_gih->m_bLocalClr)
  492. {
  493. $colors = $this->image->m_gih->m_nTableSize;
  494. $pal = $this->image->m_gih->m_colorTable->toString();
  495. if ($background_color != -1)
  496. $background_color = $this->image->m_gih->m_colorTable->colorIndex($background_color);
  497. }
  498. elseif ($this->header->m_bGlobalClr)
  499. {
  500. $colors = $this->header->m_nTableSize;
  501. $pal = $this->header->m_colorTable->toString();
  502. if ($background_color != -1)
  503. $background_color = $this->header->m_colorTable->colorIndex($background_color);
  504. }
  505. else
  506. {
  507. $colors = 0;
  508. $background_color = -1;
  509. }
  510. if ($background_color == -1)
  511. $background_color = $this->header->m_nBgColor;
  512. $data = &$this->image->m_data;
  513. $header = &$this->image->m_gih;
  514. $i = 0;
  515. $bmp = '';
  516. // Prepare the bitmap itself.
  517. for ($y = 0; $y < $this->header->m_nHeight; $y++)
  518. {
  519. $bmp .= "\x00";
  520. for ($x = 0; $x < $this->header->m_nWidth; $x++, $i++)
  521. {
  522. // Is this in the proper range? If so, get the specific pixel data...
  523. if ($x >= $header->m_nLeft && $y >= $header->m_nTop && $x < ($header->m_nLeft + $header->m_nWidth) && $y < ($header->m_nTop + $header->m_nHeight))
  524. $bmp .= $data{$i};
  525. // Otherwise, this is background...
  526. else
  527. $bmp .= chr($background_color);
  528. }
  529. }
  530. $bmp = gzcompress($bmp, 9);
  531. // Output the basic signature first of all.
  532. $out = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
  533. // Now, we want the header...
  534. $out .= "\x00\x00\x00\x0D";
  535. $tmp = 'IHDR' . pack('N', (int) $this->header->m_nWidth) . pack('N', (int) $this->header->m_nHeight) . "\x08\x03\x00\x00\x00";
  536. $out .= $tmp . pack('N', smf_crc32($tmp));
  537. // The palette, assuming we have one to speak of...
  538. if ($colors > 0)
  539. {
  540. $out .= pack('N', (int) $colors * 3);
  541. $tmp = 'PLTE' . $pal;
  542. $out .= $tmp . pack('N', smf_crc32($tmp));
  543. }
  544. // Do we have any transparency we want to make available?
  545. if ($this->image->m_bTrans && $colors > 0)
  546. {
  547. $out .= pack('N', (int) $colors);
  548. $tmp = 'tRNS';
  549. // Stick each color on - full transparency or none.
  550. for ($i = 0; $i < $colors; $i++)
  551. $tmp .= $i == $this->image->m_nTrans ? "\x00" : "\xFF";
  552. $out .= $tmp . pack('N', smf_crc32($tmp));
  553. }
  554. // Here's the data itself!
  555. $out .= pack('N', strlen($bmp));
  556. $tmp = 'IDAT' . $bmp;
  557. $out .= $tmp . pack('N', smf_crc32($tmp));
  558. // EOF marker...
  559. $out .= "\x00\x00\x00\x00" . 'IEND' . "\xAE\x42\x60\x82";
  560. return $out;
  561. }
  562. }
  563. // 64-bit only functions?
  564. if (!function_exists('smf_crc32'))
  565. {
  566. require_once $sourcedir . '/Subs-Compat.php';
  567. }
  568. ?>