PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/source/plugin/mobile/qrcode.class.php

https://github.com/kuaileshike/upload
PHP | 2673 lines | 2117 code | 550 blank | 6 comment | 432 complexity | a84b00a45d73dd25f04e27a2d966fd43 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: qrcode.class.php 27464 2012-02-01 09:12:37Z monkey $
  7. */
  8. define('QR_MODE_NUL', -1);
  9. define('QR_MODE_NUM', 0);
  10. define('QR_MODE_AN', 1);
  11. define('QR_MODE_8', 2);
  12. define('QR_MODE_KANJI', 3);
  13. define('QR_MODE_STRUCTURE', 4);
  14. define('QR_ECLEVEL_L', 0);
  15. define('QR_ECLEVEL_M', 1);
  16. define('QR_ECLEVEL_Q', 2);
  17. define('QR_ECLEVEL_H', 3);
  18. define('QR_FORMAT_TEXT', 0);
  19. define('QR_FORMAT_PNG', 1);
  20. class qrstr {
  21. public static function set(&$srctab, $x, $y, $repl, $replLen = false) {
  22. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false) ? substr($repl, 0, $replLen) : $repl, $x, ($replLen !== false) ? $replLen : strlen($repl));
  23. }
  24. }
  25. define('QR_CACHEABLE', false);
  26. define('QR_CACHE_DIR', false);
  27. define('QR_LOG_DIR', false);
  28. define('QR_FIND_BEST_MASK', true);
  29. define('QR_FIND_FROM_RANDOM', 2);
  30. define('QR_DEFAULT_MASK', 2);
  31. define('QR_PNG_MAXIMUM_SIZE', 1024);
  32. class QRtools {
  33. public static function binarize($frame) {
  34. $len = count($frame);
  35. foreach ($frame as &$frameLine) {
  36. for ($i = 0; $i < $len; $i++) {
  37. $frameLine[$i] = (ord($frameLine[$i]) & 1) ? '1' : '0';
  38. }
  39. }
  40. return $frame;
  41. }
  42. public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037') {
  43. $barcode_array = array();
  44. if (!is_array($mode))
  45. $mode = explode(',', $mode);
  46. $eccLevel = 'L';
  47. if (count($mode) > 1) {
  48. $eccLevel = $mode[1];
  49. }
  50. $qrTab = QRcode::text($code, false, $eccLevel);
  51. $size = count($qrTab);
  52. $barcode_array['num_rows'] = $size;
  53. $barcode_array['num_cols'] = $size;
  54. $barcode_array['bcode'] = array();
  55. foreach ($qrTab as $line) {
  56. $arrAdd = array();
  57. foreach (str_split($line) as $char)
  58. $arrAdd[] = ($char == '1') ? 1 : 0;
  59. $barcode_array['bcode'][] = $arrAdd;
  60. }
  61. return $barcode_array;
  62. }
  63. public static function clearCache() {
  64. self::$frames = array();
  65. }
  66. public static function buildCache() {
  67. QRtools::markTime('before_build_cache');
  68. $mask = new QRmask();
  69. for ($a = 1; $a <= QRSPEC_VERSION_MAX; $a++) {
  70. $frame = QRspec::newFrame($a);
  71. if (QR_IMAGE) {
  72. $fileName = QR_CACHE_DIR . 'frame_' . $a . '.png';
  73. QRimage::png(self::binarize($frame), $fileName, 1, 0);
  74. }
  75. $width = count($frame);
  76. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  77. for ($maskNo = 0; $maskNo < 8; $maskNo++)
  78. $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
  79. }
  80. QRtools::markTime('after_build_cache');
  81. }
  82. public static function log($outfile, $err) {
  83. if (QR_LOG_DIR !== false) {
  84. if ($err != '') {
  85. if ($outfile !== false) {
  86. file_put_contents(QR_LOG_DIR . basename($outfile) . '-errors.txt', date('Y-m-d H:i:s') . ': ' . $err, FILE_APPEND);
  87. } else {
  88. file_put_contents(QR_LOG_DIR . 'errors.txt', date('Y-m-d H:i:s') . ': ' . $err, FILE_APPEND);
  89. }
  90. }
  91. }
  92. }
  93. public static function dumpMask($frame) {
  94. $width = count($frame);
  95. for ($y = 0; $y < $width; $y++) {
  96. for ($x = 0; $x < $width; $x++) {
  97. echo ord($frame[$y][$x]) . ',';
  98. }
  99. }
  100. }
  101. public static function markTime($markerId) {
  102. list($usec, $sec) = explode(" ", microtime());
  103. $time = ((float) $usec + (float) $sec);
  104. if (!isset($GLOBALS['qr_time_bench']))
  105. $GLOBALS['qr_time_bench'] = array();
  106. $GLOBALS['qr_time_bench'][$markerId] = $time;
  107. }
  108. public static function timeBenchmark() {
  109. self::markTime('finish');
  110. $lastTime = 0;
  111. $startTime = 0;
  112. $p = 0;
  113. echo '<table cellpadding="3" cellspacing="1">
  114. <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead>
  115. <tbody>';
  116. foreach ($GLOBALS['qr_time_bench'] as $markerId => $thisTime) {
  117. if ($p > 0) {
  118. echo '<tr><th style="text-align:right">till ' . $markerId . ': </th><td>' . number_format($thisTime - $lastTime, 6) . 's</td></tr>';
  119. } else {
  120. $startTime = $thisTime;
  121. }
  122. $p++;
  123. $lastTime = $thisTime;
  124. }
  125. echo '</tbody><tfoot>
  126. <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>' . number_format($lastTime - $startTime, 6) . 's</td></tr>
  127. </tfoot>
  128. </table>';
  129. }
  130. }
  131. QRtools::markTime('start');
  132. define('QRSPEC_VERSION_MAX', 40);
  133. define('QRSPEC_WIDTH_MAX', 177);
  134. define('QRCAP_WIDTH', 0);
  135. define('QRCAP_WORDS', 1);
  136. define('QRCAP_REMINDER', 2);
  137. define('QRCAP_EC', 3);
  138. class QRspec {
  139. public static $capacity = array(
  140. array(0, 0, 0, array(0, 0, 0, 0)),
  141. array(21, 26, 0, array(7, 10, 13, 17)),
  142. array(25, 44, 7, array(10, 16, 22, 28)),
  143. array(29, 70, 7, array(15, 26, 36, 44)),
  144. array(33, 100, 7, array(20, 36, 52, 64)),
  145. array(37, 134, 7, array(26, 48, 72, 88)),
  146. array(41, 172, 7, array(36, 64, 96, 112)),
  147. array(45, 196, 0, array(40, 72, 108, 130)),
  148. array(49, 242, 0, array(48, 88, 132, 156)),
  149. array(53, 292, 0, array(60, 110, 160, 192)),
  150. array(57, 346, 0, array(72, 130, 192, 224)),
  151. array(61, 404, 0, array(80, 150, 224, 264)),
  152. array(65, 466, 0, array(96, 176, 260, 308)),
  153. array(69, 532, 0, array(104, 198, 288, 352)),
  154. array(73, 581, 3, array(120, 216, 320, 384)),
  155. array(77, 655, 3, array(132, 240, 360, 432)),
  156. array(81, 733, 3, array(144, 280, 408, 480)),
  157. array(85, 815, 3, array(168, 308, 448, 532)),
  158. array(89, 901, 3, array(180, 338, 504, 588)),
  159. array(93, 991, 3, array(196, 364, 546, 650)),
  160. array(97, 1085, 3, array(224, 416, 600, 700)),
  161. array(101, 1156, 4, array(224, 442, 644, 750)),
  162. array(105, 1258, 4, array(252, 476, 690, 816)),
  163. array(109, 1364, 4, array(270, 504, 750, 900)),
  164. array(113, 1474, 4, array(300, 560, 810, 960)),
  165. array(117, 1588, 4, array(312, 588, 870, 1050)),
  166. array(121, 1706, 4, array(336, 644, 952, 1110)),
  167. array(125, 1828, 4, array(360, 700, 1020, 1200)),
  168. array(129, 1921, 3, array(390, 728, 1050, 1260)),
  169. array(133, 2051, 3, array(420, 784, 1140, 1350)),
  170. array(137, 2185, 3, array(450, 812, 1200, 1440)),
  171. array(141, 2323, 3, array(480, 868, 1290, 1530)),
  172. array(145, 2465, 3, array(510, 924, 1350, 1620)),
  173. array(149, 2611, 3, array(540, 980, 1440, 1710)),
  174. array(153, 2761, 3, array(570, 1036, 1530, 1800)),
  175. array(157, 2876, 0, array(570, 1064, 1590, 1890)),
  176. array(161, 3034, 0, array(600, 1120, 1680, 1980)),
  177. array(165, 3196, 0, array(630, 1204, 1770, 2100)),
  178. array(169, 3362, 0, array(660, 1260, 1860, 2220)),
  179. array(173, 3532, 0, array(720, 1316, 1950, 2310)),
  180. array(177, 3706, 0, array(750, 1372, 2040, 2430))
  181. );
  182. public static function getDataLength($version, $level) {
  183. return self::$capacity[$version][QRCAP_WORDS] - self::$capacity[$version][QRCAP_EC][$level];
  184. }
  185. public static function getECCLength($version, $level) {
  186. return self::$capacity[$version][QRCAP_EC][$level];
  187. }
  188. public static function getWidth($version) {
  189. return self::$capacity[$version][QRCAP_WIDTH];
  190. }
  191. public static function getRemainder($version) {
  192. return self::$capacity[$version][QRCAP_REMINDER];
  193. }
  194. public static function getMinimumVersion($size, $level) {
  195. for ($i = 1; $i <= QRSPEC_VERSION_MAX; $i++) {
  196. $words = self::$capacity[$i][QRCAP_WORDS] - self::$capacity[$i][QRCAP_EC][$level];
  197. if ($words >= $size)
  198. return $i;
  199. }
  200. return -1;
  201. }
  202. public static $lengthTableBits = array(
  203. array(10, 12, 14),
  204. array(9, 11, 13),
  205. array(8, 16, 16),
  206. array(8, 10, 12)
  207. );
  208. public static function lengthIndicator($mode, $version) {
  209. if ($mode == QR_MODE_STRUCTURE)
  210. return 0;
  211. if ($version <= 9) {
  212. $l = 0;
  213. } else if ($version <= 26) {
  214. $l = 1;
  215. } else {
  216. $l = 2;
  217. }
  218. return self::$lengthTableBits[$mode][$l];
  219. }
  220. public static function maximumWords($mode, $version) {
  221. if ($mode == QR_MODE_STRUCTURE)
  222. return 3;
  223. if ($version <= 9) {
  224. $l = 0;
  225. } else if ($version <= 26) {
  226. $l = 1;
  227. } else {
  228. $l = 2;
  229. }
  230. $bits = self::$lengthTableBits[$mode][$l];
  231. $words = (1 << $bits) - 1;
  232. if ($mode == QR_MODE_KANJI) {
  233. $words *= 2;
  234. }
  235. return $words;
  236. }
  237. public static $eccTable = array(
  238. array(array(0, 0), array(0, 0), array(0, 0), array(0, 0)),
  239. array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)),
  240. array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)),
  241. array(array(1, 0), array(1, 0), array(2, 0), array(2, 0)),
  242. array(array(1, 0), array(2, 0), array(2, 0), array(4, 0)),
  243. array(array(1, 0), array(2, 0), array(2, 2), array(2, 2)),
  244. array(array(2, 0), array(4, 0), array(4, 0), array(4, 0)),
  245. array(array(2, 0), array(4, 0), array(2, 4), array(4, 1)),
  246. array(array(2, 0), array(2, 2), array(4, 2), array(4, 2)),
  247. array(array(2, 0), array(3, 2), array(4, 4), array(4, 4)),
  248. array(array(2, 2), array(4, 1), array(6, 2), array(6, 2)),
  249. array(array(4, 0), array(1, 4), array(4, 4), array(3, 8)),
  250. array(array(2, 2), array(6, 2), array(4, 6), array(7, 4)),
  251. array(array(4, 0), array(8, 1), array(8, 4), array(12, 4)),
  252. array(array(3, 1), array(4, 5), array(11, 5), array(11, 5)),
  253. array(array(5, 1), array(5, 5), array(5, 7), array(11, 7)),
  254. array(array(5, 1), array(7, 3), array(15, 2), array(3, 13)),
  255. array(array(1, 5), array(10, 1), array(1, 15), array(2, 17)),
  256. array(array(5, 1), array(9, 4), array(17, 1), array(2, 19)),
  257. array(array(3, 4), array(3, 11), array(17, 4), array(9, 16)),
  258. array(array(3, 5), array(3, 13), array(15, 5), array(15, 10)),
  259. array(array(4, 4), array(17, 0), array(17, 6), array(19, 6)),
  260. array(array(2, 7), array(17, 0), array(7, 16), array(34, 0)),
  261. array(array(4, 5), array(4, 14), array(11, 14), array(16, 14)),
  262. array(array(6, 4), array(6, 14), array(11, 16), array(30, 2)),
  263. array(array(8, 4), array(8, 13), array(7, 22), array(22, 13)),
  264. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)),
  265. array(array(8, 4), array(22, 3), array(8, 26), array(12, 28)),
  266. array(array(3, 10), array(3, 23), array(4, 31), array(11, 31)),
  267. array(array(7, 7), array(21, 7), array(1, 37), array(19, 26)),
  268. array(array(5, 10), array(19, 10), array(15, 25), array(23, 25)),
  269. array(array(13, 3), array(2, 29), array(42, 1), array(23, 28)),
  270. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)),
  271. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)),
  272. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)),
  273. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)),
  274. array(array(6, 14), array(6, 34), array(46, 10), array(2, 64)),
  275. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)),
  276. array(array(4, 18), array(13, 32), array(48, 14), array(42, 32)),
  277. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)),
  278. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)),
  279. );
  280. public static function getEccSpec($version, $level, array &$spec) {
  281. if (count($spec) < 5) {
  282. $spec = array(0, 0, 0, 0, 0);
  283. }
  284. $b1 = self::$eccTable[$version][$level][0];
  285. $b2 = self::$eccTable[$version][$level][1];
  286. $data = self::getDataLength($version, $level);
  287. $ecc = self::getECCLength($version, $level);
  288. if ($b2 == 0) {
  289. $spec[0] = $b1;
  290. $spec[1] = (int) ($data / $b1);
  291. $spec[2] = (int) ($ecc / $b1);
  292. $spec[3] = 0;
  293. $spec[4] = 0;
  294. } else {
  295. $spec[0] = $b1;
  296. $spec[1] = (int) ($data / ($b1 + $b2));
  297. $spec[2] = (int) ($ecc / ($b1 + $b2));
  298. $spec[3] = $b2;
  299. $spec[4] = $spec[1] + 1;
  300. }
  301. }
  302. public static $alignmentPattern = array(
  303. array(0, 0),
  304. array(0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0),
  305. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50),
  306. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48),
  307. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62),
  308. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58),
  309. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52),
  310. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54),
  311. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58),
  312. );
  313. public static function putAlignmentMarker(array &$frame, $ox, $oy) {
  314. $finder = array(
  315. "\xa1\xa1\xa1\xa1\xa1",
  316. "\xa1\xa0\xa0\xa0\xa1",
  317. "\xa1\xa0\xa1\xa0\xa1",
  318. "\xa1\xa0\xa0\xa0\xa1",
  319. "\xa1\xa1\xa1\xa1\xa1"
  320. );
  321. $yStart = $oy - 2;
  322. $xStart = $ox - 2;
  323. for ($y = 0; $y < 5; $y++) {
  324. QRstr::set($frame, $xStart, $yStart + $y, $finder[$y]);
  325. }
  326. }
  327. public static function putAlignmentPattern($version, &$frame, $width) {
  328. if ($version < 2)
  329. return;
  330. $d = self::$alignmentPattern[$version][1] - self::$alignmentPattern[$version][0];
  331. if ($d < 0) {
  332. $w = 2;
  333. } else {
  334. $w = (int) (($width - self::$alignmentPattern[$version][0]) / $d + 2);
  335. }
  336. if ($w * $w - 3 == 1) {
  337. $x = self::$alignmentPattern[$version][0];
  338. $y = self::$alignmentPattern[$version][0];
  339. self::putAlignmentMarker($frame, $x, $y);
  340. return;
  341. }
  342. $cx = self::$alignmentPattern[$version][0];
  343. for ($x = 1; $x < $w - 1; $x++) {
  344. self::putAlignmentMarker($frame, 6, $cx);
  345. self::putAlignmentMarker($frame, $cx, 6);
  346. $cx += $d;
  347. }
  348. $cy = self::$alignmentPattern[$version][0];
  349. for ($y = 0; $y < $w - 1; $y++) {
  350. $cx = self::$alignmentPattern[$version][0];
  351. for ($x = 0; $x < $w - 1; $x++) {
  352. self::putAlignmentMarker($frame, $cx, $cy);
  353. $cx += $d;
  354. }
  355. $cy += $d;
  356. }
  357. }
  358. public static $versionPattern = array(
  359. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
  360. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
  361. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
  362. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
  363. 0x27541, 0x28c69
  364. );
  365. public static function getVersionPattern($version) {
  366. if ($version < 7 || $version > QRSPEC_VERSION_MAX)
  367. return 0;
  368. return self::$versionPattern[$version - 7];
  369. }
  370. public static $formatInfo = array(
  371. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
  372. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
  373. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
  374. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
  375. );
  376. public static function getFormatInfo($mask, $level) {
  377. if ($mask < 0 || $mask > 7)
  378. return 0;
  379. if ($level < 0 || $level > 3)
  380. return 0;
  381. return self::$formatInfo[$level][$mask];
  382. }
  383. public static $frames = array();
  384. public static function putFinderPattern(&$frame, $ox, $oy) {
  385. $finder = array(
  386. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  387. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  388. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  389. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  390. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  391. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  392. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  393. );
  394. for ($y = 0; $y < 7; $y++) {
  395. QRstr::set($frame, $ox, $oy + $y, $finder[$y]);
  396. }
  397. }
  398. public static function createFrame($version) {
  399. $width = self::$capacity[$version][QRCAP_WIDTH];
  400. $frameLine = str_repeat("\0", $width);
  401. $frame = array_fill(0, $width, $frameLine);
  402. self::putFinderPattern($frame, 0, 0);
  403. self::putFinderPattern($frame, $width - 7, 0);
  404. self::putFinderPattern($frame, 0, $width - 7);
  405. $yOffset = $width - 7;
  406. for ($y = 0; $y < 7; $y++) {
  407. $frame[$y][7] = "\xc0";
  408. $frame[$y][$width - 8] = "\xc0";
  409. $frame[$yOffset][7] = "\xc0";
  410. $yOffset++;
  411. }
  412. $setPattern = str_repeat("\xc0", 8);
  413. QRstr::set($frame, 0, 7, $setPattern);
  414. QRstr::set($frame, $width - 8, 7, $setPattern);
  415. QRstr::set($frame, 0, $width - 8, $setPattern);
  416. $setPattern = str_repeat("\x84", 9);
  417. QRstr::set($frame, 0, 8, $setPattern);
  418. QRstr::set($frame, $width - 8, 8, $setPattern, 8);
  419. $yOffset = $width - 8;
  420. for ($y = 0; $y < 8; $y++, $yOffset++) {
  421. $frame[$y][8] = "\x84";
  422. $frame[$yOffset][8] = "\x84";
  423. }
  424. for ($i = 1; $i < $width - 15; $i++) {
  425. $frame[6][7 + $i] = chr(0x90 | ($i & 1));
  426. $frame[7 + $i][6] = chr(0x90 | ($i & 1));
  427. }
  428. self::putAlignmentPattern($version, $frame, $width);
  429. if ($version >= 7) {
  430. $vinf = self::getVersionPattern($version);
  431. $v = $vinf;
  432. for ($x = 0; $x < 6; $x++) {
  433. for ($y = 0; $y < 3; $y++) {
  434. $frame[($width - 11) + $y][$x] = chr(0x88 | ($v & 1));
  435. $v = $v >> 1;
  436. }
  437. }
  438. $v = $vinf;
  439. for ($y = 0; $y < 6; $y++) {
  440. for ($x = 0; $x < 3; $x++) {
  441. $frame[$y][$x + ($width - 11)] = chr(0x88 | ($v & 1));
  442. $v = $v >> 1;
  443. }
  444. }
  445. }
  446. $frame[$width - 8][8] = "\x81";
  447. return $frame;
  448. }
  449. public static function debug($frame, $binary_mode = false) {
  450. if ($binary_mode) {
  451. foreach ($frame as &$frameLine) {
  452. $frameLine = join('<span class="m">&nbsp;&nbsp;</span>', explode('0', $frameLine));
  453. $frameLine = join('&#9608;&#9608;', explode('1', $frameLine));
  454. }
  455. ?>
  456. <style>
  457. .m { background-color: white; }
  458. </style>
  459. <?php
  460. echo '<pre><tt><br/ ><br/ ><br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  461. echo join("<br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $frame);
  462. echo '</tt></pre><br/ ><br/ ><br/ ><br/ ><br/ ><br/ >';
  463. } else {
  464. foreach ($frame as &$frameLine) {
  465. $frameLine = join('<span class="m">&nbsp;</span>', explode("\xc0", $frameLine));
  466. $frameLine = join('<span class="m">&#9618;</span>', explode("\xc1", $frameLine));
  467. $frameLine = join('<span class="p">&nbsp;</span>', explode("\xa0", $frameLine));
  468. $frameLine = join('<span class="p">&#9618;</span>', explode("\xa1", $frameLine));
  469. $frameLine = join('<span class="s">&#9671;</span>', explode("\x84", $frameLine));
  470. $frameLine = join('<span class="s">&#9670;</span>', explode("\x85", $frameLine));
  471. $frameLine = join('<span class="x">&#9762;</span>', explode("\x81", $frameLine));
  472. $frameLine = join('<span class="c">&nbsp;</span>', explode("\x90", $frameLine));
  473. $frameLine = join('<span class="c">&#9719;</span>', explode("\x91", $frameLine));
  474. $frameLine = join('<span class="f">&nbsp;</span>', explode("\x88", $frameLine));
  475. $frameLine = join('<span class="f">&#9618;</span>', explode("\x89", $frameLine));
  476. $frameLine = join('&#9830;', explode("\x01", $frameLine));
  477. $frameLine = join('&#8901;', explode("\0", $frameLine));
  478. }
  479. ?>
  480. <style>
  481. .p { background-color: yellow; }
  482. .m { background-color: #00FF00; }
  483. .s { background-color: #FF0000; }
  484. .c { background-color: aqua; }
  485. .x { background-color: pink; }
  486. .f { background-color: gold; }
  487. </style>
  488. <?php
  489. echo "<pre><tt>";
  490. echo join("<br/ >", $frame);
  491. echo "</tt></pre>";
  492. }
  493. }
  494. public static function serial($frame) {
  495. return gzcompress(join("\n", $frame), 9);
  496. }
  497. public static function unserial($code) {
  498. return explode("\n", gzuncompress($code));
  499. }
  500. public static function newFrame($version) {
  501. if ($version < 1 || $version > QRSPEC_VERSION_MAX)
  502. return null;
  503. if (!isset(self::$frames[$version])) {
  504. $fileName = QR_CACHE_DIR . 'frame_' . $version . '.dat';
  505. if (QR_CACHEABLE) {
  506. if (file_exists($fileName)) {
  507. self::$frames[$version] = self::unserial(file_get_contents($fileName));
  508. } else {
  509. self::$frames[$version] = self::createFrame($version);
  510. file_put_contents($fileName, self::serial(self::$frames[$version]));
  511. }
  512. } else {
  513. self::$frames[$version] = self::createFrame($version);
  514. }
  515. }
  516. if (is_null(self::$frames[$version]))
  517. return null;
  518. return self::$frames[$version];
  519. }
  520. public static function rsBlockNum($spec) {
  521. return $spec[0] + $spec[3];
  522. }
  523. public static function rsBlockNum1($spec) {
  524. return $spec[0];
  525. }
  526. public static function rsDataCodes1($spec) {
  527. return $spec[1];
  528. }
  529. public static function rsEccCodes1($spec) {
  530. return $spec[2];
  531. }
  532. public static function rsBlockNum2($spec) {
  533. return $spec[3];
  534. }
  535. public static function rsDataCodes2($spec) {
  536. return $spec[4];
  537. }
  538. public static function rsEccCodes2($spec) {
  539. return $spec[2];
  540. }
  541. public static function rsDataLength($spec) {
  542. return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
  543. }
  544. public static function rsEccLength($spec) {
  545. return ($spec[0] + $spec[3]) * $spec[2];
  546. }
  547. }
  548. define('QR_IMAGE', true);
  549. class QRimage {
  550. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4, $saveandprint = FALSE) {
  551. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  552. if ($filename === false) {
  553. Header("Content-type: image/png");
  554. ImagePng($image);
  555. } else {
  556. if ($saveandprint === TRUE) {
  557. ImagePng($image, $filename);
  558. header("Content-type: image/png");
  559. ImagePng($image);
  560. } else {
  561. ImagePng($image, $filename);
  562. }
  563. }
  564. ImageDestroy($image);
  565. }
  566. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) {
  567. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  568. if ($filename === false) {
  569. Header("Content-type: image/jpeg");
  570. ImageJpeg($image, null, $q);
  571. } else {
  572. ImageJpeg($image, $filename, $q);
  573. }
  574. ImageDestroy($image);
  575. }
  576. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) {
  577. $h = count($frame);
  578. $w = strlen($frame[0]);
  579. $imgW = $w + 2 * $outerFrame;
  580. $imgH = $h + 2 * $outerFrame;
  581. $base_image = ImageCreate($imgW, $imgH);
  582. $col[0] = ImageColorAllocate($base_image, 255, 255, 255);
  583. $col[1] = ImageColorAllocate($base_image, 0, 0, 0);
  584. imagefill($base_image, 0, 0, $col[0]);
  585. for ($y = 0; $y < $h; $y++) {
  586. for ($x = 0; $x < $w; $x++) {
  587. if ($frame[$y][$x] == '1') {
  588. ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]);
  589. }
  590. }
  591. }
  592. $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  593. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  594. ImageDestroy($base_image);
  595. return $target_image;
  596. }
  597. }
  598. define('STRUCTURE_HEADER_BITS', 20);
  599. define('MAX_STRUCTURED_SYMBOLS', 16);
  600. class QRinputItem {
  601. public $mode;
  602. public $size;
  603. public $data;
  604. public $bstream;
  605. public function __construct($mode, $size, $data, $bstream = null) {
  606. $setData = array_slice($data, 0, $size);
  607. if (count($setData) < $size) {
  608. $setData = array_merge($setData, array_fill(0, $size - count($setData), 0));
  609. }
  610. if (!QRinput::check($mode, $size, $setData)) {
  611. throw new Exception('Error m:' . $mode . ',s:' . $size . ',d:' . join(',', $setData));
  612. return null;
  613. }
  614. $this->mode = $mode;
  615. $this->size = $size;
  616. $this->data = $setData;
  617. $this->bstream = $bstream;
  618. }
  619. public function encodeModeNum($version) {
  620. try {
  621. $words = (int) ($this->size / 3);
  622. $bs = new QRbitstream();
  623. $val = 0x1;
  624. $bs->appendNum(4, $val);
  625. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size);
  626. for ($i = 0; $i < $words; $i++) {
  627. $val = (ord($this->data[$i * 3]) - ord('0')) * 100;
  628. $val += (ord($this->data[$i * 3 + 1]) - ord('0')) * 10;
  629. $val += (ord($this->data[$i * 3 + 2]) - ord('0'));
  630. $bs->appendNum(10, $val);
  631. }
  632. if ($this->size - $words * 3 == 1) {
  633. $val = ord($this->data[$words * 3]) - ord('0');
  634. $bs->appendNum(4, $val);
  635. } else if ($this->size - $words * 3 == 2) {
  636. $val = (ord($this->data[$words * 3]) - ord('0')) * 10;
  637. $val += (ord($this->data[$words * 3 + 1]) - ord('0'));
  638. $bs->appendNum(7, $val);
  639. }
  640. $this->bstream = $bs;
  641. return 0;
  642. } catch (Exception $e) {
  643. return -1;
  644. }
  645. }
  646. public function encodeModeAn($version) {
  647. try {
  648. $words = (int) ($this->size / 2);
  649. $bs = new QRbitstream();
  650. $bs->appendNum(4, 0x02);
  651. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size);
  652. for ($i = 0; $i < $words; $i++) {
  653. $val = (int) QRinput::lookAnTable(ord($this->data[$i * 2])) * 45;
  654. $val += (int) QRinput::lookAnTable(ord($this->data[$i * 2 + 1]));
  655. $bs->appendNum(11, $val);
  656. }
  657. if ($this->size & 1) {
  658. $val = QRinput::lookAnTable(ord($this->data[$words * 2]));
  659. $bs->appendNum(6, $val);
  660. }
  661. $this->bstream = $bs;
  662. return 0;
  663. } catch (Exception $e) {
  664. return -1;
  665. }
  666. }
  667. public function encodeMode8($version) {
  668. try {
  669. $bs = new QRbitstream();
  670. $bs->appendNum(4, 0x4);
  671. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size);
  672. for ($i = 0; $i < $this->size; $i++) {
  673. $bs->appendNum(8, ord($this->data[$i]));
  674. }
  675. $this->bstream = $bs;
  676. return 0;
  677. } catch (Exception $e) {
  678. return -1;
  679. }
  680. }
  681. public function encodeModeKanji($version) {
  682. try {
  683. $bs = new QRbitrtream();
  684. $bs->appendNum(4, 0x8);
  685. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int) ($this->size / 2));
  686. for ($i = 0; $i < $this->size; $i+=2) {
  687. $val = (ord($this->data[$i]) << 8) | ord($this->data[$i + 1]);
  688. if ($val <= 0x9ffc) {
  689. $val -= 0x8140;
  690. } else {
  691. $val -= 0xc140;
  692. }
  693. $h = ($val >> 8) * 0xc0;
  694. $val = ($val & 0xff) + $h;
  695. $bs->appendNum(13, $val);
  696. }
  697. $this->bstream = $bs;
  698. return 0;
  699. } catch (Exception $e) {
  700. return -1;
  701. }
  702. }
  703. public function encodeModeStructure() {
  704. try {
  705. $bs = new QRbitstream();
  706. $bs->appendNum(4, 0x03);
  707. $bs->appendNum(4, ord($this->data[1]) - 1);
  708. $bs->appendNum(4, ord($this->data[0]) - 1);
  709. $bs->appendNum(8, ord($this->data[2]));
  710. $this->bstream = $bs;
  711. return 0;
  712. } catch (Exception $e) {
  713. return -1;
  714. }
  715. }
  716. public function estimateBitStreamSizeOfEntry($version) {
  717. $bits = 0;
  718. if ($version == 0)
  719. $version = 1;
  720. switch ($this->mode) {
  721. case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size);
  722. break;
  723. case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size);
  724. break;
  725. case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size);
  726. break;
  727. case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);
  728. break;
  729. case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS;
  730. default:
  731. return 0;
  732. }
  733. $l = QRspec::lengthIndicator($this->mode, $version);
  734. $m = 1 << $l;
  735. $num = (int) (($this->size + $m - 1) / $m);
  736. $bits += $num * (4 + $l);
  737. return $bits;
  738. }
  739. public function encodeBitStream($version) {
  740. try {
  741. unset($this->bstream);
  742. $words = QRspec::maximumWords($this->mode, $version);
  743. if ($this->size > $words) {
  744. $st1 = new QRinputItem($this->mode, $words, $this->data);
  745. $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words));
  746. $st1->encodeBitStream($version);
  747. $st2->encodeBitStream($version);
  748. $this->bstream = new QRbitstream();
  749. $this->bstream->append($st1->bstream);
  750. $this->bstream->append($st2->bstream);
  751. unset($st1);
  752. unset($st2);
  753. } else {
  754. $ret = 0;
  755. switch ($this->mode) {
  756. case QR_MODE_NUM: $ret = $this->encodeModeNum($version);
  757. break;
  758. case QR_MODE_AN: $ret = $this->encodeModeAn($version);
  759. break;
  760. case QR_MODE_8: $ret = $this->encodeMode8($version);
  761. break;
  762. case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version);
  763. break;
  764. case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure();
  765. break;
  766. default:
  767. break;
  768. }
  769. if ($ret < 0)
  770. return -1;
  771. }
  772. return $this->bstream->size();
  773. } catch (Exception $e) {
  774. return -1;
  775. }
  776. }
  777. }
  778. ;
  779. class QRinput {
  780. public $items;
  781. private $version;
  782. private $level;
  783. public function __construct($version = 0, $level = QR_ECLEVEL_L) {
  784. if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
  785. throw new Exception('Invalid version no');
  786. return NULL;
  787. }
  788. $this->version = $version;
  789. $this->level = $level;
  790. }
  791. public function getVersion() {
  792. return $this->version;
  793. }
  794. public function setVersion($version) {
  795. if ($version < 0 || $version > QRSPEC_VERSION_MAX) {
  796. throw new Exception('Invalid version no');
  797. return -1;
  798. }
  799. $this->version = $version;
  800. return 0;
  801. }
  802. public function getErrorCorrectionLevel() {
  803. return $this->level;
  804. }
  805. public function setErrorCorrectionLevel($level) {
  806. if ($level > QR_ECLEVEL_H) {
  807. throw new Exception('Invalid ECLEVEL');
  808. return -1;
  809. }
  810. $this->level = $level;
  811. return 0;
  812. }
  813. public function appendEntry(QRinputItem $entry) {
  814. $this->items[] = $entry;
  815. }
  816. public function append($mode, $size, $data) {
  817. try {
  818. $entry = new QRinputItem($mode, $size, $data);
  819. $this->items[] = $entry;
  820. return 0;
  821. } catch (Exception $e) {
  822. return -1;
  823. }
  824. }
  825. public function insertStructuredAppendHeader($size, $index, $parity) {
  826. if ($size > MAX_STRUCTURED_SYMBOLS) {
  827. throw new Exception('insertStructuredAppendHeader wrong size');
  828. }
  829. if ($index <= 0 || $index > MAX_STRUCTURED_SYMBOLS) {
  830. throw new Exception('insertStructuredAppendHeader wrong index');
  831. }
  832. $buf = array($size, $index, $parity);
  833. try {
  834. $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf);
  835. array_unshift($this->items, $entry);
  836. return 0;
  837. } catch (Exception $e) {
  838. return -1;
  839. }
  840. }
  841. public function calcParity() {
  842. $parity = 0;
  843. foreach ($this->items as $item) {
  844. if ($item->mode != QR_MODE_STRUCTURE) {
  845. for ($i = $item->size - 1; $i >= 0; $i--) {
  846. $parity ^= $item->data[$i];
  847. }
  848. }
  849. }
  850. return $parity;
  851. }
  852. public static function checkModeNum($size, $data) {
  853. for ($i = 0; $i < $size; $i++) {
  854. if ((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))) {
  855. return false;
  856. }
  857. }
  858. return true;
  859. }
  860. public static function estimateBitsModeNum($size) {
  861. $w = (int) $size / 3;
  862. $bits = $w * 10;
  863. switch ($size - $w * 3) {
  864. case 1:
  865. $bits += 4;
  866. break;
  867. case 2:
  868. $bits += 7;
  869. break;
  870. default:
  871. break;
  872. }
  873. return $bits;
  874. }
  875. public static $anTable = array(
  876. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  877. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  878. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  879. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  880. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  881. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  882. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  883. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  884. );
  885. public static function lookAnTable($c) {
  886. return (($c > 127) ? -1 : self::$anTable[$c]);
  887. }
  888. public static function checkModeAn($size, $data) {
  889. for ($i = 0; $i < $size; $i++) {
  890. if (self::lookAnTable(ord($data[$i])) == -1) {
  891. return false;
  892. }
  893. }
  894. return true;
  895. }
  896. public static function estimateBitsModeAn($size) {
  897. $w = (int) ($size / 2);
  898. $bits = $w * 11;
  899. if ($size & 1) {
  900. $bits += 6;
  901. }
  902. return $bits;
  903. }
  904. public static function estimateBitsMode8($size) {
  905. return $size * 8;
  906. }
  907. public function estimateBitsModeKanji($size) {
  908. return (int) (($size / 2) * 13);
  909. }
  910. public static function checkModeKanji($size, $data) {
  911. if ($size & 1)
  912. return false;
  913. for ($i = 0; $i < $size; $i+=2) {
  914. $val = (ord($data[$i]) << 8) | ord($data[$i + 1]);
  915. if ($val < 0x8140
  916. || ($val > 0x9ffc && $val < 0xe040)
  917. || $val > 0xebbf) {
  918. return false;
  919. }
  920. }
  921. return true;
  922. }
  923. public static function check($mode, $size, $data) {
  924. if ($size <= 0)
  925. return false;
  926. switch ($mode) {
  927. case QR_MODE_NUM: return self::checkModeNum($size, $data);
  928. break;
  929. case QR_MODE_AN: return self::checkModeAn($size, $data);
  930. break;
  931. case QR_MODE_KANJI: return self::checkModeKanji($size, $data);
  932. break;
  933. case QR_MODE_8: return true;
  934. break;
  935. case QR_MODE_STRUCTURE: return true;
  936. break;
  937. default:
  938. break;
  939. }
  940. return false;
  941. }
  942. public function estimateBitStreamSize($version) {
  943. $bits = 0;
  944. foreach ($this->items as $item) {
  945. $bits += $item->estimateBitStreamSizeOfEntry($version);
  946. }
  947. return $bits;
  948. }
  949. public function estimateVersion() {
  950. $version = 0;
  951. $prev = 0;
  952. do {
  953. $prev = $version;
  954. $bits = $this->estimateBitStreamSize($prev);
  955. $version = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level);
  956. if ($version < 0) {
  957. return -1;
  958. }
  959. } while ($version > $prev);
  960. return $version;
  961. }
  962. public static function lengthOfCode($mode, $version, $bits) {
  963. $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version);
  964. switch ($mode) {
  965. case QR_MODE_NUM:
  966. $chunks = (int) ($payload / 10);
  967. $remain = $payload - $chunks * 10;
  968. $size = $chunks * 3;
  969. if ($remain >= 7) {
  970. $size += 2;
  971. } else if ($remain >= 4) {
  972. $size += 1;
  973. }
  974. break;
  975. case QR_MODE_AN:
  976. $chunks = (int) ($payload / 11);
  977. $remain = $payload - $chunks * 11;
  978. $size = $chunks * 2;
  979. if ($remain >= 6)
  980. $size++;
  981. break;
  982. case QR_MODE_8:
  983. $size = (int) ($payload / 8);
  984. break;
  985. case QR_MODE_KANJI:
  986. $size = (int) (($payload / 13) * 2);
  987. break;
  988. case QR_MODE_STRUCTURE:
  989. $size = (int) ($payload / 8);
  990. break;
  991. default:
  992. $size = 0;
  993. break;
  994. }
  995. $maxsize = QRspec::maximumWords($mode, $version);
  996. if ($size < 0)
  997. $size = 0;
  998. if ($size > $maxsize)
  999. $size = $maxsize;
  1000. return $size;
  1001. }
  1002. public function createBitStream() {
  1003. $total = 0;
  1004. foreach ($this->items as $item) {
  1005. $bits = $item->encodeBitStream($this->version);
  1006. if ($bits < 0)
  1007. return -1;
  1008. $total += $bits;
  1009. }
  1010. return $total;
  1011. }
  1012. public function convertData() {
  1013. $ver = $this->estimateVersion();
  1014. if ($ver > $this->getVersion()) {
  1015. $this->setVersion($ver);
  1016. }
  1017. for (;;) {
  1018. $bits = $this->createBitStream();
  1019. if ($bits < 0)
  1020. return -1;
  1021. $ver = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level);
  1022. if ($ver < 0) {
  1023. throw new Exception('WRONG VERSION');
  1024. return -1;
  1025. } else if ($ver > $this->getVersion()) {
  1026. $this->setVersion($ver);
  1027. } else {
  1028. break;
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. public function appendPaddingBit(&$bstream) {
  1034. $bits = $bstream->size();
  1035. $maxwords = QRspec::getDataLength($this->version, $this->level);
  1036. $maxbits = $maxwords * 8;
  1037. if ($maxbits == $bits) {
  1038. return 0;
  1039. }
  1040. if ($maxbits - $bits < 5) {
  1041. return $bstream->appendNum($maxbits - $bits, 0);
  1042. }
  1043. $bits += 4;
  1044. $words = (int) (($bits + 7) / 8);
  1045. $padding = new QRbitstream();
  1046. $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
  1047. if ($ret < 0)
  1048. return $ret;
  1049. $padlen = $maxwords - $words;
  1050. if ($padlen > 0) {
  1051. $padbuf = array();
  1052. for ($i = 0; $i < $padlen; $i++) {
  1053. $padbuf[$i] = ($i & 1) ? 0x11 : 0xec;
  1054. }
  1055. $ret = $padding->appendBytes($padlen, $padbuf);
  1056. if ($ret < 0)
  1057. return $ret;
  1058. }
  1059. $ret = $bstream->append($padding);
  1060. return $ret;
  1061. }
  1062. public function mergeBitStream() {
  1063. if ($this->convertData() < 0) {
  1064. return null;
  1065. }
  1066. $bstream = new QRbitstream();
  1067. foreach ($this->items as $item) {
  1068. $ret = $bstream->append($item->bstream);
  1069. if ($ret < 0) {
  1070. return null;
  1071. }
  1072. }
  1073. return $bstream;
  1074. }
  1075. public function getBitStream() {
  1076. $bstream = $this->mergeBitStream();
  1077. if ($bstream == null) {
  1078. return null;
  1079. }
  1080. $ret = $this->appendPaddingBit($bstream);
  1081. if ($ret < 0) {
  1082. return null;
  1083. }
  1084. return $bstream;
  1085. }
  1086. public function getByteStream() {
  1087. $bstream = $this->getBitStream();
  1088. if ($bstream == null) {
  1089. return null;
  1090. }
  1091. return $bstream->toByte();
  1092. }
  1093. }
  1094. class QRbitstream {
  1095. public $data = array();
  1096. public function size() {
  1097. return count($this->data);
  1098. }
  1099. public function allocate($setLength) {
  1100. $this->data = array_fill(0, $setLength, 0);
  1101. return 0;
  1102. }
  1103. public static function newFromNum($bits, $num) {
  1104. $bstream = new QRbitstream();
  1105. $bstream->allocate($bits);
  1106. $mask = 1 << ($bits - 1);
  1107. for ($i = 0; $i < $bits; $i++) {
  1108. if ($num & $mask) {
  1109. $bstream->data[$i] = 1;
  1110. } else {
  1111. $bstream->data[$i] = 0;
  1112. }
  1113. $mask = $mask >> 1;
  1114. }
  1115. return $bstream;
  1116. }
  1117. public static function newFromBytes($size, $data) {
  1118. $bstream = new QRbitstream();
  1119. $bstream->allocate($size * 8);
  1120. $p = 0;
  1121. for ($i = 0; $i < $size; $i++) {
  1122. $mask = 0x80;
  1123. for ($j = 0; $j < 8; $j++) {
  1124. if ($data[$i] & $mask) {
  1125. $bstream->data[$p] = 1;
  1126. } else {
  1127. $bstream->data[$p] = 0;
  1128. }
  1129. $p++;
  1130. $mask = $mask >> 1;
  1131. }
  1132. }
  1133. return $bstream;
  1134. }
  1135. public function append(QRbitstream $arg) {
  1136. if (is_null($arg)) {
  1137. return -1;
  1138. }
  1139. if ($arg->size() == 0) {
  1140. return 0;
  1141. }
  1142. if ($this->size() == 0) {
  1143. $this->data = $arg->data;
  1144. return 0;
  1145. }
  1146. $this->data = array_values(array_merge($this->data, $arg->data));
  1147. return 0;
  1148. }
  1149. public function appendNum($bits, $num) {
  1150. if ($bits == 0)
  1151. return 0;
  1152. $b = QRbitstream::newFromNum($bits, $num);
  1153. if (is_null($b))
  1154. return -1;
  1155. $ret = $this->append($b);
  1156. unset($b);
  1157. return $ret;
  1158. }
  1159. public function appendBytes($size, $data) {
  1160. if ($size == 0)
  1161. return 0;
  1162. $b = QRbitstream::newFromBytes($size, $data);
  1163. if (is_null($b))
  1164. return -1;
  1165. $ret = $this->append($b);
  1166. unset($b);
  1167. return $ret;
  1168. }
  1169. public function toByte() {
  1170. $size = $this->size();
  1171. if ($size == 0) {
  1172. return array();
  1173. }
  1174. $data = array_fill(0, (int) (($size + 7) / 8), 0);
  1175. $bytes = (int) ($size / 8);
  1176. $p = 0;
  1177. for ($i = 0; $i < $bytes; $i++) {
  1178. $v = 0;
  1179. for ($j = 0; $j < 8; $j++) {
  1180. $v = $v << 1;
  1181. $v |= $this->data[$p];
  1182. $p++;
  1183. }
  1184. $data[$i] = $v;
  1185. }
  1186. if ($size & 7) {
  1187. $v = 0;
  1188. for ($j = 0; $j < ($size & 7); $j++) {
  1189. $v = $v << 1;
  1190. $v |= $this->data[$p];
  1191. $p++;
  1192. }
  1193. $data[$bytes] = $v;
  1194. }
  1195. return $data;
  1196. }
  1197. }
  1198. class QRsplit {
  1199. public $dataStr = '';
  1200. public $input;
  1201. public $modeHint;
  1202. public function __construct($dataStr, $input, $modeHint) {
  1203. $this->dataStr = $dataStr;
  1204. $this->input = $input;
  1205. $this->modeHint = $modeHint;
  1206. }
  1207. public static function isdigitat($str, $pos) {
  1208. if ($pos >= strlen($str))
  1209. return false;
  1210. return ((ord($str[$pos]) >= ord('0')) && (ord($str[$pos]) <= ord('9')));
  1211. }
  1212. public static function isalnumat($str, $pos) {
  1213. if ($pos >= strlen($str))
  1214. return false;
  1215. return (QRinput::lookAnTable(ord($str[$pos])) >= 0);
  1216. }
  1217. public function identifyMode($pos) {
  1218. if ($pos >= strlen($this->dataStr))
  1219. return QR_MODE_NUL;
  1220. $c = $this->dataStr[$pos];
  1221. if (self::isdigitat($this->dataStr, $pos)) {
  1222. return QR_MODE_NUM;
  1223. } else if (self::isalnumat($this->dataStr, $pos)) {
  1224. return QR_MODE_AN;
  1225. } else if ($this->modeHint == QR_MODE_KANJI) {
  1226. if ($pos + 1 < strlen($this->dataStr)) {
  1227. $d = $this->dataStr[$pos + 1];
  1228. $word = (ord($c) << 8) | ord($d);
  1229. if (($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
  1230. return QR_MODE_KANJI;
  1231. }
  1232. }
  1233. }
  1234. return QR_MODE_8;
  1235. }
  1236. public function eatNum() {
  1237. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1238. $p = 0;
  1239. while (self::isdigitat($this->dataStr, $p)) {
  1240. $p++;
  1241. }
  1242. $run = $p;
  1243. $mode = $this->identifyMode($p);
  1244. if ($mode == QR_MODE_8) {
  1245. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1246. + QRinput::estimateBitsMode8(1)
  1247. - QRinput::estimateBitsMode8($run + 1);
  1248. if ($dif > 0) {
  1249. return $this->eat8();
  1250. }
  1251. }
  1252. if ($mode == QR_MODE_AN) {
  1253. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1254. + QRinput::estimateBitsModeAn(1)
  1255. - QRinput::estimateBitsModeAn($run + 1);
  1256. if ($dif > 0) {
  1257. return $this->eatAn();
  1258. }
  1259. }
  1260. $ret = $this->input->append(QR_MODE_NUM, $run, str_split($this->dataStr));
  1261. if ($ret < 0)
  1262. return -1;
  1263. return $run;
  1264. }
  1265. public function eatAn() {
  1266. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1267. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1268. $p = 0;
  1269. while (self::isalnumat($this->dataStr, $p)) {
  1270. if (self::isdigitat($this->dataStr, $p)) {
  1271. $q = $p;
  1272. while (self::isdigitat($this->dataStr, $q)) {
  1273. $q++;
  1274. }
  1275. $dif = QRinput::estimateBitsModeAn($p)
  1276. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1277. - QRinput::estimateBitsModeAn($q);
  1278. if ($dif < 0) {
  1279. break;
  1280. } else {
  1281. $p = $q;
  1282. }
  1283. } else {
  1284. $p++;
  1285. }
  1286. }
  1287. $run = $p;
  1288. if (!self::isalnumat($this->dataStr, $p)) {
  1289. $dif = QRinput::estimateBitsModeAn($run) + 4 + $la
  1290. + QRinput::estimateBitsMode8(1)
  1291. - QRinput::estimateBitsMode8($run + 1);
  1292. if ($dif > 0) {
  1293. return $this->eat8();
  1294. }
  1295. }
  1296. $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr));
  1297. if ($ret < 0)
  1298. return -1;
  1299. return $run;
  1300. }
  1301. public function eatKanji() {
  1302. $p = 0;
  1303. while ($this->identifyMode($p) == QR_MODE_KANJI) {
  1304. $p += 2;
  1305. }
  1306. $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr));
  1307. if ($ret < 0)
  1308. return -1;
  1309. return $run;
  1310. }
  1311. public function eat8() {
  1312. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1313. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1314. $p = 1;
  1315. $dataStrLen = strlen($this->dataStr);
  1316. while ($p < $dataStrLen) {
  1317. $mode = $this->identifyMode($p);
  1318. if ($mode == QR_MODE_KANJI) {
  1319. break;
  1320. }
  1321. if ($mode == QR_MODE_NUM) {
  1322. $q = $p;
  1323. while (self::isdigitat($this->dataStr, $q)) {
  1324. $q++;
  1325. }
  1326. $dif = QRinput::estimateBitsMode8($p)
  1327. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1328. - QRinput::estimateBitsMode8($q);
  1329. if ($dif < 0) {
  1330. break;
  1331. } else {
  1332. $p = $q;
  1333. }
  1334. } else if ($mode == QR_MODE_AN) {
  1335. $q = $p;
  1336. while (self::isalnumat($this->dataStr, $q)) {
  1337. $q++;
  1338. }
  1339. $dif = QRinput::estimateBitsMode8($p)
  1340. + QRinput::estimateBitsModeAn($q - $p) + 4 + $la
  1341. - QRinput::estimateBitsMode8($q);
  1342. if ($dif < 0) {
  1343. break;
  1344. } else {
  1345. $p = $q;
  1346. }
  1347. } else {
  1348. $p++;
  1349. }
  1350. }
  1351. $run = $p;
  1352. $ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr));
  1353. if ($ret < 0)
  1354. return -1;
  1355. return $run;
  1356. }
  1357. public function splitString() {
  1358. while (strlen($this->dataStr) > 0) {
  1359. if ($this->dataStr == '')
  1360. return 0;
  1361. $mode = $this->identifyMode(0);
  1362. switch ($mode) {
  1363. case QR_MODE_NUM: $length = $this->eatNum();
  1364. break;
  1365. case QR_MODE_AN: $length = $this->eatAn();
  1366. break;
  1367. case QR_MODE_KANJI:
  1368. if ($hint == QR_MODE_KANJI)
  1369. $length = $this->eatKanji();
  1370. else
  1371. $length = $this->eat8();
  1372. break;
  1373. default: $length = $this->eat8();
  1374. break;
  1375. }
  1376. if ($length == 0)
  1377. return 0;
  1378. if ($length < 0)
  1379. return -1;
  1380. $this->dataStr = substr($this->dataStr, $length);
  1381. }
  1382. }
  1383. public function toUpper() {
  1384. $stringLen = strlen($this->dataStr);
  1385. $p = 0;
  1386. while ($p < $stringLen) {
  1387. $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
  1388. if ($mode == QR_MODE_KANJI) {
  1389. $p += 2;
  1390. } else {
  1391. if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
  1392. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1393. }
  1394. $p++;
  1395. }
  1396. }
  1397. return $this->dataStr;
  1398. }
  1399. public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true) {
  1400. if (is_null($string) || $string == '\0' || $string == '') {
  1401. throw new Exception('empty string!!!');
  1402. }
  1403. $split = new QRsplit($string, $input, $modeHint);
  1404. if (!$casesensitive)
  1405. $split->toUpper();
  1406. return $split->splitString();
  1407. }
  1408. }
  1409. class QRrsItem {
  1410. public $mm;
  1411. public $nn;
  1412. public $alpha_to = array();
  1413. public $index_of = array();
  1414. public $genpoly = array();
  1415. public $nroots;
  1416. public $fcr;
  1417. public $prim;
  1418. public $iprim;
  1419. public $pad;
  1420. public $gfpoly;
  1421. public function modnn($x) {
  1422. while ($x >= $this->nn) {
  1423. $x -= $this->nn;
  1424. $x = ($x >> $this->mm) + ($x & $this->nn);
  1425. }
  1426. return $x;
  1427. }
  1428. public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  1429. $rs = null;
  1430. if ($symsize < 0 || $symsize > 8)
  1431. return $rs;
  1432. if ($fcr < 0 || $fcr >= (1 << $symsize))
  1433. return $rs;
  1434. if ($prim <= 0 || $prim >= (1 << $symsize))
  1435. return $rs;
  1436. if ($nroots < 0 || $nroots >= (1 << $symsize))
  1437. return $rs;
  1438. if ($pad < 0 || $pad >= ((1 << $symsize) - 1 - $nroots))
  1439. return $rs;
  1440. $rs = new QRrsItem();
  1441. $rs->mm = $symsize;
  1442. $rs->nn = (1 << $symsize) - 1;
  1443. $rs->pad = $pad;
  1444. $rs->alpha_to = array_fill(0, $rs->nn + 1, 0);
  1445. $rs->index_of = array_fill(0, $rs->nn + 1, 0);
  1446. $NN = & $rs->nn;
  1447. $A0 = & $NN;
  1448. $rs->index_of[0] = $A0;
  1449. $rs->alpha_to[$A0] = 0;
  1450. $sr = 1;
  1451. for ($i = 0; $i < $rs->nn; $i++) {
  1452. $rs->index_of[$sr] = $i;
  1453. $rs->alpha_to[$i] = $sr;
  1454. $sr <<= 1;
  1455. if ($sr & (1 << $symsize)) {
  1456. $sr ^= $gfpoly;
  1457. }
  1458. $sr &= $rs->nn;
  1459. }
  1460. if ($sr != 1) {
  1461. $rs = NULL;
  1462. return $rs;
  1463. }
  1464. $rs->genpoly = array_fill(0, $nroots + 1, 0);
  1465. $rs->fcr = $fcr;
  1466. $rs->prim = $prim;
  1467. $rs->nroots = $nroots;
  1468. $rs->gfpoly = $gfpoly;
  1469. for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rs->nn)
  1470. ;
  1471. $rs->iprim = (int) ($iprim / $prim);
  1472. $rs->genpoly[0] = 1;
  1473. for ($i = 0, $root = $fcr * $prim; $i < $nroots; $i++, $root += $prim) {
  1474. $rs->genpoly[$i + 1] = 1;
  1475. for ($j = $i; $j > 0; $j--) {
  1476. if ($rs->genpoly[$j] != 0) {
  1477. $rs->genpoly[$j] = $rs->genpoly[$j - 1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  1478. } else {
  1479. $rs->genpoly[$j] = $rs->genpoly[$j - 1];
  1480. }
  1481. }
  1482. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  1483. }
  1484. for ($i = 0; $i <= $nroots; $i++)
  1485. $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  1486. return $rs;
  1487. }
  1488. public function encode_rs_char($data, &$parity) {
  1489. $MM = & $this->mm;
  1490. $NN = & $this->nn;
  1491. $ALPHA_TO = & $this->alpha_to;
  1492. $INDEX_OF = & $this->index_of;
  1493. $GENPOLY = & $this->genpoly;
  1494. $NROOTS = & $this->nroots;
  1495. $FCR = & $this->fcr;
  1496. $PRIM = & $this->prim;
  1497. $IPRIM = & $this->iprim;
  1498. $PAD = & $this->pad;
  1499. $A0 = & $NN;
  1500. $parity = array_fill(0, $NROOTS, 0);
  1501. for ($i = 0; $i < ($NN - $NROOTS - $PAD); $i++) {
  1502. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  1503. if ($feedback != $A0) {
  1504. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  1505. for ($j = 1; $j < $NROOTS; $j++) {
  1506. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS - $j])];
  1507. }
  1508. }
  1509. array_shift($parity);
  1510. if ($feedback != $A0) {
  1511. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  1512. } else {
  1513. array_push($parity, 0);
  1514. }
  1515. }
  1516. }
  1517. }
  1518. class QRrs {
  1519. public static $items = array();
  1520. public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  1521. foreach (self::$items as $rs) {
  1522. if ($rs->pad != $pad)
  1523. continue;
  1524. if ($rs->nroots != $nroots)
  1525. continue;
  1526. if ($rs->mm != $symsize)
  1527. continue;
  1528. if ($rs->gfpoly != $gfpoly)
  1529. continue;
  1530. if ($rs->fcr != $fcr)
  1531. continue;
  1532. if ($rs->prim != $prim)
  1533. continue;
  1534. return $rs;
  1535. }
  1536. $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  1537. array_unshift(self::$items, $rs);
  1538. return $rs;
  1539. }
  1540. }
  1541. define('N1', 3);
  1542. define('N2', 3);
  1543. define('N3', 40);
  1544. define('N4', 10);
  1545. class QRmask {
  1546. public $runLength = array();
  1547. public function __construct() {
  1548. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  1549. }
  1550. public function writeFormatInformation($width, &$frame, $mask, $level) {
  1551. $blacks = 0;
  1552. $format = QRspec::getFormatInfo($mask, $level);
  1553. for ($i = 0; $i < 8; $i++) {
  1554. if ($format & 1) {
  1555. $blacks += 2;
  1556. $v = 0x85;
  1557. } else {
  1558. $v = 0x84;
  1559. }
  1560. $frame[8][$width - 1 - $i] = chr($v);
  1561. if ($i < 6) {
  1562. $frame[$i][8] = chr($v);
  1563. } else {
  1564. $frame[$i + 1][8] = chr($v);
  1565. }
  1566. $format = $format >> 1;
  1567. }
  1568. for ($i = 0; $i < 7; $i++) {
  1569. if ($format & 1) {
  1570. $blacks += 2;
  1571. $v = 0x85;
  1572. } else {
  1573. $v = 0x84;
  1574. }
  1575. $frame[$width - 7 + $i][8] = chr($v);
  1576. if ($i == 0) {
  1577. $frame[8][7] = chr($v);
  1578. } else {
  1579. $frame[8][6 - $i] = chr($v);
  1580. }
  1581. $format = $format >> 1;
  1582. }
  1583. return $blacks;
  1584. }
  1585. public function mask0($x, $y) {
  1586. return ($x + $y) & 1;
  1587. }
  1588. public function mask1($x, $y) {
  1589. return ($y & 1);
  1590. }
  1591. public function mask2($x, $y) {
  1592. return ($x % 3);
  1593. }
  1594. public function mask3($x, $y) {
  1595. return ($x + $y) % 3;
  1596. }
  1597. public function mask4($x, $y) {
  1598. return (((int) ($y / 2)) + ((int) ($x / 3))) & 1;
  1599. }
  1600. public function mask5($x, $y) {
  1601. return (($x * $y) & 1) + ($x * $y) % 3;
  1602. }
  1603. public function mask6($x, $y) {
  1604. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  1605. }
  1606. public function mask7($x, $y) {
  1607. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  1608. }
  1609. private function generateMaskNo($maskNo, $width, $frame) {
  1610. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  1611. for ($y = 0; $y < $width; $y++) {
  1612. for ($x = 0; $x < $width; $x++) {
  1613. if (ord($frame[$y][$x]) & 0x80) {
  1614. $bitMask[$y][$x] = 0;
  1615. } else {
  1616. $maskFunc = call_user_func(array($this, 'mask' . $maskNo), $x, $y);
  1617. $bitMask[$y][$x] = ($maskFunc == 0) ? 1 : 0;
  1618. }
  1619. }
  1620. }
  1621. return $bitMask;
  1622. }
  1623. public static function serial($bitFrame) {
  1624. $codeArr = array();
  1625. foreach ($bitFrame as $line)
  1626. $codeArr[] = join('', $line);
  1627. return gzcompress(join("\n", $codeArr), 9);
  1628. }
  1629. public static function unserial($code) {
  1630. $codeArr = array();
  1631. $codeLines = explode("\n", gzuncompress($code));
  1632. foreach ($codeLines as $line)
  1633. $codeArr[] = str_split($line);
  1634. return $codeArr;
  1635. }
  1636. public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) {
  1637. $b = 0;
  1638. $bitMask = array();
  1639. $fileName = QR_CACHE_DIR . 'mask_' . $maskNo . DIRECTORY_SEPARATOR . 'mask_' . $width . '_' . $maskNo . '.dat';
  1640. if (QR_CACHEABLE) {
  1641. if (file_exists($fileName)) {
  1642. $bitMask = self::unserial(file_get_contents($fileName));
  1643. } else {
  1644. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  1645. if (!file_exists(QR_CACHE_DIR . 'mask_' . $maskNo))
  1646. mkdir(QR_CACHE_DIR . 'mask_' . $maskNo);
  1647. file_put_contents($fileName, self::serial($bitMask));
  1648. }
  1649. } else {
  1650. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  1651. }
  1652. if ($maskGenOnly)
  1653. return;
  1654. $d = $s;
  1655. for ($y = 0; $y < $width; $y++) {
  1656. for ($x = 0; $x < $width; $x++) {
  1657. if ($bitMas

Large files files are truncated, but you can click here to view the full file