You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3312 lines
116 KiB

3 years ago
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * This file contains MERGED version of PHP QR Code library.
  6. * It was auto-generated from full version for your convenience.
  7. *
  8. * This merged version was configured to not requre any external files,
  9. * with disabled cache, error loging and weker but faster mask matching.
  10. * If you need tune it up please use non-merged version.
  11. *
  12. * For full version, documentation, examples of use please visit:
  13. *
  14. * http://phpqrcode.sourceforge.net/
  15. * https://sourceforge.net/projects/phpqrcode/
  16. *
  17. * PHP QR Code is distributed under LGPL 3
  18. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  19. *
  20. * This library is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU Lesser General Public
  22. * License as published by the Free Software Foundation; either
  23. * version 3 of the License, or any later version.
  24. *
  25. * This library is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * Lesser General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Lesser General Public
  31. * License along with this library; if not, write to the Free Software
  32. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  33. */
  34. /*
  35. * Version: 1.1.4
  36. * Build: 2010100721
  37. */
  38. //---- qrconst.php -----------------------------
  39. /*
  40. * PHP QR Code encoder
  41. *
  42. * Common constants
  43. *
  44. * Based on libqrencode C library distributed under LGPL 2.1
  45. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  46. *
  47. * PHP QR Code is distributed under LGPL 3
  48. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  49. *
  50. * This library is free software; you can redistribute it and/or
  51. * modify it under the terms of the GNU Lesser General Public
  52. * License as published by the Free Software Foundation; either
  53. * version 3 of the License, or any later version.
  54. *
  55. * This library is distributed in the hope that it will be useful,
  56. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  57. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  58. * Lesser General Public License for more details.
  59. *
  60. * You should have received a copy of the GNU Lesser General Public
  61. * License along with this library; if not, write to the Free Software
  62. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  63. */
  64. // Encoding modes
  65. define('QR_MODE_NUL', -1);
  66. define('QR_MODE_NUM', 0);
  67. define('QR_MODE_AN', 1);
  68. define('QR_MODE_8', 2);
  69. define('QR_MODE_KANJI', 3);
  70. define('QR_MODE_STRUCTURE', 4);
  71. // Levels of error correction.
  72. define('QR_ECLEVEL_L', 0);
  73. define('QR_ECLEVEL_M', 1);
  74. define('QR_ECLEVEL_Q', 2);
  75. define('QR_ECLEVEL_H', 3);
  76. // Supported output formats
  77. define('QR_FORMAT_TEXT', 0);
  78. define('QR_FORMAT_PNG', 1);
  79. class qrstr {
  80. public static function set(&$srctab, $x, $y, $repl, $replLen = false) {
  81. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  82. }
  83. }
  84. //---- merged_config.php -----------------------------
  85. /*
  86. * PHP QR Code encoder
  87. *
  88. * Config file, tuned-up for merged verion
  89. */
  90. define('QR_CACHEABLE', false); // use cache - more disk reads but less CPU power, masks and format templates are stored there
  91. define('QR_CACHE_DIR', false); // used when QR_CACHEABLE === true
  92. define('QR_LOG_DIR', false); // default error logs dir
  93. define('QR_FIND_BEST_MASK', true); // if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  94. define('QR_FIND_FROM_RANDOM', 2); // if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  95. define('QR_DEFAULT_MASK', 2); // when QR_FIND_BEST_MASK === false
  96. define('QR_PNG_MAXIMUM_SIZE', 1024); // maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images
  97. //---- qrtools.php -----------------------------
  98. /*
  99. * PHP QR Code encoder
  100. *
  101. * Toolset, handy and debug utilites.
  102. *
  103. * PHP QR Code is distributed under LGPL 3
  104. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  105. *
  106. * This library is free software; you can redistribute it and/or
  107. * modify it under the terms of the GNU Lesser General Public
  108. * License as published by the Free Software Foundation; either
  109. * version 3 of the License, or any later version.
  110. *
  111. * This library is distributed in the hope that it will be useful,
  112. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  113. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  114. * Lesser General Public License for more details.
  115. *
  116. * You should have received a copy of the GNU Lesser General Public
  117. * License along with this library; if not, write to the Free Software
  118. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  119. */
  120. class QRtools {
  121. //----------------------------------------------------------------------
  122. public static function binarize($frame)
  123. {
  124. $len = count($frame);
  125. foreach ($frame as &$frameLine) {
  126. for($i=0; $i<$len; $i++) {
  127. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  128. }
  129. }
  130. return $frame;
  131. }
  132. //----------------------------------------------------------------------
  133. public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037')
  134. {
  135. $barcode_array = array();
  136. if (!is_array($mode))
  137. $mode = explode(',', $mode);
  138. $eccLevel = 'L';
  139. if (count($mode) > 1) {
  140. $eccLevel = $mode[1];
  141. }
  142. $qrTab = QRcode::text($code, false, $eccLevel);
  143. $size = count($qrTab);
  144. $barcode_array['num_rows'] = $size;
  145. $barcode_array['num_cols'] = $size;
  146. $barcode_array['bcode'] = array();
  147. foreach ($qrTab as $line) {
  148. $arrAdd = array();
  149. foreach(str_split($line) as $char)
  150. $arrAdd[] = ($char=='1')?1:0;
  151. $barcode_array['bcode'][] = $arrAdd;
  152. }
  153. return $barcode_array;
  154. }
  155. //----------------------------------------------------------------------
  156. public static function clearCache()
  157. {
  158. self::$frames = array();
  159. }
  160. //----------------------------------------------------------------------
  161. public static function buildCache()
  162. {
  163. QRtools::markTime('before_build_cache');
  164. $mask = new QRmask();
  165. for ($a=1; $a <= QRSPEC_VERSION_MAX; $a++) {
  166. $frame = QRspec::newFrame($a);
  167. if (QR_IMAGE) {
  168. $fileName = QR_CACHE_DIR.'frame_'.$a.'.png';
  169. QRimage::png(self::binarize($frame), $fileName, 1, 0);
  170. }
  171. $width = count($frame);
  172. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  173. for ($maskNo=0; $maskNo<8; $maskNo++)
  174. $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
  175. }
  176. QRtools::markTime('after_build_cache');
  177. }
  178. //----------------------------------------------------------------------
  179. public static function log($outfile, $err)
  180. {
  181. if (QR_LOG_DIR !== false) {
  182. if ($err != '') {
  183. if ($outfile !== false) {
  184. file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  185. } else {
  186. file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  187. }
  188. }
  189. }
  190. }
  191. //----------------------------------------------------------------------
  192. public static function dumpMask($frame)
  193. {
  194. $width = count($frame);
  195. for($y=0;$y<$width;$y++) {
  196. for($x=0;$x<$width;$x++) {
  197. echo ord($frame[$y][$x]).',';
  198. }
  199. }
  200. }
  201. //----------------------------------------------------------------------
  202. public static function markTime($markerId)
  203. {
  204. list($usec, $sec) = explode(" ", microtime());
  205. $time = ((float)$usec + (float)$sec);
  206. if (!isset($GLOBALS['qr_time_bench']))
  207. $GLOBALS['qr_time_bench'] = array();
  208. $GLOBALS['qr_time_bench'][$markerId] = $time;
  209. }
  210. //----------------------------------------------------------------------
  211. public static function timeBenchmark()
  212. {
  213. self::markTime('finish');
  214. $lastTime = 0;
  215. $startTime = 0;
  216. $p = 0;
  217. echo '<table cellpadding="3" cellspacing="1">
  218. <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead>
  219. <tbody>';
  220. foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) {
  221. if ($p > 0) {
  222. echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, 6).'s</td></tr>';
  223. } else {
  224. $startTime = $thisTime;
  225. }
  226. $p++;
  227. $lastTime = $thisTime;
  228. }
  229. echo '</tbody><tfoot>
  230. <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr>
  231. </tfoot>
  232. </table>';
  233. }
  234. }
  235. //##########################################################################
  236. QRtools::markTime('start');
  237. //---- qrspec.php -----------------------------
  238. /*
  239. * PHP QR Code encoder
  240. *
  241. * QR Code specifications
  242. *
  243. * Based on libqrencode C library distributed under LGPL 2.1
  244. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  245. *
  246. * PHP QR Code is distributed under LGPL 3
  247. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  248. *
  249. * The following data / specifications are taken from
  250. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  251. * or
  252. * "Automatic identification and data capture techniques --
  253. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  254. *
  255. * This library is free software; you can redistribute it and/or
  256. * modify it under the terms of the GNU Lesser General Public
  257. * License as published by the Free Software Foundation; either
  258. * version 3 of the License, or any later version.
  259. *
  260. * This library is distributed in the hope that it will be useful,
  261. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  262. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  263. * Lesser General Public License for more details.
  264. *
  265. * You should have received a copy of the GNU Lesser General Public
  266. * License along with this library; if not, write to the Free Software
  267. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  268. */
  269. define('QRSPEC_VERSION_MAX', 40);
  270. define('QRSPEC_WIDTH_MAX', 177);
  271. define('QRCAP_WIDTH', 0);
  272. define('QRCAP_WORDS', 1);
  273. define('QRCAP_REMINDER', 2);
  274. define('QRCAP_EC', 3);
  275. class QRspec {
  276. public static $capacity = array(
  277. array( 0, 0, 0, array( 0, 0, 0, 0)),
  278. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  279. array( 25, 44, 7, array( 10, 16, 22, 28)),
  280. array( 29, 70, 7, array( 15, 26, 36, 44)),
  281. array( 33, 100, 7, array( 20, 36, 52, 64)),
  282. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  283. array( 41, 172, 7, array( 36, 64, 96, 112)),
  284. array( 45, 196, 0, array( 40, 72, 108, 130)),
  285. array( 49, 242, 0, array( 48, 88, 132, 156)),
  286. array( 53, 292, 0, array( 60, 110, 160, 192)),
  287. array( 57, 346, 0, array( 72, 130, 192, 224)), //10
  288. array( 61, 404, 0, array( 80, 150, 224, 264)),
  289. array( 65, 466, 0, array( 96, 176, 260, 308)),
  290. array( 69, 532, 0, array( 104, 198, 288, 352)),
  291. array( 73, 581, 3, array( 120, 216, 320, 384)),
  292. array( 77, 655, 3, array( 132, 240, 360, 432)), //15
  293. array( 81, 733, 3, array( 144, 280, 408, 480)),
  294. array( 85, 815, 3, array( 168, 308, 448, 532)),
  295. array( 89, 901, 3, array( 180, 338, 504, 588)),
  296. array( 93, 991, 3, array( 196, 364, 546, 650)),
  297. array( 97, 1085, 3, array( 224, 416, 600, 700)), //20
  298. array(101, 1156, 4, array( 224, 442, 644, 750)),
  299. array(105, 1258, 4, array( 252, 476, 690, 816)),
  300. array(109, 1364, 4, array( 270, 504, 750, 900)),
  301. array(113, 1474, 4, array( 300, 560, 810, 960)),
  302. array(117, 1588, 4, array( 312, 588, 870, 1050)), //25
  303. array(121, 1706, 4, array( 336, 644, 952, 1110)),
  304. array(125, 1828, 4, array( 360, 700, 1020, 1200)),
  305. array(129, 1921, 3, array( 390, 728, 1050, 1260)),
  306. array(133, 2051, 3, array( 420, 784, 1140, 1350)),
  307. array(137, 2185, 3, array( 450, 812, 1200, 1440)), //30
  308. array(141, 2323, 3, array( 480, 868, 1290, 1530)),
  309. array(145, 2465, 3, array( 510, 924, 1350, 1620)),
  310. array(149, 2611, 3, array( 540, 980, 1440, 1710)),
  311. array(153, 2761, 3, array( 570, 1036, 1530, 1800)),
  312. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), //35
  313. array(161, 3034, 0, array( 600, 1120, 1680, 1980)),
  314. array(165, 3196, 0, array( 630, 1204, 1770, 2100)),
  315. array(169, 3362, 0, array( 660, 1260, 1860, 2220)),
  316. array(173, 3532, 0, array( 720, 1316, 1950, 2310)),
  317. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) //40
  318. );
  319. //----------------------------------------------------------------------
  320. public static function getDataLength($version, $level)
  321. {
  322. return self::$capacity[$version][QRCAP_WORDS] - self::$capacity[$version][QRCAP_EC][$level];
  323. }
  324. //----------------------------------------------------------------------
  325. public static function getECCLength($version, $level)
  326. {
  327. return self::$capacity[$version][QRCAP_EC][$level];
  328. }
  329. //----------------------------------------------------------------------
  330. public static function getWidth($version)
  331. {
  332. return self::$capacity[$version][QRCAP_WIDTH];
  333. }
  334. //----------------------------------------------------------------------
  335. public static function getRemainder($version)
  336. {
  337. return self::$capacity[$version][QRCAP_REMINDER];
  338. }
  339. //----------------------------------------------------------------------
  340. public static function getMinimumVersion($size, $level)
  341. {
  342. for($i=1; $i<= QRSPEC_VERSION_MAX; $i++) {
  343. $words = self::$capacity[$i][QRCAP_WORDS] - self::$capacity[$i][QRCAP_EC][$level];
  344. if($words >= $size)
  345. return $i;
  346. }
  347. return -1;
  348. }
  349. //######################################################################
  350. public static $lengthTableBits = array(
  351. array(10, 12, 14),
  352. array( 9, 11, 13),
  353. array( 8, 16, 16),
  354. array( 8, 10, 12)
  355. );
  356. //----------------------------------------------------------------------
  357. public static function lengthIndicator($mode, $version)
  358. {
  359. if ($mode == QR_MODE_STRUCTURE)
  360. return 0;
  361. if ($version <= 9) {
  362. $l = 0;
  363. } else if ($version <= 26) {
  364. $l = 1;
  365. } else {
  366. $l = 2;
  367. }
  368. return self::$lengthTableBits[$mode][$l];
  369. }
  370. //----------------------------------------------------------------------
  371. public static function maximumWords($mode, $version)
  372. {
  373. if($mode == QR_MODE_STRUCTURE)
  374. return 3;
  375. if($version <= 9) {
  376. $l = 0;
  377. } else if($version <= 26) {
  378. $l = 1;
  379. } else {
  380. $l = 2;
  381. }
  382. $bits = self::$lengthTableBits[$mode][$l];
  383. $words = (1 << $bits) - 1;
  384. if($mode == QR_MODE_KANJI) {
  385. $words *= 2; // the number of bytes is required
  386. }
  387. return $words;
  388. }
  389. // Error correction code -----------------------------------------------
  390. // Table of the error correction code (Reed-Solomon block)
  391. // See Table 12-16 (pp.30-36), JIS X0510:2004.
  392. public static $eccTable = array(
  393. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)),
  394. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  395. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)),
  396. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)),
  397. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)),
  398. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  399. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)),
  400. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)),
  401. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)),
  402. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)),
  403. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), //10
  404. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)),
  405. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)),
  406. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)),
  407. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)),
  408. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), //15
  409. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)),
  410. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)),
  411. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)),
  412. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)),
  413. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), //20
  414. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)),
  415. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)),
  416. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)),
  417. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)),
  418. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), //25
  419. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)),
  420. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)),
  421. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)),
  422. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)),
  423. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), //30
  424. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)),
  425. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)),
  426. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)),
  427. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)),
  428. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), //35
  429. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)),
  430. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)),
  431. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)),
  432. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)),
  433. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)),//40
  434. );
  435. //----------------------------------------------------------------------
  436. // CACHEABLE!!!
  437. public static function getEccSpec($version, $level, array &$spec)
  438. {
  439. if (count($spec) < 5) {
  440. $spec = array(0,0,0,0,0);
  441. }
  442. $b1 = self::$eccTable[$version][$level][0];
  443. $b2 = self::$eccTable[$version][$level][1];
  444. $data = self::getDataLength($version, $level);
  445. $ecc = self::getECCLength($version, $level);
  446. if($b2 == 0) {
  447. $spec[0] = $b1;
  448. $spec[1] = (int)($data / $b1);
  449. $spec[2] = (int)($ecc / $b1);
  450. $spec[3] = 0;
  451. $spec[4] = 0;
  452. } else {
  453. $spec[0] = $b1;
  454. $spec[1] = (int)($data / ($b1 + $b2));
  455. $spec[2] = (int)($ecc / ($b1 + $b2));
  456. $spec[3] = $b2;
  457. $spec[4] = $spec[1] + 1;
  458. }
  459. }
  460. // Alignment pattern ---------------------------------------------------
  461. // Positions of alignment patterns.
  462. // This array includes only the second and the third position of the
  463. // alignment patterns. Rest of them can be calculated from the distance
  464. // between them.
  465. // See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
  466. public static $alignmentPattern = array(
  467. array( 0, 0),
  468. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  469. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  470. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), //11-15
  471. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), //16-20
  472. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), //21-25
  473. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), //26-30
  474. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), //31-35
  475. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58), //35-40
  476. );
  477. /** --------------------------------------------------------------------
  478. * Put an alignment marker.
  479. * @param frame
  480. * @param width
  481. * @param ox,oy center coordinate of the pattern
  482. */
  483. public static function putAlignmentMarker(array &$frame, $ox, $oy)
  484. {
  485. $finder = array(
  486. "\xa1\xa1\xa1\xa1\xa1",
  487. "\xa1\xa0\xa0\xa0\xa1",
  488. "\xa1\xa0\xa1\xa0\xa1",
  489. "\xa1\xa0\xa0\xa0\xa1",
  490. "\xa1\xa1\xa1\xa1\xa1"
  491. );
  492. $yStart = $oy-2;
  493. $xStart = $ox-2;
  494. for($y=0; $y<5; $y++) {
  495. QRstr::set($frame, $xStart, $yStart+$y, $finder[$y]);
  496. }
  497. }
  498. //----------------------------------------------------------------------
  499. public static function putAlignmentPattern($version, &$frame, $width)
  500. {
  501. if($version < 2)
  502. return;
  503. $d = self::$alignmentPattern[$version][1] - self::$alignmentPattern[$version][0];
  504. if($d < 0) {
  505. $w = 2;
  506. } else {
  507. $w = (int)(($width - self::$alignmentPattern[$version][0]) / $d + 2);
  508. }
  509. if($w * $w - 3 == 1) {
  510. $x = self::$alignmentPattern[$version][0];
  511. $y = self::$alignmentPattern[$version][0];
  512. self::putAlignmentMarker($frame, $x, $y);
  513. return;
  514. }
  515. $cx = self::$alignmentPattern[$version][0];
  516. for($x=1; $x<$w - 1; $x++) {
  517. self::putAlignmentMarker($frame, 6, $cx);
  518. self::putAlignmentMarker($frame, $cx, 6);
  519. $cx += $d;
  520. }
  521. $cy = self::$alignmentPattern[$version][0];
  522. for($y=0; $y<$w-1; $y++) {
  523. $cx = self::$alignmentPattern[$version][0];
  524. for($x=0; $x<$w-1; $x++) {
  525. self::putAlignmentMarker($frame, $cx, $cy);
  526. $cx += $d;
  527. }
  528. $cy += $d;
  529. }
  530. }
  531. // Version information pattern -----------------------------------------
  532. // Version information pattern (BCH coded).
  533. // See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
  534. // size: [QRSPEC_VERSION_MAX - 6]
  535. public static $versionPattern = array(
  536. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
  537. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
  538. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
  539. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
  540. 0x27541, 0x28c69
  541. );
  542. //----------------------------------------------------------------------
  543. public static function getVersionPattern($version)
  544. {
  545. if($version < 7 || $version > QRSPEC_VERSION_MAX)
  546. return 0;
  547. return self::$versionPattern[$version -7];
  548. }
  549. // Format information --------------------------------------------------
  550. // See calcFormatInfo in tests/test_qrspec.c (orginal qrencode c lib)
  551. public static $formatInfo = array(
  552. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
  553. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
  554. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
  555. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
  556. );
  557. public static function getFormatInfo($mask, $level)
  558. {
  559. if($mask < 0 || $mask > 7)
  560. return 0;
  561. if($level < 0 || $level > 3)
  562. return 0;
  563. return self::$formatInfo[$level][$mask];
  564. }
  565. // Frame ---------------------------------------------------------------
  566. // Cache of initial frames.
  567. public static $frames = array();
  568. /** --------------------------------------------------------------------
  569. * Put a finder pattern.
  570. * @param frame
  571. * @param width
  572. * @param ox,oy upper-left coordinate of the pattern
  573. */
  574. public static function putFinderPattern(&$frame, $ox, $oy)
  575. {
  576. $finder = array(
  577. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  578. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  579. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  580. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  581. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  582. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  583. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  584. );
  585. for($y=0; $y<7; $y++) {
  586. QRstr::set($frame, $ox, $oy+$y, $finder[$y]);
  587. }
  588. }
  589. //----------------------------------------------------------------------
  590. public static function createFrame($version)
  591. {
  592. $width = self::$capacity[$version][QRCAP_WIDTH];
  593. $frameLine = str_repeat ("\0", $width);
  594. $frame = array_fill(0, $width, $frameLine);
  595. // Finder pattern
  596. self::putFinderPattern($frame, 0, 0);
  597. self::putFinderPattern($frame, $width - 7, 0);
  598. self::putFinderPattern($frame, 0, $width - 7);
  599. // Separator
  600. $yOffset = $width - 7;
  601. for($y=0; $y<7; $y++) {
  602. $frame[$y][7] = "\xc0";
  603. $frame[$y][$width - 8] = "\xc0";
  604. $frame[$yOffset][7] = "\xc0";
  605. $yOffset++;
  606. }
  607. $setPattern = str_repeat("\xc0", 8);
  608. QRstr::set($frame, 0, 7, $setPattern);
  609. QRstr::set($frame, $width-8, 7, $setPattern);
  610. QRstr::set($frame, 0, $width - 8, $setPattern);
  611. // Format info
  612. $setPattern = str_repeat("\x84", 9);
  613. QRstr::set($frame, 0, 8, $setPattern);
  614. QRstr::set($frame, $width - 8, 8, $setPattern, 8);
  615. $yOffset = $width - 8;
  616. for($y=0; $y<8; $y++,$yOffset++) {
  617. $frame[$y][8] = "\x84";
  618. $frame[$yOffset][8] = "\x84";
  619. }
  620. // Timing pattern
  621. for($i=1; $i<$width-15; $i++) {
  622. $frame[6][7+$i] = chr(0x90 | ($i & 1));
  623. $frame[7+$i][6] = chr(0x90 | ($i & 1));
  624. }
  625. // Alignment pattern
  626. self::putAlignmentPattern($version, $frame, $width);
  627. // Version information
  628. if($version >= 7) {
  629. $vinf = self::getVersionPattern($version);
  630. $v = $vinf;
  631. for($x=0; $x<6; $x++) {
  632. for($y=0; $y<3; $y++) {
  633. $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
  634. $v = $v >> 1;
  635. }
  636. }
  637. $v = $vinf;
  638. for($y=0; $y<6; $y++) {
  639. for($x=0; $x<3; $x++) {
  640. $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
  641. $v = $v >> 1;
  642. }
  643. }
  644. }
  645. // and a little bit...
  646. $frame[$width - 8][8] = "\x81";
  647. return $frame;
  648. }
  649. //----------------------------------------------------------------------
  650. public static function debug($frame, $binary_mode = false)
  651. {
  652. if ($binary_mode) {
  653. foreach ($frame as &$frameLine) {
  654. $frameLine = join('<span class="m">&nbsp;&nbsp;</span>', explode('0', $frameLine));
  655. $frameLine = join('&#9608;&#9608;', explode('1', $frameLine));
  656. }
  657. ?>
  658. <style>
  659. .m { background-color: white; }
  660. </style>
  661. <?php
  662. echo '<pre><tt><br/ ><br/ ><br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  663. echo join("<br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $frame);
  664. echo '</tt></pre><br/ ><br/ ><br/ ><br/ ><br/ ><br/ >';
  665. } else {
  666. foreach ($frame as &$frameLine) {
  667. $frameLine = join('<span class="m">&nbsp;</span>', explode("\xc0", $frameLine));
  668. $frameLine = join('<span class="m">&#9618;</span>', explode("\xc1", $frameLine));
  669. $frameLine = join('<span class="p">&nbsp;</span>', explode("\xa0", $frameLine));
  670. $frameLine = join('<span class="p">&#9618;</span>', explode("\xa1", $frameLine));
  671. $frameLine = join('<span class="s">&#9671;</span>', explode("\x84", $frameLine)); //format 0
  672. $frameLine = join('<span class="s">&#9670;</span>', explode("\x85", $frameLine)); //format 1
  673. $frameLine = join('<span class="x">&#9762;</span>', explode("\x81", $frameLine)); //special bit
  674. $frameLine = join('<span class="c">&nbsp;</span>', explode("\x90", $frameLine)); //clock 0
  675. $frameLine = join('<span class="c">&#9719;</span>', explode("\x91", $frameLine)); //clock 1
  676. $frameLine = join('<span class="f">&nbsp;</span>', explode("\x88", $frameLine)); //version
  677. $frameLine = join('<span class="f">&#9618;</span>', explode("\x89", $frameLine)); //version
  678. $frameLine = join('&#9830;', explode("\x01", $frameLine));
  679. $frameLine = join('&#8901;', explode("\0", $frameLine));
  680. }
  681. ?>
  682. <style>
  683. .p { background-color: yellow; }
  684. .m { background-color: #00FF00; }
  685. .s { background-color: #FF0000; }
  686. .c { background-color: aqua; }
  687. .x { background-color: pink; }
  688. .f { background-color: gold; }
  689. </style>
  690. <?php
  691. echo "<pre><tt>";
  692. echo join("<br/ >", $frame);
  693. echo "</tt></pre>";
  694. }
  695. }
  696. //----------------------------------------------------------------------
  697. public static function serial($frame)
  698. {
  699. return gzcompress(join("\n", $frame), 9);
  700. }
  701. //----------------------------------------------------------------------
  702. public static function unserial($code)
  703. {
  704. return explode("\n", gzuncompress($code));
  705. }
  706. //----------------------------------------------------------------------
  707. public static function newFrame($version)
  708. {
  709. if($version < 1 || $version > QRSPEC_VERSION_MAX)
  710. return null;
  711. if(!isset(self::$frames[$version])) {
  712. $fileName = QR_CACHE_DIR.'frame_'.$version.'.dat';
  713. if (QR_CACHEABLE) {
  714. if (file_exists($fileName)) {
  715. self::$frames[$version] = self::unserial(file_get_contents($fileName));
  716. } else {
  717. self::$frames[$version] = self::createFrame($version);
  718. file_put_contents($fileName, self::serial(self::$frames[$version]));
  719. }
  720. } else {
  721. self::$frames[$version] = self::createFrame($version);
  722. }
  723. }
  724. if(is_null(self::$frames[$version]))
  725. return null;
  726. return self::$frames[$version];
  727. }
  728. //----------------------------------------------------------------------
  729. public static function rsBlockNum($spec) { return $spec[0] + $spec[3]; }
  730. public static function rsBlockNum1($spec) { return $spec[0]; }
  731. public static function rsDataCodes1($spec) { return $spec[1]; }
  732. public static function rsEccCodes1($spec) { return $spec[2]; }
  733. public static function rsBlockNum2($spec) { return $spec[3]; }
  734. public static function rsDataCodes2($spec) { return $spec[4]; }
  735. public static function rsEccCodes2($spec) { return $spec[2]; }
  736. public static function rsDataLength($spec) { return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]); }
  737. public static function rsEccLength($spec) { return ($spec[0] + $spec[3]) * $spec[2]; }
  738. }
  739. //---- qrimage.php -----------------------------
  740. /*
  741. * PHP QR Code encoder
  742. *
  743. * Image output of code using GD2
  744. *
  745. * PHP QR Code is distributed under LGPL 3
  746. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  747. *
  748. * This library is free software; you can redistribute it and/or
  749. * modify it under the terms of the GNU Lesser General Public
  750. * License as published by the Free Software Foundation; either
  751. * version 3 of the License, or any later version.
  752. *
  753. * This library is distributed in the hope that it will be useful,
  754. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  755. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  756. * Lesser General Public License for more details.
  757. *
  758. * You should have received a copy of the GNU Lesser General Public
  759. * License along with this library; if not, write to the Free Software
  760. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  761. */
  762. define('QR_IMAGE', true);
  763. class QRimage {
  764. //----------------------------------------------------------------------
  765. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
  766. {
  767. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  768. if ($filename === false) {
  769. Header("Content-type: image/png");
  770. ImagePng($image);
  771. } else {
  772. if($saveandprint===TRUE){
  773. ImagePng($image, $filename);
  774. header("Content-type: image/png");
  775. ImagePng($image);
  776. }else{
  777. ImagePng($image, $filename);
  778. }
  779. }
  780. ImageDestroy($image);
  781. }
  782. //----------------------------------------------------------------------
  783. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
  784. {
  785. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  786. if ($filename === false) {
  787. Header("Content-type: image/jpeg");
  788. ImageJpeg($image, null, $q);
  789. } else {
  790. ImageJpeg($image, $filename, $q);
  791. }
  792. ImageDestroy($image);
  793. }
  794. //----------------------------------------------------------------------
  795. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
  796. {
  797. $h = count($frame);
  798. $w = strlen($frame[0]);
  799. $imgW = $w + 2*$outerFrame;
  800. $imgH = $h + 2*$outerFrame;
  801. $base_image =ImageCreate($imgW, $imgH);
  802. $col[0] = ImageColorAllocate($base_image,255,255,255);
  803. $col[1] = ImageColorAllocate($base_image,0,0,0);
  804. imagefill($base_image, 0, 0, $col[0]);
  805. for($y=0; $y<$h; $y++) {
  806. for($x=0; $x<$w; $x++) {
  807. if ($frame[$y][$x] == '1') {
  808. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  809. }
  810. }
  811. }
  812. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  813. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  814. ImageDestroy($base_image);
  815. return $target_image;
  816. }
  817. }
  818. //---- qrinput.php -----------------------------
  819. /*
  820. * PHP QR Code encoder
  821. *
  822. * Input encoding class
  823. *
  824. * Based on libqrencode C library distributed under LGPL 2.1
  825. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  826. *
  827. * PHP QR Code is distributed under LGPL 3
  828. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  829. *
  830. * This library is free software; you can redistribute it and/or
  831. * modify it under the terms of the GNU Lesser General Public
  832. * License as published by the Free Software Foundation; either
  833. * version 3 of the License, or any later version.
  834. *
  835. * This library is distributed in the hope that it will be useful,
  836. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  837. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  838. * Lesser General Public License for more details.
  839. *
  840. * You should have received a copy of the GNU Lesser General Public
  841. * License along with this library; if not, write to the Free Software
  842. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  843. */
  844. define('STRUCTURE_HEADER_BITS', 20);
  845. define('MAX_STRUCTURED_SYMBOLS', 16);
  846. class QRinputItem {
  847. public $mode;
  848. public $size;
  849. public $data;
  850. public $bstream;
  851. public function __construct($mode, $size, $data, $bstream = null)
  852. {
  853. $setData = array_slice($data, 0, $size);
  854. if (count($setData) < $size) {
  855. $setData = array_merge($setData, array_fill(0,$size-count($setData),0));
  856. }
  857. if(!QRinput::check($mode, $size, $setData)) {
  858. throw new Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',',$setData));
  859. return null;
  860. }
  861. $this->mode = $mode;
  862. $this->size = $size;
  863. $this->data = $setData;
  864. $this->bstream = $bstream;
  865. }
  866. //----------------------------------------------------------------------
  867. public function encodeModeNum($version)
  868. {
  869. try {
  870. $words = (int)($this->size / 3);
  871. $bs = new QRbitstream();
  872. $val = 0x1;
  873. $bs->appendNum(4, $val);
  874. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size);
  875. for($i=0; $i<$words; $i++) {
  876. $val = (ord($this->data[$i*3 ]) - ord('0')) * 100;
  877. $val += (ord($this->data[$i*3+1]) - ord('0')) * 10;
  878. $val += (ord($this->data[$i*3+2]) - ord('0'));
  879. $bs->appendNum(10, $val);
  880. }
  881. if($this->size - $words * 3 == 1) {
  882. $val = ord($this->data[$words*3]) - ord('0');
  883. $bs->appendNum(4, $val);
  884. } else if($this->size - $words * 3 == 2) {
  885. $val = (ord($this->data[$words*3 ]) - ord('0')) * 10;
  886. $val += (ord($this->data[$words*3+1]) - ord('0'));
  887. $bs->appendNum(7, $val);
  888. }
  889. $this->bstream = $bs;
  890. return 0;
  891. } catch (Exception $e) {
  892. return -1;
  893. }
  894. }
  895. //----------------------------------------------------------------------
  896. public function encodeModeAn($version)
  897. {
  898. try {
  899. $words = (int)($this->size / 2);
  900. $bs = new QRbitstream();
  901. $bs->appendNum(4, 0x02);
  902. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size);
  903. for($i=0; $i<$words; $i++) {
  904. $val = (int)QRinput::lookAnTable(ord($this->data[$i*2 ])) * 45;
  905. $val += (int)QRinput::lookAnTable(ord($this->data[$i*2+1]));
  906. $bs->appendNum(11, $val);
  907. }
  908. if($this->size & 1) {
  909. $val = QRinput::lookAnTable(ord($this->data[$words * 2]));
  910. $bs->appendNum(6, $val);
  911. }
  912. $this->bstream = $bs;
  913. return 0;
  914. } catch (Exception $e) {
  915. return -1;
  916. }
  917. }
  918. //----------------------------------------------------------------------
  919. public function encodeMode8($version)
  920. {
  921. try {
  922. $bs = new QRbitstream();
  923. $bs->appendNum(4, 0x4);
  924. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size);
  925. for($i=0; $i<$this->size; $i++) {
  926. $bs->appendNum(8, ord($this->data[$i]));
  927. }
  928. $this->bstream = $bs;
  929. return 0;
  930. } catch (Exception $e) {
  931. return -1;
  932. }
  933. }
  934. //----------------------------------------------------------------------
  935. public function encodeModeKanji($version)
  936. {
  937. try {
  938. $bs = new QRbitrtream();
  939. $bs->appendNum(4, 0x8);
  940. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int)($this->size / 2));
  941. for($i=0; $i<$this->size; $i+=2) {
  942. $val = (ord($this->data[$i]) << 8) | ord($this->data[$i+1]);
  943. if($val <= 0x9ffc) {
  944. $val -= 0x8140;
  945. } else {
  946. $val -= 0xc140;
  947. }
  948. $h = ($val >> 8) * 0xc0;
  949. $val = ($val & 0xff) + $h;
  950. $bs->appendNum(13, $val);
  951. }
  952. $this->bstream = $bs;
  953. return 0;
  954. } catch (Exception $e) {
  955. return -1;
  956. }
  957. }
  958. //----------------------------------------------------------------------
  959. public function encodeModeStructure()
  960. {
  961. try {
  962. $bs = new QRbitstream();
  963. $bs->appendNum(4, 0x03);
  964. $bs->appendNum(4, ord($this->data[1]) - 1);
  965. $bs->appendNum(4, ord($this->data[0]) - 1);
  966. $bs->appendNum(8, ord($this->data[2]));
  967. $this->bstream = $bs;
  968. return 0;
  969. } catch (Exception $e) {
  970. return -1;
  971. }
  972. }
  973. //----------------------------------------------------------------------
  974. public function estimateBitStreamSizeOfEntry($version)
  975. {
  976. $bits = 0;
  977. if($version == 0)
  978. $version = 1;
  979. switch($this->mode) {
  980. case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size); break;
  981. case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size); break;
  982. case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size); break;
  983. case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);break;
  984. case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS;
  985. default:
  986. return 0;
  987. }
  988. $l = QRspec::lengthIndicator($this->mode, $version);
  989. $m = 1 << $l;
  990. $num = (int)(($this->size + $m - 1) / $m);
  991. $bits += $num * (4 + $l);
  992. return $bits;
  993. }
  994. //----------------------------------------------------------------------
  995. public function encodeBitStream($version)
  996. {
  997. try {
  998. unset($this->bstream);
  999. $words = QRspec::maximumWords($this->mode, $version);
  1000. if($this->size > $words) {
  1001. $st1 = new QRinputItem($this->mode, $words, $this->data);
  1002. $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words));
  1003. $st1->encodeBitStream($version);
  1004. $st2->encodeBitStream($version);
  1005. $this->bstream = new QRbitstream();
  1006. $this->bstream->append($st1->bstream);
  1007. $this->bstream->append($st2->bstream);
  1008. unset($st1);
  1009. unset($st2);
  1010. } else {
  1011. $ret = 0;
  1012. switch($this->mode) {
  1013. case QR_MODE_NUM: $ret = $this->encodeModeNum($version); break;
  1014. case QR_MODE_AN: $ret = $this->encodeModeAn($version); break;
  1015. case QR_MODE_8: $ret = $this->encodeMode8($version); break;
  1016. case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version);break;
  1017. case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure(); break;
  1018. default:
  1019. break;
  1020. }
  1021. if($ret < 0)
  1022. return -1;
  1023. }
  1024. return $this->bstream->size();
  1025. } catch (Exception $e) {
  1026. return -1;
  1027. }
  1028. }
  1029. };
  1030. //##########################################################################
  1031. class QRinput {
  1032. public $items;
  1033. private $version;
  1034. private $level;
  1035. //----------------------------------------------------------------------
  1036. public function __construct($version = 0, $level = QR_ECLEVEL_L)
  1037. {
  1038. if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
  1039. throw new Exception('Invalid version no');
  1040. return NULL;
  1041. }
  1042. $this->version = $version;
  1043. $this->level = $level;
  1044. }
  1045. //----------------------------------------------------------------------
  1046. public function getVersion()
  1047. {
  1048. return $this->version;
  1049. }
  1050. //----------------------------------------------------------------------
  1051. public function setVersion($version)
  1052. {
  1053. if($version < 0 || $version > QRSPEC_VERSION_MAX) {
  1054. throw new Exception('Invalid version no');
  1055. return -1;
  1056. }
  1057. $this->version = $version;
  1058. return 0;
  1059. }
  1060. //----------------------------------------------------------------------
  1061. public function getErrorCorrectionLevel()
  1062. {
  1063. return $this->level;
  1064. }
  1065. //----------------------------------------------------------------------
  1066. public function setErrorCorrectionLevel($level)
  1067. {
  1068. if($level > QR_ECLEVEL_H) {
  1069. throw new Exception('Invalid ECLEVEL');
  1070. return -1;
  1071. }
  1072. $this->level = $level;
  1073. return 0;
  1074. }
  1075. //----------------------------------------------------------------------
  1076. public function appendEntry(QRinputItem $entry)
  1077. {
  1078. $this->items[] = $entry;
  1079. }
  1080. //----------------------------------------------------------------------
  1081. public function append($mode, $size, $data)
  1082. {
  1083. try {
  1084. $entry = new QRinputItem($mode, $size, $data);
  1085. $this->items[] = $entry;
  1086. return 0;
  1087. } catch (Exception $e) {
  1088. return -1;
  1089. }
  1090. }
  1091. //----------------------------------------------------------------------
  1092. public function insertStructuredAppendHeader($size, $index, $parity)
  1093. {
  1094. if( $size > MAX_STRUCTURED_SYMBOLS ) {
  1095. throw new Exception('insertStructuredAppendHeader wrong size');
  1096. }
  1097. if( $index <= 0 || $index > MAX_STRUCTURED_SYMBOLS ) {
  1098. throw new Exception('insertStructuredAppendHeader wrong index');
  1099. }
  1100. $buf = array($size, $index, $parity);
  1101. try {
  1102. $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf);
  1103. array_unshift($this->items, $entry);
  1104. return 0;
  1105. } catch (Exception $e) {
  1106. return -1;
  1107. }
  1108. }
  1109. //----------------------------------------------------------------------
  1110. public function calcParity()
  1111. {
  1112. $parity = 0;
  1113. foreach($this->items as $item) {
  1114. if($item->mode != QR_MODE_STRUCTURE) {
  1115. for($i=$item->size-1; $i>=0; $i--) {
  1116. $parity ^= $item->data[$i];
  1117. }
  1118. }
  1119. }
  1120. return $parity;
  1121. }
  1122. //----------------------------------------------------------------------
  1123. public static function checkModeNum($size, $data)
  1124. {
  1125. for($i=0; $i<$size; $i++) {
  1126. if((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))){
  1127. return false;
  1128. }
  1129. }
  1130. return true;
  1131. }
  1132. //----------------------------------------------------------------------
  1133. public static function estimateBitsModeNum($size)
  1134. {
  1135. $w = (int)$size / 3;
  1136. $bits = $w * 10;
  1137. switch($size - $w * 3) {
  1138. case 1:
  1139. $bits += 4;
  1140. break;
  1141. case 2:
  1142. $bits += 7;
  1143. break;
  1144. default:
  1145. break;
  1146. }
  1147. return $bits;
  1148. }
  1149. //----------------------------------------------------------------------
  1150. public static $anTable = array(
  1151. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1152. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1153. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  1154. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  1155. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  1156. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  1157. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1158. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  1159. );
  1160. //----------------------------------------------------------------------
  1161. public static function lookAnTable($c)
  1162. {
  1163. return (($c > 127)?-1:self::$anTable[$c]);
  1164. }
  1165. //----------------------------------------------------------------------
  1166. public static function checkModeAn($size, $data)
  1167. {
  1168. for($i=0; $i<$size; $i++) {
  1169. if (self::lookAnTable(ord($data[$i])) == -1) {
  1170. return false;
  1171. }
  1172. }
  1173. return true;
  1174. }
  1175. //----------------------------------------------------------------------
  1176. public static function estimateBitsModeAn($size)
  1177. {
  1178. $w = (int)($size / 2);
  1179. $bits = $w * 11;
  1180. if($size & 1) {
  1181. $bits += 6;
  1182. }
  1183. return $bits;
  1184. }
  1185. //----------------------------------------------------------------------
  1186. public static function estimateBitsMode8($size)
  1187. {
  1188. return $size * 8;
  1189. }
  1190. //----------------------------------------------------------------------
  1191. public function estimateBitsModeKanji($size)
  1192. {
  1193. return (int)(($size / 2) * 13);
  1194. }
  1195. //----------------------------------------------------------------------
  1196. public static function checkModeKanji($size, $data)
  1197. {
  1198. if($size & 1)
  1199. return false;
  1200. for($i=0; $i<$size; $i+=2) {
  1201. $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
  1202. if( $val < 0x8140
  1203. || ($val > 0x9ffc && $val < 0xe040)
  1204. || $val > 0xebbf) {
  1205. return false;
  1206. }
  1207. }
  1208. return true;
  1209. }
  1210. /***********************************************************************
  1211. * Validation
  1212. **********************************************************************/
  1213. public static function check($mode, $size, $data)
  1214. {
  1215. if($size <= 0)
  1216. return false;
  1217. switch($mode) {
  1218. case QR_MODE_NUM: return self::checkModeNum($size, $data); break;
  1219. case QR_MODE_AN: return self::checkModeAn($size, $data); break;
  1220. case QR_MODE_KANJI: return self::checkModeKanji($size, $data); break;
  1221. case QR_MODE_8: return true; break;
  1222. case QR_MODE_STRUCTURE: return true; break;
  1223. default:
  1224. break;
  1225. }
  1226. return false;
  1227. }
  1228. //----------------------------------------------------------------------
  1229. public function estimateBitStreamSize($version)
  1230. {
  1231. $bits = 0;
  1232. foreach($this->items as $item) {
  1233. $bits += $item->estimateBitStreamSizeOfEntry($version);
  1234. }
  1235. return $bits;
  1236. }
  1237. //----------------------------------------------------------------------
  1238. public function estimateVersion()
  1239. {
  1240. $version = 0;
  1241. $prev = 0;
  1242. do {
  1243. $prev = $version;
  1244. $bits = $this->estimateBitStreamSize($prev);
  1245. $version = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1246. if ($version < 0) {
  1247. return -1;
  1248. }
  1249. } while ($version > $prev);
  1250. return $version;
  1251. }
  1252. //----------------------------------------------------------------------
  1253. public static function lengthOfCode($mode, $version, $bits)
  1254. {
  1255. $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version);
  1256. switch($mode) {
  1257. case QR_MODE_NUM:
  1258. $chunks = (int)($payload / 10);
  1259. $remain = $payload - $chunks * 10;
  1260. $size = $chunks * 3;
  1261. if($remain >= 7) {
  1262. $size += 2;
  1263. } else if($remain >= 4) {
  1264. $size += 1;
  1265. }
  1266. break;
  1267. case QR_MODE_AN:
  1268. $chunks = (int)($payload / 11);
  1269. $remain = $payload - $chunks * 11;
  1270. $size = $chunks * 2;
  1271. if($remain >= 6)
  1272. $size++;
  1273. break;
  1274. case QR_MODE_8:
  1275. $size = (int)($payload / 8);
  1276. break;
  1277. case QR_MODE_KANJI:
  1278. $size = (int)(($payload / 13) * 2);
  1279. break;
  1280. case QR_MODE_STRUCTURE:
  1281. $size = (int)($payload / 8);
  1282. break;
  1283. default:
  1284. $size = 0;
  1285. break;
  1286. }
  1287. $maxsize = QRspec::maximumWords($mode, $version);
  1288. if($size < 0) $size = 0;
  1289. if($size > $maxsize) $size = $maxsize;
  1290. return $size;
  1291. }
  1292. //----------------------------------------------------------------------
  1293. public function createBitStream()
  1294. {
  1295. $total = 0;
  1296. foreach($this->items as $item) {
  1297. $bits = $item->encodeBitStream($this->version);
  1298. if($bits < 0)
  1299. return -1;
  1300. $total += $bits;
  1301. }
  1302. return $total;
  1303. }
  1304. //----------------------------------------------------------------------
  1305. public function convertData()
  1306. {
  1307. $ver = $this->estimateVersion();
  1308. if($ver > $this->getVersion()) {
  1309. $this->setVersion($ver);
  1310. }
  1311. for(;;) {
  1312. $bits = $this->createBitStream();
  1313. if($bits < 0)
  1314. return -1;
  1315. $ver = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1316. if($ver < 0) {
  1317. throw new Exception('WRONG VERSION');
  1318. return -1;
  1319. } else if($ver > $this->getVersion()) {
  1320. $this->setVersion($ver);
  1321. } else {
  1322. break;
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. //----------------------------------------------------------------------
  1328. public function appendPaddingBit(&$bstream)
  1329. {
  1330. $bits = $bstream->size();
  1331. $maxwords = QRspec::getDataLength($this->version, $this->level);
  1332. $maxbits = $maxwords * 8;
  1333. if ($maxbits == $bits) {
  1334. return 0;
  1335. }
  1336. if ($maxbits - $bits < 5) {
  1337. return $bstream->appendNum($maxbits - $bits, 0);
  1338. }
  1339. $bits += 4;
  1340. $words = (int)(($bits + 7) / 8);
  1341. $padding = new QRbitstream();
  1342. $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
  1343. if($ret < 0)
  1344. return $ret;
  1345. $padlen = $maxwords - $words;
  1346. if($padlen > 0) {
  1347. $padbuf = array();
  1348. for($i=0; $i<$padlen; $i++) {
  1349. $padbuf[$i] = ($i&1)?0x11:0xec;
  1350. }
  1351. $ret = $padding->appendBytes($padlen, $padbuf);
  1352. if($ret < 0)
  1353. return $ret;
  1354. }
  1355. $ret = $bstream->append($padding);
  1356. return $ret;
  1357. }
  1358. //----------------------------------------------------------------------
  1359. public function mergeBitStream()
  1360. {
  1361. if($this->convertData() < 0) {
  1362. return null;
  1363. }
  1364. $bstream = new QRbitstream();
  1365. foreach($this->items as $item) {
  1366. $ret = $bstream->append($item->bstream);
  1367. if($ret < 0) {
  1368. return null;
  1369. }
  1370. }
  1371. return $bstream;
  1372. }
  1373. //----------------------------------------------------------------------
  1374. public function getBitStream()
  1375. {
  1376. $bstream = $this->mergeBitStream();
  1377. if($bstream == null) {
  1378. return null;
  1379. }
  1380. $ret = $this->appendPaddingBit($bstream);
  1381. if($ret < 0) {
  1382. return null;
  1383. }
  1384. return $bstream;
  1385. }
  1386. //----------------------------------------------------------------------
  1387. public function getByteStream()
  1388. {
  1389. $bstream = $this->getBitStream();
  1390. if($bstream == null) {
  1391. return null;
  1392. }
  1393. return $bstream->toByte();
  1394. }
  1395. }
  1396. //---- qrbitstream.php -----------------------------
  1397. /*
  1398. * PHP QR Code encoder
  1399. *
  1400. * Bitstream class
  1401. *
  1402. * Based on libqrencode C library distributed under LGPL 2.1
  1403. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  1404. *
  1405. * PHP QR Code is distributed under LGPL 3
  1406. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  1407. *
  1408. * This library is free software; you can redistribute it and/or
  1409. * modify it under the terms of the GNU Lesser General Public
  1410. * License as published by the Free Software Foundation; either
  1411. * version 3 of the License, or any later version.
  1412. *
  1413. * This library is distributed in the hope that it will be useful,
  1414. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1415. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1416. * Lesser General Public License for more details.
  1417. *
  1418. * You should have received a copy of the GNU Lesser General Public
  1419. * License along with this library; if not, write to the Free Software
  1420. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1421. */
  1422. class QRbitstream {
  1423. public $data = array();
  1424. //----------------------------------------------------------------------
  1425. public function size()
  1426. {
  1427. return count($this->data);
  1428. }
  1429. //----------------------------------------------------------------------
  1430. public function allocate($setLength)
  1431. {
  1432. $this->data = array_fill(0, $setLength, 0);
  1433. return 0;
  1434. }
  1435. //----------------------------------------------------------------------
  1436. public static function newFromNum($bits, $num)
  1437. {
  1438. $bstream = new QRbitstream();
  1439. $bstream->allocate($bits);
  1440. $mask = 1 << ($bits - 1);
  1441. for($i=0; $i<$bits; $i++) {
  1442. if($num & $mask) {
  1443. $bstream->data[$i] = 1;
  1444. } else {
  1445. $bstream->data[$i] = 0;
  1446. }
  1447. $mask = $mask >> 1;
  1448. }
  1449. return $bstream;
  1450. }
  1451. //----------------------------------------------------------------------
  1452. public static function newFromBytes($size, $data)
  1453. {
  1454. $bstream = new QRbitstream();
  1455. $bstream->allocate($size * 8);
  1456. $p=0;
  1457. for($i=0; $i<$size; $i++) {
  1458. $mask = 0x80;
  1459. for($j=0; $j<8; $j++) {
  1460. if($data[$i] & $mask) {
  1461. $bstream->data[$p] = 1;
  1462. } else {
  1463. $bstream->data[$p] = 0;
  1464. }
  1465. $p++;
  1466. $mask = $mask >> 1;
  1467. }
  1468. }
  1469. return $bstream;
  1470. }
  1471. //----------------------------------------------------------------------
  1472. public function append(QRbitstream $arg)
  1473. {
  1474. if (is_null($arg)) {
  1475. return -1;
  1476. }
  1477. if($arg->size() == 0) {
  1478. return 0;
  1479. }
  1480. if($this->size() == 0) {
  1481. $this->data = $arg->data;
  1482. return 0;
  1483. }
  1484. $this->data = array_values(array_merge($this->data, $arg->data));
  1485. return 0;
  1486. }
  1487. //----------------------------------------------------------------------
  1488. public function appendNum($bits, $num)
  1489. {
  1490. if ($bits == 0)
  1491. return 0;
  1492. $b = QRbitstream::newFromNum($bits, $num);
  1493. if(is_null($b))
  1494. return -1;
  1495. $ret = $this->append($b);
  1496. unset($b);
  1497. return $ret;
  1498. }
  1499. //----------------------------------------------------------------------
  1500. public function appendBytes($size, $data)
  1501. {
  1502. if ($size == 0)
  1503. return 0;
  1504. $b = QRbitstream::newFromBytes($size, $data);
  1505. if(is_null($b))
  1506. return -1;
  1507. $ret = $this->append($b);
  1508. unset($b);
  1509. return $ret;
  1510. }
  1511. //----------------------------------------------------------------------
  1512. public function toByte()
  1513. {
  1514. $size = $this->size();
  1515. if($size == 0) {
  1516. return array();
  1517. }
  1518. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  1519. $bytes = (int)($size / 8);
  1520. $p = 0;
  1521. for($i=0; $i<$bytes; $i++) {
  1522. $v = 0;
  1523. for($j=0; $j<8; $j++) {
  1524. $v = $v << 1;
  1525. $v |= $this->data[$p];
  1526. $p++;
  1527. }
  1528. $data[$i] = $v;
  1529. }
  1530. if($size & 7) {
  1531. $v = 0;
  1532. for($j=0; $j<($size & 7); $j++) {
  1533. $v = $v << 1;
  1534. $v |= $this->data[$p];
  1535. $p++;
  1536. }
  1537. $data[$bytes] = $v;
  1538. }
  1539. return $data;
  1540. }
  1541. }
  1542. //---- qrsplit.php -----------------------------
  1543. /*
  1544. * PHP QR Code encoder
  1545. *
  1546. * Input splitting classes
  1547. *
  1548. * Based on libqrencode C library distributed under LGPL 2.1
  1549. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  1550. *
  1551. * PHP QR Code is distributed under LGPL 3
  1552. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  1553. *
  1554. * The following data / specifications are taken from
  1555. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  1556. * or
  1557. * "Automatic identification and data capture techniques --
  1558. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  1559. *
  1560. * This library is free software; you can redistribute it and/or
  1561. * modify it under the terms of the GNU Lesser General Public
  1562. * License as published by the Free Software Foundation; either
  1563. * version 3 of the License, or any later version.
  1564. *
  1565. * This library is distributed in the hope that it will be useful,
  1566. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1567. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1568. * Lesser General Public License for more details.
  1569. *
  1570. * You should have received a copy of the GNU Lesser General Public
  1571. * License along with this library; if not, write to the Free Software
  1572. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1573. */
  1574. class QRsplit {
  1575. public $dataStr = '';
  1576. public $input;
  1577. public $modeHint;
  1578. //----------------------------------------------------------------------
  1579. public function __construct($dataStr, $input, $modeHint)
  1580. {
  1581. $this->dataStr = $dataStr;
  1582. $this->input = $input;
  1583. $this->modeHint = $modeHint;
  1584. }
  1585. //----------------------------------------------------------------------
  1586. public static function isdigitat($str, $pos)
  1587. {
  1588. if ($pos >= strlen($str))
  1589. return false;
  1590. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1591. }
  1592. //----------------------------------------------------------------------
  1593. public static function isalnumat($str, $pos)
  1594. {
  1595. if ($pos >= strlen($str))
  1596. return false;
  1597. return (QRinput::lookAnTable(ord($str[$pos])) >= 0);
  1598. }
  1599. //----------------------------------------------------------------------
  1600. public function identifyMode($pos)
  1601. {
  1602. if ($pos >= strlen($this->dataStr))
  1603. return QR_MODE_NUL;
  1604. $c = $this->dataStr[$pos];
  1605. if(self::isdigitat($this->dataStr, $pos)) {
  1606. return QR_MODE_NUM;
  1607. } else if(self::isalnumat($this->dataStr, $pos)) {
  1608. return QR_MODE_AN;
  1609. } else if($this->modeHint == QR_MODE_KANJI) {
  1610. if ($pos+1 < strlen($this->dataStr))
  1611. {
  1612. $d = $this->dataStr[$pos+1];
  1613. $word = (ord($c) << 8) | ord($d);
  1614. if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
  1615. return QR_MODE_KANJI;
  1616. }
  1617. }
  1618. }
  1619. return QR_MODE_8;
  1620. }
  1621. //----------------------------------------------------------------------
  1622. public function eatNum()
  1623. {
  1624. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1625. $p = 0;
  1626. while(self::isdigitat($this->dataStr, $p)) {
  1627. $p++;
  1628. }
  1629. $run = $p;
  1630. $mode = $this->identifyMode($p);
  1631. if($mode == QR_MODE_8) {
  1632. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1633. + QRinput::estimateBitsMode8(1) // + 4 + l8
  1634. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  1635. if($dif > 0) {
  1636. return $this->eat8();
  1637. }
  1638. }
  1639. if($mode == QR_MODE_AN) {
  1640. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1641. + QRinput::estimateBitsModeAn(1) // + 4 + la
  1642. - QRinput::estimateBitsModeAn($run + 1);// - 4 - la
  1643. if($dif > 0) {
  1644. return $this->eatAn();
  1645. }
  1646. }
  1647. $ret = $this->input->append(QR_MODE_NUM, $run, str_split($this->dataStr));
  1648. if($ret < 0)
  1649. return -1;
  1650. return $run;
  1651. }
  1652. //----------------------------------------------------------------------
  1653. public function eatAn()
  1654. {
  1655. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1656. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1657. $p = 0;
  1658. while(self::isalnumat($this->dataStr, $p)) {
  1659. if(self::isdigitat($this->dataStr, $p)) {
  1660. $q = $p;
  1661. while(self::isdigitat($this->dataStr, $q)) {
  1662. $q++;
  1663. }
  1664. $dif = QRinput::estimateBitsModeAn($p) // + 4 + la
  1665. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1666. - QRinput::estimateBitsModeAn($q); // - 4 - la
  1667. if($dif < 0) {
  1668. break;
  1669. } else {
  1670. $p = $q;
  1671. }
  1672. } else {
  1673. $p++;
  1674. }
  1675. }
  1676. $run = $p;
  1677. if(!self::isalnumat($this->dataStr, $p)) {
  1678. $dif = QRinput::estimateBitsModeAn($run) + 4 + $la
  1679. + QRinput::estimateBitsMode8(1) // + 4 + l8
  1680. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  1681. if($dif > 0) {
  1682. return $this->eat8();
  1683. }
  1684. }
  1685. $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr));
  1686. if($ret < 0)
  1687. return -1;
  1688. return $run;
  1689. }
  1690. //----------------------------------------------------------------------
  1691. public function eatKanji()
  1692. {
  1693. $p = 0;
  1694. while($this->identifyMode($p) == QR_MODE_KANJI) {
  1695. $p += 2;
  1696. }
  1697. $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr));
  1698. if($ret < 0)
  1699. return -1;
  1700. return $run;
  1701. }
  1702. //----------------------------------------------------------------------
  1703. public function eat8()
  1704. {
  1705. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1706. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1707. $p = 1;
  1708. $dataStrLen = strlen($this->dataStr);
  1709. while($p < $dataStrLen) {
  1710. $mode = $this->identifyMode($p);
  1711. if($mode == QR_MODE_KANJI) {
  1712. break;
  1713. }
  1714. if($mode == QR_MODE_NUM) {
  1715. $q = $p;
  1716. while(self::isdigitat($this->dataStr, $q)) {
  1717. $q++;
  1718. }
  1719. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  1720. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1721. - QRinput::estimateBitsMode8($q); // - 4 - l8
  1722. if($dif < 0) {
  1723. break;
  1724. } else {
  1725. $p = $q;
  1726. }
  1727. } else if($mode == QR_MODE_AN) {
  1728. $q = $p;
  1729. while(self::isalnumat($this->dataStr, $q)) {
  1730. $q++;
  1731. }
  1732. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  1733. + QRinput::estimateBitsModeAn($q - $p) + 4 + $la
  1734. - QRinput::estimateBitsMode8($q); // - 4 - l8
  1735. if($dif < 0) {
  1736. break;
  1737. } else {
  1738. $p = $q;
  1739. }
  1740. } else {
  1741. $p++;
  1742. }
  1743. }
  1744. $run = $p;
  1745. $ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr));
  1746. if($ret < 0)
  1747. return -1;
  1748. return $run;
  1749. }
  1750. //----------------------------------------------------------------------
  1751. public function splitString()
  1752. {
  1753. while (strlen($this->dataStr) > 0)
  1754. {
  1755. if($this->dataStr == '')
  1756. return 0;
  1757. $mode = $this->identifyMode(0);
  1758. switch ($mode) {
  1759. case QR_MODE_NUM: $length = $this->eatNum(); break;
  1760. case QR_MODE_AN: $length = $this->eatAn(); break;
  1761. case QR_MODE_KANJI:
  1762. if ($hint == QR_MODE_KANJI)
  1763. $length = $this->eatKanji();
  1764. else $length = $this->eat8();
  1765. break;
  1766. default: $length = $this->eat8(); break;
  1767. }
  1768. if($length == 0) return 0;
  1769. if($length < 0) return -1;
  1770. $this->dataStr = substr($this->dataStr, $length);
  1771. }
  1772. }
  1773. //----------------------------------------------------------------------
  1774. public function toUpper()
  1775. {
  1776. $stringLen = strlen($this->dataStr);
  1777. $p = 0;
  1778. while ($p<$stringLen) {
  1779. $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
  1780. if($mode == QR_MODE_KANJI) {
  1781. $p += 2;
  1782. } else {
  1783. if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
  1784. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1785. }
  1786. $p++;
  1787. }
  1788. }
  1789. return $this->dataStr;
  1790. }
  1791. //----------------------------------------------------------------------
  1792. public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true)
  1793. {
  1794. if(is_null($string) || $string == '\0' || $string == '') {
  1795. throw new Exception('empty string!!!');
  1796. }
  1797. $split = new QRsplit($string, $input, $modeHint);
  1798. if(!$casesensitive)
  1799. $split->toUpper();
  1800. return $split->splitString();
  1801. }
  1802. }
  1803. //---- qrrscode.php -----------------------------
  1804. /*
  1805. * PHP QR Code encoder
  1806. *
  1807. * Reed-Solomon error correction support
  1808. *
  1809. * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
  1810. * (libfec is released under the GNU Lesser General Public License.)
  1811. *
  1812. * Based on libqrencode C library distributed under LGPL 2.1
  1813. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  1814. *
  1815. * PHP QR Code is distributed under LGPL 3
  1816. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  1817. *
  1818. * This library is free software; you can redistribute it and/or
  1819. * modify it under the terms of the GNU Lesser General Public
  1820. * License as published by the Free Software Foundation; either
  1821. * version 3 of the License, or any later version.
  1822. *
  1823. * This library is distributed in the hope that it will be useful,
  1824. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1825. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1826. * Lesser General Public License for more details.
  1827. *
  1828. * You should have received a copy of the GNU Lesser General Public
  1829. * License along with this library; if not, write to the Free Software
  1830. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1831. */
  1832. class QRrsItem {
  1833. public $mm; // Bits per symbol
  1834. public $nn; // Symbols per block (= (1<<mm)-1)
  1835. public $alpha_to = array(); // log lookup table
  1836. public $index_of = array(); // Antilog lookup table
  1837. public $genpoly = array(); // Generator polynomial
  1838. public $nroots; // Number of generator roots = number of parity symbols
  1839. public $fcr; // First consecutive root, index form
  1840. public $prim; // Primitive element, index form
  1841. public $iprim; // prim-th root of 1, index form
  1842. public $pad; // Padding bytes in shortened block
  1843. public $gfpoly;
  1844. //----------------------------------------------------------------------
  1845. public function modnn($x)
  1846. {
  1847. while ($x >= $this->nn) {
  1848. $x -= $this->nn;
  1849. $x = ($x >> $this->mm) + ($x & $this->nn);
  1850. }
  1851. return $x;
  1852. }
  1853. //----------------------------------------------------------------------
  1854. public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
  1855. {
  1856. // Common code for intializing a Reed-Solomon control block (char or int symbols)
  1857. // Copyright 2004 Phil Karn, KA9Q
  1858. // May be used under the terms of the GNU Lesser General Public License (LGPL)
  1859. $rs = null;
  1860. // Check parameter ranges
  1861. if($symsize < 0 || $symsize > 8) return $rs;
  1862. if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs;
  1863. if($prim <= 0 || $prim >= (1<<$symsize)) return $rs;
  1864. if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values!
  1865. if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding
  1866. $rs = new QRrsItem();
  1867. $rs->mm = $symsize;
  1868. $rs->nn = (1<<$symsize)-1;
  1869. $rs->pad = $pad;
  1870. $rs->alpha_to = array_fill(0, $rs->nn+1, 0);
  1871. $rs->index_of = array_fill(0, $rs->nn+1, 0);
  1872. // PHP style macro replacement ;)
  1873. $NN =& $rs->nn;
  1874. $A0 =& $NN;
  1875. // Generate Galois field lookup tables
  1876. $rs->index_of[0] = $A0; // log(zero) = -inf
  1877. $rs->alpha_to[$A0] = 0; // alpha**-inf = 0
  1878. $sr = 1;
  1879. for($i=0; $i<$rs->nn; $i++) {
  1880. $rs->index_of[$sr] = $i;
  1881. $rs->alpha_to[$i] = $sr;
  1882. $sr <<= 1;
  1883. if($sr & (1<<$symsize)) {
  1884. $sr ^= $gfpoly;
  1885. }
  1886. $sr &= $rs->nn;
  1887. }
  1888. if($sr != 1){
  1889. // field generator polynomial is not primitive!
  1890. $rs = NULL;
  1891. return $rs;
  1892. }
  1893. /* Form RS code generator polynomial from its roots */
  1894. $rs->genpoly = array_fill(0, $nroots+1, 0);
  1895. $rs->fcr = $fcr;
  1896. $rs->prim = $prim;
  1897. $rs->nroots = $nroots;
  1898. $rs->gfpoly = $gfpoly;
  1899. /* Find prim-th root of 1, used in decoding */
  1900. for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn)
  1901. ; // intentional empty-body loop!
  1902. $rs->iprim = (int)($iprim / $prim);
  1903. $rs->genpoly[0] = 1;
  1904. for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
  1905. $rs->genpoly[$i+1] = 1;
  1906. // Multiply rs->genpoly[] by @**(root + x)
  1907. for ($j = $i; $j > 0; $j--) {
  1908. if ($rs->genpoly[$j] != 0) {
  1909. $rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  1910. } else {
  1911. $rs->genpoly[$j] = $rs->genpoly[$j-1];
  1912. }
  1913. }
  1914. // rs->genpoly[0] can never be zero
  1915. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  1916. }
  1917. // convert rs->genpoly[] to index form for quicker encoding
  1918. for ($i = 0; $i <= $nroots; $i++)
  1919. $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  1920. return $rs;
  1921. }
  1922. //----------------------------------------------------------------------
  1923. public function encode_rs_char($data, &$parity)
  1924. {
  1925. $MM =& $this->mm;
  1926. $NN =& $this->nn;
  1927. $ALPHA_TO =& $this->alpha_to;
  1928. $INDEX_OF =& $this->index_of;
  1929. $GENPOLY =& $this->genpoly;
  1930. $NROOTS =& $this->nroots;
  1931. $FCR =& $this->fcr;
  1932. $PRIM =& $this->prim;
  1933. $IPRIM =& $this->iprim;
  1934. $PAD =& $this->pad;
  1935. $A0 =& $NN;
  1936. $parity = array_fill(0, $NROOTS, 0);
  1937. for($i=0; $i< ($NN-$NROOTS-$PAD); $i++) {
  1938. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  1939. if($feedback != $A0) {
  1940. // feedback term is non-zero
  1941. // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
  1942. // always be for the polynomials constructed by init_rs()
  1943. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  1944. for($j=1;$j<$NROOTS;$j++) {
  1945. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])];
  1946. }
  1947. }
  1948. // Shift
  1949. array_shift($parity);
  1950. if($feedback != $A0) {
  1951. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  1952. } else {
  1953. array_push($parity, 0);
  1954. }
  1955. }
  1956. }
  1957. }
  1958. //##########################################################################
  1959. class QRrs {
  1960. public static $items = array();
  1961. //----------------------------------------------------------------------
  1962. public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
  1963. {
  1964. foreach(self::$items as $rs) {
  1965. if($rs->pad != $pad) continue;
  1966. if($rs->nroots != $nroots) continue;
  1967. if($rs->mm != $symsize) continue;
  1968. if($rs->gfpoly != $gfpoly) continue;
  1969. if($rs->fcr != $fcr) continue;
  1970. if($rs->prim != $prim) continue;
  1971. return $rs;
  1972. }
  1973. $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  1974. array_unshift(self::$items, $rs);
  1975. return $rs;
  1976. }
  1977. }
  1978. //---- qrmask.php -----------------------------
  1979. /*
  1980. * PHP QR Code encoder
  1981. *
  1982. * Masking
  1983. *
  1984. * Based on libqrencode C library distributed under LGPL 2.1
  1985. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  1986. *
  1987. * PHP QR Code is distributed under LGPL 3
  1988. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  1989. *
  1990. * This library is free software; you can redistribute it and/or
  1991. * modify it under the terms of the GNU Lesser General Public
  1992. * License as published by the Free Software Foundation; either
  1993. * version 3 of the License, or any later version.
  1994. *
  1995. * This library is distributed in the hope that it will be useful,
  1996. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1997. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1998. * Lesser General Public License for more details.
  1999. *
  2000. * You should have received a copy of the GNU Lesser General Public
  2001. * License along with this library; if not, write to the Free Software
  2002. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  2003. */
  2004. define('N1', 3);
  2005. define('N2', 3);
  2006. define('N3', 40);
  2007. define('N4', 10);
  2008. class QRmask {
  2009. public $runLength = array();
  2010. //----------------------------------------------------------------------
  2011. public function __construct()
  2012. {
  2013. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  2014. }
  2015. //----------------------------------------------------------------------
  2016. public function writeFormatInformation($width, &$frame, $mask, $level)
  2017. {
  2018. $blacks = 0;
  2019. $format = QRspec::getFormatInfo($mask, $level);
  2020. for($i=0; $i<8; $i++) {
  2021. if($format & 1) {
  2022. $blacks += 2;
  2023. $v = 0x85;
  2024. } else {
  2025. $v = 0x84;
  2026. }
  2027. $frame[8][$width - 1 - $i] = chr($v);
  2028. if($i < 6) {
  2029. $frame[$i][8] = chr($v);
  2030. } else {
  2031. $frame[$i + 1][8] = chr($v);
  2032. }
  2033. $format = $format >> 1;
  2034. }
  2035. for($i=0; $i<7; $i++) {
  2036. if($format & 1) {
  2037. $blacks += 2;
  2038. $v = 0x85;
  2039. } else {
  2040. $v = 0x84;
  2041. }
  2042. $frame[$width - 7 + $i][8] = chr($v);
  2043. if($i == 0) {
  2044. $frame[8][7] = chr($v);
  2045. } else {
  2046. $frame[8][6 - $i] = chr($v);
  2047. }
  2048. $format = $format >> 1;
  2049. }
  2050. return $blacks;
  2051. }
  2052. //----------------------------------------------------------------------
  2053. public function mask0($x, $y) { return ($x+$y)&1; }
  2054. public function mask1($x, $y) { return ($y&1); }
  2055. public function mask2($x, $y) { return ($x%3); }
  2056. public function mask3($x, $y) { return ($x+$y)%3; }
  2057. public function mask4($x, $y) { return (((int)($y/2))+((int)($x/3)))&1; }
  2058. public function mask5($x, $y) { return (($x*$y)&1)+($x*$y)%3; }
  2059. public function mask6($x, $y) { return ((($x*$y)&1)+($x*$y)%3)&1; }
  2060. public function mask7($x, $y) { return ((($x*$y)%3)+(($x+$y)&1))&1; }
  2061. //----------------------------------------------------------------------
  2062. private function generateMaskNo($maskNo, $width, $frame)
  2063. {
  2064. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  2065. for($y=0; $y<$width; $y++) {
  2066. for($x=0; $x<$width; $x++) {
  2067. if(ord($frame[$y][$x]) & 0x80) {
  2068. $bitMask[$y][$x] = 0;
  2069. } else {
  2070. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  2071. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  2072. }
  2073. }
  2074. }
  2075. return $bitMask;
  2076. }
  2077. //----------------------------------------------------------------------
  2078. public static function serial($bitFrame)
  2079. {
  2080. $codeArr = array();
  2081. foreach ($bitFrame as $line)
  2082. $codeArr[] = join('', $line);
  2083. return gzcompress(join("\n", $codeArr), 9);
  2084. }
  2085. //----------------------------------------------------------------------
  2086. public static function unserial($code)
  2087. {
  2088. $codeArr = array();
  2089. $codeLines = explode("\n", gzuncompress($code));
  2090. foreach ($codeLines as $line)
  2091. $codeArr[] = str_split($line);
  2092. return $codeArr;
  2093. }
  2094. //----------------------------------------------------------------------
  2095. public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false)
  2096. {
  2097. $b = 0;
  2098. $bitMask = array();
  2099. $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat';
  2100. if (QR_CACHEABLE) {
  2101. if (file_exists($fileName)) {
  2102. $bitMask = self::unserial(file_get_contents($fileName));
  2103. } else {
  2104. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  2105. if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo))
  2106. mkdir(QR_CACHE_DIR.'mask_'.$maskNo);
  2107. file_put_contents($fileName, self::serial($bitMask));
  2108. }
  2109. } else {
  2110. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  2111. }
  2112. if ($maskGenOnly)
  2113. return;
  2114. $d = $s;
  2115. for($y=0; $y<$width; $y++) {
  2116. for($x=0; $x<$width; $x++) {
  2117. if($bitMask[$y][$x] == 1) {
  2118. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  2119. }
  2120. $b += (int)(ord($d[$y][$x]) & 1);
  2121. }
  2122. }
  2123. return $b;
  2124. }
  2125. //----------------------------------------------------------------------
  2126. public function makeMask($width, $frame, $maskNo, $level)
  2127. {
  2128. $masked = array_fill(0, $width, str_repeat("\0", $width));
  2129. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  2130. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  2131. return $masked;
  2132. }
  2133. //----------------------------------------------------------------------
  2134. public function calcN1N3($length)
  2135. {
  2136. $demerit = 0;
  2137. for($i=0; $i<$length; $i++) {
  2138. if($this->runLength[$i] >= 5) {
  2139. $demerit += (N1 + ($this->runLength[$i] - 5));
  2140. }
  2141. if($i & 1) {
  2142. if(($i >= 3) && ($i < ($length-2)) && ($this->runLength[$i] % 3 == 0)) {
  2143. $fact = (int)($this->runLength[$i] / 3);
  2144. if(($this->runLength[$i-2] == $fact) &&
  2145. ($this->runLength[$i-1] == $fact) &&
  2146. ($this->runLength[$i+1] == $fact) &&
  2147. ($this->runLength[$i+2] == $fact)) {
  2148. if(($this->runLength[$i-3] < 0) || ($this->runLength[$i-3] >= (4 * $fact))) {
  2149. $demerit += N3;
  2150. } else if((($i+3) >= $length) || ($this->runLength[$i+3] >= (4 * $fact))) {
  2151. $demerit += N3;
  2152. }
  2153. }
  2154. }
  2155. }
  2156. }
  2157. return $demerit;
  2158. }
  2159. //----------------------------------------------------------------------
  2160. public function evaluateSymbol($width, $frame)
  2161. {
  2162. $head = 0;
  2163. $demerit = 0;
  2164. for($y=0; $y<$width; $y++) {
  2165. $head = 0;
  2166. $this->runLength[0] = 1;
  2167. $frameY = $frame[$y];
  2168. if ($y>0)
  2169. $frameYM = $frame[$y-1];
  2170. for($x=0; $x<$width; $x++) {
  2171. if(($x > 0) && ($y > 0)) {
  2172. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  2173. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  2174. if(($b22 | ($w22 ^ 1))&1) {
  2175. $demerit += N2;
  2176. }
  2177. }
  2178. if(($x == 0) && (ord($frameY[$x]) & 1)) {
  2179. $this->runLength[0] = -1;
  2180. $head = 1;
  2181. $this->runLength[$head] = 1;
  2182. } else if($x > 0) {
  2183. if((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  2184. $head++;
  2185. $this->runLength[$head] = 1;
  2186. } else {
  2187. $this->runLength[$head]++;
  2188. }
  2189. }
  2190. }
  2191. $demerit += $this->calcN1N3($head+1);
  2192. }
  2193. for($x=0; $x<$width; $x++) {
  2194. $head = 0;
  2195. $this->runLength[0] = 1;
  2196. for($y=0; $y<$width; $y++) {
  2197. if($y == 0 && (ord($frame[$y][$x]) & 1)) {
  2198. $this->runLength[0] = -1;
  2199. $head = 1;
  2200. $this->runLength[$head] = 1;
  2201. } else if($y > 0) {
  2202. if((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  2203. $head++;
  2204. $this->runLength[$head] = 1;
  2205. } else {
  2206. $this->runLength[$head]++;
  2207. }
  2208. }
  2209. }
  2210. $demerit += $this->calcN1N3($head+1);
  2211. }
  2212. return $demerit;
  2213. }
  2214. //----------------------------------------------------------------------
  2215. public function mask($width, $frame, $level)
  2216. {
  2217. $minDemerit = PHP_INT_MAX;
  2218. $bestMaskNum = 0;
  2219. $bestMask = array();
  2220. $checked_masks = array(0,1,2,3,4,5,6,7);
  2221. if (QR_FIND_FROM_RANDOM !== false) {
  2222. $howManuOut = 8-(QR_FIND_FROM_RANDOM % 9);
  2223. for ($i = 0; $i < $howManuOut; $i++) {
  2224. $remPos = rand (0, count($checked_masks)-1);
  2225. unset($checked_masks[$remPos]);
  2226. $checked_masks = array_values($checked_masks);
  2227. }
  2228. }
  2229. $bestMask = $frame;
  2230. foreach($checked_masks as $i) {
  2231. $mask = array_fill(0, $width, str_repeat("\0", $width));
  2232. $demerit = 0;
  2233. $blacks = 0;
  2234. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  2235. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  2236. $blacks = (int)(100 * $blacks / ($width * $width));
  2237. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  2238. $demerit += $this->evaluateSymbol($width, $mask);
  2239. if($demerit < $minDemerit) {
  2240. $minDemerit = $demerit;
  2241. $bestMask = $mask;
  2242. $bestMaskNum = $i;
  2243. }
  2244. }
  2245. return $bestMask;
  2246. }
  2247. //----------------------------------------------------------------------
  2248. }
  2249. //---- qrencode.php -----------------------------
  2250. /*
  2251. * PHP QR Code encoder
  2252. *
  2253. * Main encoder classes.
  2254. *
  2255. * Based on libqrencode C library distributed under LGPL 2.1
  2256. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  2257. *
  2258. * PHP QR Code is distributed under LGPL 3
  2259. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  2260. *
  2261. * This library is free software; you can redistribute it and/or
  2262. * modify it under the terms of the GNU Lesser General Public
  2263. * License as published by the Free Software Foundation; either
  2264. * version 3 of the License, or any later version.
  2265. *
  2266. * This library is distributed in the hope that it will be useful,
  2267. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2268. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  2269. * Lesser General Public License for more details.
  2270. *
  2271. * You should have received a copy of the GNU Lesser General Public
  2272. * License along with this library; if not, write to the Free Software
  2273. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  2274. */
  2275. class QRrsblock {
  2276. public $dataLength;
  2277. public $data = array();
  2278. public $eccLength;
  2279. public $ecc = array();
  2280. public function __construct($dl, $data, $el, &$ecc, QRrsItem $rs)
  2281. {
  2282. $rs->encode_rs_char($data, $ecc);
  2283. $this->dataLength = $dl;
  2284. $this->data = $data;
  2285. $this->eccLength = $el;
  2286. $this->ecc = $ecc;
  2287. }
  2288. };
  2289. //##########################################################################
  2290. class QRrawcode {
  2291. public $version;
  2292. public $datacode = array();
  2293. public $ecccode = array();
  2294. public $blocks;
  2295. public $rsblocks = array(); //of RSblock
  2296. public $count;
  2297. public $dataLength;
  2298. public $eccLength;
  2299. public $b1;
  2300. //----------------------------------------------------------------------
  2301. public function __construct(QRinput $input)
  2302. {
  2303. $spec = array(0,0,0,0,0);
  2304. $this->datacode = $input->getByteStream();
  2305. if(is_null($this->datacode)) {
  2306. throw new Exception('null imput string');
  2307. }
  2308. QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
  2309. $this->version = $input->getVersion();
  2310. $this->b1 = QRspec::rsBlockNum1($spec);
  2311. $this->dataLength = QRspec::rsDataLength($spec);
  2312. $this->eccLength = QRspec::rsEccLength($spec);
  2313. $this->ecccode = array_fill(0, $this->eccLength, 0);
  2314. $this->blocks = QRspec::rsBlockNum($spec);
  2315. $ret = $this->init($spec);
  2316. if($ret < 0) {
  2317. throw new Exception('block alloc error');
  2318. return null;
  2319. }
  2320. $this->count = 0;
  2321. }
  2322. //----------------------------------------------------------------------
  2323. public function init(array $spec)
  2324. {
  2325. $dl = QRspec::rsDataCodes1($spec);
  2326. $el = QRspec::rsEccCodes1($spec);
  2327. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  2328. $blockNo = 0;
  2329. $dataPos = 0;
  2330. $eccPos = 0;
  2331. for($i=0; $i<QRspec::rsBlockNum1($spec); $i++) {
  2332. $ecc = array_slice($this->ecccode,$eccPos);
  2333. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  2334. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  2335. $dataPos += $dl;
  2336. $eccPos += $el;
  2337. $blockNo++;
  2338. }
  2339. if(QRspec::rsBlockNum2($spec) == 0)
  2340. return 0;
  2341. $dl = QRspec::rsDataCodes2($spec);
  2342. $el = QRspec::rsEccCodes2($spec);
  2343. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  2344. if($rs == NULL) return -1;
  2345. for($i=0; $i<QRspec::rsBlockNum2($spec); $i++) {
  2346. $ecc = array_slice($this->ecccode,$eccPos);
  2347. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  2348. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  2349. $dataPos += $dl;
  2350. $eccPos += $el;
  2351. $blockNo++;
  2352. }
  2353. return 0;
  2354. }
  2355. //----------------------------------------------------------------------
  2356. public function getCode()
  2357. {
  2358. $ret;
  2359. if($this->count < $this->dataLength) {
  2360. $row = $this->count % $this->blocks;
  2361. $col = $this->count / $this->blocks;
  2362. if($col >= $this->rsblocks[0]->dataLength) {
  2363. $row += $this->b1;
  2364. }
  2365. $ret = $this->rsblocks[$row]->data[$col];
  2366. } else if($this->count < $this->dataLength + $this->eccLength) {
  2367. $row = ($this->count - $this->dataLength) % $this->blocks;
  2368. $col = ($this->count - $this->dataLength) / $this->blocks;
  2369. $ret = $this->rsblocks[$row]->ecc[$col];
  2370. } else {
  2371. return 0;
  2372. }
  2373. $this->count++;
  2374. return $ret;
  2375. }
  2376. }
  2377. //##########################################################################
  2378. class QRcode {
  2379. public $version;
  2380. public $width;
  2381. public $data;
  2382. //----------------------------------------------------------------------
  2383. public function encodeMask(QRinput $input, $mask)
  2384. {
  2385. if($input->getVersion() < 0 || $input->getVersion() > QRSPEC_VERSION_MAX) {
  2386. throw new Exception('wrong version');
  2387. }
  2388. if($input->getErrorCorrectionLevel() > QR_ECLEVEL_H) {
  2389. throw new Exception('wrong level');
  2390. }
  2391. $raw = new QRrawcode($input);
  2392. QRtools::markTime('after_raw');
  2393. $version = $raw->version;
  2394. $width = QRspec::getWidth($version);
  2395. $frame = QRspec::newFrame($version);
  2396. $filler = new FrameFiller($width, $frame);
  2397. if(is_null($filler)) {
  2398. return NULL;
  2399. }
  2400. // inteleaved data and ecc codes
  2401. for($i=0; $i<$raw->dataLength + $raw->eccLength; $i++) {
  2402. $code = $raw->getCode();
  2403. $bit = 0x80;
  2404. for($j=0; $j<8; $j++) {
  2405. $addr = $filler->next();
  2406. $filler->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  2407. $bit = $bit >> 1;
  2408. }
  2409. }
  2410. QRtools::markTime('after_filler');
  2411. unset($raw);
  2412. // remainder bits
  2413. $j = QRspec::getRemainder($version);
  2414. for($i=0; $i<$j; $i++) {
  2415. $addr = $filler->next();
  2416. $filler->setFrameAt($addr, 0x02);
  2417. }
  2418. $frame = $filler->frame;
  2419. unset($filler);
  2420. // masking
  2421. $maskObj = new QRmask();
  2422. if($mask < 0) {
  2423. if (QR_FIND_BEST_MASK) {
  2424. $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
  2425. } else {
  2426. $masked = $maskObj->makeMask($width, $frame, (intval(QR_DEFAULT_MASK) % 8), $input->getErrorCorrectionLevel());
  2427. }
  2428. } else {
  2429. $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
  2430. }
  2431. if($masked == NULL) {
  2432. return NULL;
  2433. }
  2434. QRtools::markTime('after_mask');
  2435. $this->version = $version;
  2436. $this->width = $width;
  2437. $this->data = $masked;
  2438. return $this;
  2439. }
  2440. //----------------------------------------------------------------------
  2441. public function encodeInput(QRinput $input)
  2442. {
  2443. return $this->encodeMask($input, -1);
  2444. }
  2445. //----------------------------------------------------------------------
  2446. public function encodeString8bit($string, $version, $level)
  2447. {
  2448. if(string == NULL) {
  2449. throw new Exception('empty string!');
  2450. return NULL;
  2451. }
  2452. $input = new QRinput($version, $level);
  2453. if($input == NULL) return NULL;
  2454. $ret = $input->append($input, QR_MODE_8, strlen($string), str_split($string));
  2455. if($ret < 0) {
  2456. unset($input);
  2457. return NULL;
  2458. }
  2459. return $this->encodeInput($input);
  2460. }
  2461. //----------------------------------------------------------------------
  2462. public function encodeString($string, $version, $level, $hint, $casesensitive)
  2463. {
  2464. if($hint != QR_MODE_8 && $hint != QR_MODE_KANJI) {
  2465. throw new Exception('bad hint');
  2466. return NULL;
  2467. }
  2468. $input = new QRinput($version, $level);
  2469. if($input == NULL) return NULL;
  2470. $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
  2471. if($ret < 0) {
  2472. return NULL;
  2473. }
  2474. return $this->encodeInput($input);
  2475. }
  2476. //----------------------------------------------------------------------
  2477. public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
  2478. {
  2479. $enc = QRencode::factory($level, $size, $margin);
  2480. return $enc->encodePNG($text, $outfile, $saveandprint=false);
  2481. }
  2482. //----------------------------------------------------------------------
  2483. public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  2484. {
  2485. $enc = QRencode::factory($level, $size, $margin);
  2486. return $enc->encode($text, $outfile);
  2487. }
  2488. //----------------------------------------------------------------------
  2489. public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  2490. {
  2491. $enc = QRencode::factory($level, $size, $margin);
  2492. return $enc->encodeRAW($text, $outfile);
  2493. }
  2494. }
  2495. //##########################################################################
  2496. class FrameFiller {
  2497. public $width;
  2498. public $frame;
  2499. public $x;
  2500. public $y;
  2501. public $dir;
  2502. public $bit;
  2503. //----------------------------------------------------------------------
  2504. public function __construct($width, &$frame)
  2505. {
  2506. $this->width = $width;
  2507. $this->frame = $frame;
  2508. $this->x = $width - 1;
  2509. $this->y = $width - 1;
  2510. $this->dir = -1;
  2511. $this->bit = -1;
  2512. }
  2513. //----------------------------------------------------------------------
  2514. public function setFrameAt($at, $val)
  2515. {
  2516. $this->frame[$at['y']][$at['x']] = chr($val);
  2517. }
  2518. //----------------------------------------------------------------------
  2519. public function getFrameAt($at)
  2520. {
  2521. return ord($this->frame[$at['y']][$at['x']]);
  2522. }
  2523. //----------------------------------------------------------------------
  2524. public function next()
  2525. {
  2526. do {
  2527. if($this->bit == -1) {
  2528. $this->bit = 0;
  2529. return array('x'=>$this->x, 'y'=>$this->y);
  2530. }
  2531. $x = $this->x;
  2532. $y = $this->y;
  2533. $w = $this->width;
  2534. if($this->bit == 0) {
  2535. $x--;
  2536. $this->bit++;
  2537. } else {
  2538. $x++;
  2539. $y += $this->dir;
  2540. $this->bit--;
  2541. }
  2542. if($this->dir < 0) {
  2543. if($y < 0) {
  2544. $y = 0;
  2545. $x -= 2;
  2546. $this->dir = 1;
  2547. if($x == 6) {
  2548. $x--;
  2549. $y = 9;
  2550. }
  2551. }
  2552. } else {
  2553. if($y == $w) {
  2554. $y = $w - 1;
  2555. $x -= 2;
  2556. $this->dir = -1;
  2557. if($x == 6) {
  2558. $x--;
  2559. $y -= 8;
  2560. }
  2561. }
  2562. }
  2563. if($x < 0 || $y < 0) return null;
  2564. $this->x = $x;
  2565. $this->y = $y;
  2566. } while(ord($this->frame[$y][$x]) & 0x80);
  2567. return array('x'=>$x, 'y'=>$y);
  2568. }
  2569. } ;
  2570. //##########################################################################
  2571. class QRencode {
  2572. public $casesensitive = true;
  2573. public $eightbit = false;
  2574. public $version = 0;
  2575. public $size = 3;
  2576. public $margin = 4;
  2577. public $structured = 0; // not supported yet
  2578. public $level = QR_ECLEVEL_L;
  2579. public $hint = QR_MODE_8;
  2580. //----------------------------------------------------------------------
  2581. public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  2582. {
  2583. $enc = new QRencode();
  2584. $enc->size = $size;
  2585. $enc->margin = $margin;
  2586. switch ($level.'') {
  2587. case '0':
  2588. case '1':
  2589. case '2':
  2590. case '3':
  2591. $enc->level = $level;
  2592. break;
  2593. case 'l':
  2594. case 'L':
  2595. $enc->level = QR_ECLEVEL_L;
  2596. break;
  2597. case 'm':
  2598. case 'M':
  2599. $enc->level = QR_ECLEVEL_M;
  2600. break;
  2601. case 'q':
  2602. case 'Q':
  2603. $enc->level = QR_ECLEVEL_Q;
  2604. break;
  2605. case 'h':
  2606. case 'H':
  2607. $enc->level = QR_ECLEVEL_H;
  2608. break;
  2609. }
  2610. return $enc;
  2611. }
  2612. //----------------------------------------------------------------------
  2613. public function encodeRAW($intext, $outfile = false)
  2614. {
  2615. $code = new QRcode();
  2616. if($this->eightbit) {
  2617. $code->encodeString8bit($intext, $this->version, $this->level);
  2618. } else {
  2619. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  2620. }
  2621. return $code->data;
  2622. }
  2623. //----------------------------------------------------------------------
  2624. public function encode($intext, $outfile = false)
  2625. {
  2626. $code = new QRcode();
  2627. if($this->eightbit) {
  2628. $code->encodeString8bit($intext, $this->version, $this->level);
  2629. } else {
  2630. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  2631. }
  2632. QRtools::markTime('after_encode');
  2633. if ($outfile!== false) {
  2634. file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
  2635. } else {
  2636. return QRtools::binarize($code->data);
  2637. }
  2638. }
  2639. //----------------------------------------------------------------------
  2640. public function encodePNG($intext, $outfile = false,$saveandprint=false)
  2641. {
  2642. try {
  2643. ob_start();
  2644. $tab = $this->encode($intext);
  2645. $err = ob_get_contents();
  2646. ob_end_clean();
  2647. if ($err != '')
  2648. QRtools::log($outfile, $err);
  2649. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  2650. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
  2651. } catch (Exception $e) {
  2652. QRtools::log($outfile, $e->getMessage());
  2653. }
  2654. }
  2655. }