PageRenderTime 76ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/shared/code/qrcode.php

https://bitbucket.org/biadmin/tcexam
PHP | 2865 lines | 1688 code | 209 blank | 968 comment | 292 complexity | b57b0525d53b08c1843e739c0c51f351 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0

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

  1. <?php
  2. //============================================================+
  3. // File name : qrcode.php
  4. // Version : 1.0.004
  5. // Begin : 2010-03-22
  6. // Last Update : 2010-06-03
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2010-2010 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // DESCRIPTION :
  31. //
  32. // Class to create QR-code arrays for TCPDF class.
  33. // QR Code symbol is a 2D barcode that can be scanned by
  34. // handy terminals such as a mobile phone with CCD.
  35. // The capacity of QR Code is up to 7000 digits or 4000
  36. // characters, and has high robustness.
  37. // This class supports QR Code model 2, described in
  38. // JIS (Japanese Industrial Standards) X0510:2004
  39. // or ISO/IEC 18004.
  40. // Currently the following features are not supported:
  41. // ECI and FNC1 mode, Micro QR Code, QR Code model 1,
  42. // Structured mode.
  43. //
  44. // This class is derived from the following projects:
  45. // ---------------------------------------------------------
  46. // "PHP QR Code encoder"
  47. // License: GNU-LGPLv3
  48. // Copyright (C) 2010 by Dominik Dzienia <deltalab at poczta dot fm>
  49. // http://phpqrcode.sourceforge.net/
  50. // https://sourceforge.net/projects/phpqrcode/
  51. //
  52. // The "PHP QR Code encoder" is based on
  53. // "C libqrencode library" (ver. 3.1.1)
  54. // License: GNU-LGPL 2.1
  55. // Copyright (C) 2006-2010 by Kentaro Fukuchi
  56. // http://megaui.net/fukuchi/works/qrencode/index.en.html
  57. //
  58. // Reed-Solomon code encoder is written by Phil Karn, KA9Q.
  59. // Copyright (C) 2002-2006 Phil Karn, KA9Q
  60. //
  61. // QR Code is registered trademark of DENSO WAVE INCORPORATED
  62. // http://www.denso-wave.com/qrcode/index-e.html
  63. // ---------------------------------------------------------
  64. //============================================================+
  65. /**
  66. * Class to create QR-code arrays for TCPDF class.
  67. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  68. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  69. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  70. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  71. *
  72. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  73. * Please read comments on this class source file for full copyright and license information.
  74. *
  75. * @package com.tecnick.tcpdf
  76. * @abstract Class for generating QR-code array for TCPDF.
  77. * @author Nicola Asuni
  78. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  79. * @link http://www.tcpdf.org
  80. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  81. * @version 1.0.004
  82. */
  83. // definitions
  84. if (!defined('QRCODEDEFS')) {
  85. /**
  86. * Indicate that definitions for this class are set
  87. */
  88. define('QRCODEDEFS', true);
  89. // -----------------------------------------------------
  90. // Encoding modes (characters which can be encoded in QRcode)
  91. /**
  92. * Encoding mode
  93. */
  94. define('QR_MODE_NL', -1);
  95. /**
  96. * Encoding mode numeric (0-9). 3 characters are encoded to 10bit length. In theory, 7089 characters or less can be stored in a QRcode.
  97. */
  98. define('QR_MODE_NM', 0);
  99. /**
  100. * Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length. In theory, 4296 characters or less can be stored in a QRcode.
  101. */
  102. define('QR_MODE_AN', 1);
  103. /**
  104. * Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
  105. */
  106. define('QR_MODE_8B', 2);
  107. /**
  108. * Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length. In theory, 1817 characters or less can be stored in a QRcode.
  109. */
  110. define('QR_MODE_KJ', 3);
  111. /**
  112. * Encoding mode STRUCTURED (currently unsupported)
  113. */
  114. define('QR_MODE_ST', 4);
  115. // -----------------------------------------------------
  116. // Levels of error correction.
  117. // QRcode has a function of an error correcting for miss reading that white is black.
  118. // Error correcting is defined in 4 level as below.
  119. /**
  120. * Error correction level L : About 7% or less errors can be corrected.
  121. */
  122. define('QR_ECLEVEL_L', 0);
  123. /**
  124. * Error correction level M : About 15% or less errors can be corrected.
  125. */
  126. define('QR_ECLEVEL_M', 1);
  127. /**
  128. * Error correction level Q : About 25% or less errors can be corrected.
  129. */
  130. define('QR_ECLEVEL_Q', 2);
  131. /**
  132. * Error correction level H : About 30% or less errors can be corrected.
  133. */
  134. define('QR_ECLEVEL_H', 3);
  135. // -----------------------------------------------------
  136. // Version. Size of QRcode is defined as version.
  137. // Version is from 1 to 40.
  138. // Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
  139. // So version 40 is 177*177 matrix.
  140. /**
  141. * Maximum QR Code version.
  142. */
  143. define('QRSPEC_VERSION_MAX', 40);
  144. /**
  145. * Maximum matrix size for maximum version (version 40 is 177*177 matrix).
  146. */
  147. define('QRSPEC_WIDTH_MAX', 177);
  148. // -----------------------------------------------------
  149. /**
  150. * Matrix index to get width from $capacity array.
  151. */
  152. define('QRCAP_WIDTH', 0);
  153. /**
  154. * Matrix index to get number of words from $capacity array.
  155. */
  156. define('QRCAP_WORDS', 1);
  157. /**
  158. * Matrix index to get remainder from $capacity array.
  159. */
  160. define('QRCAP_REMINDER', 2);
  161. /**
  162. * Matrix index to get error correction level from $capacity array.
  163. */
  164. define('QRCAP_EC', 3);
  165. // -----------------------------------------------------
  166. // Structure (currently usupported)
  167. /**
  168. * Number of header bits for structured mode
  169. */
  170. define('STRUCTURE_HEADER_BITS', 20);
  171. /**
  172. * Max number of symbols for structured mode
  173. */
  174. define('MAX_STRUCTURED_SYMBOLS', 16);
  175. // -----------------------------------------------------
  176. // Masks
  177. /**
  178. * Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
  179. */
  180. define('N1', 3);
  181. /**
  182. * Down point base value for case 2 mask pattern (module block of same color)
  183. */
  184. define('N2', 3);
  185. /**
  186. * Down point base value for case 3 mask pattern (1:1:3:1:1(dark:bright:dark:bright:dark)pattern in a line or a column)
  187. */
  188. define('N3', 40);
  189. /**
  190. * Down point base value for case 4 mask pattern (ration of dark modules in whole)
  191. */
  192. define('N4', 10);
  193. // -----------------------------------------------------
  194. // Optimization settings
  195. /**
  196. * if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  197. */
  198. define('QR_FIND_BEST_MASK', true);
  199. /**
  200. * if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  201. */
  202. define('QR_FIND_FROM_RANDOM', 2);
  203. /**
  204. * when QR_FIND_BEST_MASK === false
  205. */
  206. define('QR_DEFAULT_MASK', 2);
  207. // -----------------------------------------------------
  208. } // end of definitions
  209. // #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
  210. if (!class_exists('QRcode', false)) {
  211. // for compatibility with PHP4
  212. if (!function_exists('str_split')) {
  213. /**
  214. * Convert a string to an array (needed for PHP4 compatibility)
  215. * @param string $string The input string.
  216. * @param int $split_length Maximum length of the chunk.
  217. * @return If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string , the entire string is returned as the first (and only) array element.
  218. */
  219. function str_split($string, $split_length=1) {
  220. if ((strlen($string) > $split_length) OR (!$split_length)) {
  221. do {
  222. $c = strlen($string);
  223. $parts[] = substr($string, 0, $split_length);
  224. $string = substr($string, $split_length);
  225. } while ($string !== false);
  226. } else {
  227. $parts = array($string);
  228. }
  229. return $parts;
  230. }
  231. }
  232. // #####################################################
  233. /**
  234. * Class to create QR-code arrays for TCPDF class.
  235. * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
  236. * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
  237. * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
  238. * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
  239. *
  240. * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
  241. * Please read comments on this class source file for full copyright and license information.
  242. *
  243. * @name QRcode
  244. * @package com.tecnick.tcpdf
  245. * @abstract Class for generating QR-code array for TCPDF.
  246. * @author Nicola Asuni
  247. * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  248. * @link http://www.tcpdf.org
  249. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  250. * @version 1.0.004
  251. */
  252. class QRcode {
  253. /**
  254. * @var barcode array to be returned which is readable by TCPDF
  255. * @access protected
  256. */
  257. protected $barcode_array = array();
  258. /**
  259. * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix.
  260. * @access protected
  261. */
  262. protected $version = 0;
  263. /**
  264. * @var Levels of error correction. See definitions for possible values.
  265. * @access protected
  266. */
  267. protected $level = QR_ECLEVEL_L;
  268. /**
  269. * @var Encoding mode
  270. * @access protected
  271. */
  272. protected $hint = QR_MODE_8B;
  273. /**
  274. * @var if true the input string will be converted to uppercase
  275. * @access protected
  276. */
  277. protected $casesensitive = true;
  278. /**
  279. * @var structured QR code (not supported yet)
  280. * @access protected
  281. */
  282. protected $structured = 0;
  283. /**
  284. * @var mask data
  285. * @access protected
  286. */
  287. protected $data;
  288. // FrameFiller
  289. /**
  290. * @var width
  291. * @access protected
  292. */
  293. protected $width;
  294. /**
  295. * @var frame
  296. * @access protected
  297. */
  298. protected $frame;
  299. /**
  300. * @var X position of bit
  301. * @access protected
  302. */
  303. protected $x;
  304. /**
  305. * @var Y position of bit
  306. * @access protected
  307. */
  308. protected $y;
  309. /**
  310. * @var direction
  311. * @access protected
  312. */
  313. protected $dir;
  314. /**
  315. * @var single bit
  316. * @access protected
  317. */
  318. protected $bit;
  319. // ---- QRrawcode ----
  320. /**
  321. * @var data code
  322. * @access protected
  323. */
  324. protected $datacode = array();
  325. /**
  326. * @var error correction code
  327. * @access protected
  328. */
  329. protected $ecccode = array();
  330. /**
  331. * @var blocks
  332. * @access protected
  333. */
  334. protected $blocks;
  335. /**
  336. * @var Reed-Solomon blocks
  337. * @access protected
  338. */
  339. protected $rsblocks = array(); //of RSblock
  340. /**
  341. * @var counter
  342. * @access protected
  343. */
  344. protected $count;
  345. /**
  346. * @var data length
  347. * @access protected
  348. */
  349. protected $dataLength;
  350. /**
  351. * @var error correction length
  352. * @access protected
  353. */
  354. protected $eccLength;
  355. /**
  356. * @var b1
  357. * @access protected
  358. */
  359. protected $b1;
  360. // ---- QRmask ----
  361. /**
  362. * @var run length
  363. * @access protected
  364. */
  365. protected $runLength = array();
  366. // ---- QRsplit ----
  367. /**
  368. * @var input data string
  369. * @access protected
  370. */
  371. protected $dataStr = '';
  372. /**
  373. * @var input items
  374. * @access protected
  375. */
  376. protected $items;
  377. // Reed-Solomon items
  378. /**
  379. * @var Reed-Solomon items
  380. * @access protected
  381. */
  382. protected $rsitems = array();
  383. /**
  384. * @var array of frames
  385. * @access protected
  386. */
  387. protected $frames = array();
  388. /**
  389. * @var alphabet-numeric convesion table
  390. * @access protected
  391. */
  392. protected $anTable = array(
  393. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  394. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  395. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, //
  396. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, //
  397. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
  398. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, //
  399. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
  400. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 //
  401. );
  402. /**
  403. * @var array Table of the capacity of symbols
  404. * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
  405. * @access protected
  406. */
  407. protected $capacity = array(
  408. array( 0, 0, 0, array( 0, 0, 0, 0)), //
  409. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  410. array( 25, 44, 7, array( 10, 16, 22, 28)), //
  411. array( 29, 70, 7, array( 15, 26, 36, 44)), //
  412. array( 33, 100, 7, array( 20, 36, 52, 64)), //
  413. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  414. array( 41, 172, 7, array( 36, 64, 96, 112)), //
  415. array( 45, 196, 0, array( 40, 72, 108, 130)), //
  416. array( 49, 242, 0, array( 48, 88, 132, 156)), //
  417. array( 53, 292, 0, array( 60, 110, 160, 192)), //
  418. array( 57, 346, 0, array( 72, 130, 192, 224)), // 10
  419. array( 61, 404, 0, array( 80, 150, 224, 264)), //
  420. array( 65, 466, 0, array( 96, 176, 260, 308)), //
  421. array( 69, 532, 0, array( 104, 198, 288, 352)), //
  422. array( 73, 581, 3, array( 120, 216, 320, 384)), //
  423. array( 77, 655, 3, array( 132, 240, 360, 432)), // 15
  424. array( 81, 733, 3, array( 144, 280, 408, 480)), //
  425. array( 85, 815, 3, array( 168, 308, 448, 532)), //
  426. array( 89, 901, 3, array( 180, 338, 504, 588)), //
  427. array( 93, 991, 3, array( 196, 364, 546, 650)), //
  428. array( 97, 1085, 3, array( 224, 416, 600, 700)), // 20
  429. array(101, 1156, 4, array( 224, 442, 644, 750)), //
  430. array(105, 1258, 4, array( 252, 476, 690, 816)), //
  431. array(109, 1364, 4, array( 270, 504, 750, 900)), //
  432. array(113, 1474, 4, array( 300, 560, 810, 960)), //
  433. array(117, 1588, 4, array( 312, 588, 870, 1050)), // 25
  434. array(121, 1706, 4, array( 336, 644, 952, 1110)), //
  435. array(125, 1828, 4, array( 360, 700, 1020, 1200)), //
  436. array(129, 1921, 3, array( 390, 728, 1050, 1260)), //
  437. array(133, 2051, 3, array( 420, 784, 1140, 1350)), //
  438. array(137, 2185, 3, array( 450, 812, 1200, 1440)), // 30
  439. array(141, 2323, 3, array( 480, 868, 1290, 1530)), //
  440. array(145, 2465, 3, array( 510, 924, 1350, 1620)), //
  441. array(149, 2611, 3, array( 540, 980, 1440, 1710)), //
  442. array(153, 2761, 3, array( 570, 1036, 1530, 1800)), //
  443. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), // 35
  444. array(161, 3034, 0, array( 600, 1120, 1680, 1980)), //
  445. array(165, 3196, 0, array( 630, 1204, 1770, 2100)), //
  446. array(169, 3362, 0, array( 660, 1260, 1860, 2220)), //
  447. array(173, 3532, 0, array( 720, 1316, 1950, 2310)), //
  448. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) // 40
  449. );
  450. /**
  451. * @var array Length indicator
  452. * @access protected
  453. */
  454. protected $lengthTableBits = array(
  455. array(10, 12, 14),
  456. array( 9, 11, 13),
  457. array( 8, 16, 16),
  458. array( 8, 10, 12)
  459. );
  460. /**
  461. * @var array Table of the error correction code (Reed-Solomon block)
  462. * See Table 12-16 (pp.30-36), JIS X0510:2004.
  463. * @access protected
  464. */
  465. protected $eccTable = array(
  466. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)), //
  467. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  468. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), //
  469. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)), //
  470. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)), //
  471. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  472. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)), //
  473. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)), //
  474. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)), //
  475. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)), //
  476. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), // 10
  477. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)), //
  478. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)), //
  479. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)), //
  480. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)), //
  481. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), // 15
  482. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)), //
  483. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)), //
  484. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)), //
  485. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)), //
  486. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), // 20
  487. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)), //
  488. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)), //
  489. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)), //
  490. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)), //
  491. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), // 25
  492. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)), //
  493. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)), //
  494. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)), //
  495. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)), //
  496. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30
  497. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)), //
  498. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)), //
  499. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)), //
  500. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)), //
  501. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), // 35
  502. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)), //
  503. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)), //
  504. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)), //
  505. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)), //
  506. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)) // 40
  507. );
  508. /**
  509. * @var array Positions of alignment patterns.
  510. * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them.
  511. * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
  512. * @access protected
  513. */
  514. protected $alignmentPattern = array(
  515. array( 0, 0),
  516. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  517. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  518. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15
  519. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20
  520. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25
  521. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30
  522. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35
  523. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) // 35-40
  524. );
  525. /**
  526. * @var array Version information pattern (BCH coded).
  527. * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
  528. * size: [QRSPEC_VERSION_MAX - 6]
  529. * @access protected
  530. */
  531. protected $versionPattern = array(
  532. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, //
  533. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, //
  534. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, //
  535. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, //
  536. 0x27541, 0x28c69
  537. );
  538. /**
  539. * @var array Format information
  540. * @access protected
  541. */
  542. protected $formatInfo = array(
  543. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), //
  544. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), //
  545. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), //
  546. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b) //
  547. );
  548. // -------------------------------------------------
  549. // -------------------------------------------------
  550. /**
  551. * This is the class constructor.
  552. * Creates a QRcode object
  553. * @param string $code code to represent using QRcode
  554. * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul>
  555. * @access public
  556. * @since 1.0.000
  557. */
  558. public function __construct($code, $eclevel = 'L') {
  559. $barcode_array = array();
  560. if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
  561. return false;
  562. }
  563. // set error correction level
  564. $this->level = array_search($eclevel, array('L', 'M', 'Q', 'H'));
  565. if ($this->level === false) {
  566. $this->level = QR_ECLEVEL_L;
  567. }
  568. if (($this->hint != QR_MODE_8B) AND ($this->hint != QR_MODE_KJ)) {
  569. return false;
  570. }
  571. if (($this->version < 0) OR ($this->version > QRSPEC_VERSION_MAX)) {
  572. return false;
  573. }
  574. $this->items = array();
  575. $this->encodeString($code);
  576. $qrTab = $this->binarize($this->data);
  577. $size = count($qrTab);
  578. $barcode_array['num_rows'] = $size;
  579. $barcode_array['num_cols'] = $size;
  580. $barcode_array['bcode'] = array();
  581. foreach ($qrTab as $line) {
  582. $arrAdd = array();
  583. foreach (str_split($line) as $char) {
  584. $arrAdd[] = ($char=='1')?1:0;
  585. }
  586. $barcode_array['bcode'][] = $arrAdd;
  587. }
  588. $this->barcode_array = $barcode_array;
  589. }
  590. /**
  591. * Returns a barcode array which is readable by TCPDF
  592. * @return array barcode array readable by TCPDF;
  593. * @access public
  594. */
  595. public function getBarcodeArray() {
  596. return $this->barcode_array;
  597. }
  598. /**
  599. * Convert the frame in binary form
  600. * @param array $frame array to binarize
  601. * @return array frame in binary form
  602. */
  603. protected function binarize($frame) {
  604. $len = count($frame);
  605. // the frame is square (width = height)
  606. foreach ($frame as &$frameLine) {
  607. for ($i=0; $i<$len; $i++) {
  608. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  609. }
  610. }
  611. return $frame;
  612. }
  613. /**
  614. * Encode the input string to QR code
  615. * @param string $string input string to encode
  616. */
  617. protected function encodeString($string) {
  618. $this->dataStr = $string;
  619. if (!$this->casesensitive) {
  620. $this->toUpper();
  621. }
  622. $ret = $this->splitString();
  623. if ($ret < 0) {
  624. return NULL;
  625. }
  626. $this->encodeMask(-1);
  627. }
  628. /**
  629. * Encode mask
  630. * @param int $mask masking mode
  631. */
  632. protected function encodeMask($mask) {
  633. $spec = array(0, 0, 0, 0, 0);
  634. $this->datacode = $this->getByteStream($this->items);
  635. if (is_null($this->datacode)) {
  636. return NULL;
  637. }
  638. $spec = $this->getEccSpec($this->version, $this->level, $spec);
  639. $this->b1 = $this->rsBlockNum1($spec);
  640. $this->dataLength = $this->rsDataLength($spec);
  641. $this->eccLength = $this->rsEccLength($spec);
  642. $this->ecccode = array_fill(0, $this->eccLength, 0);
  643. $this->blocks = $this->rsBlockNum($spec);
  644. $ret = $this->init($spec);
  645. if ($ret < 0) {
  646. return NULL;
  647. }
  648. $this->count = 0;
  649. $this->width = $this->getWidth($this->version);
  650. $this->frame = $this->newFrame($this->version);
  651. $this->x = $this->width - 1;
  652. $this->y = $this->width - 1;
  653. $this->dir = -1;
  654. $this->bit = -1;
  655. // inteleaved data and ecc codes
  656. for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
  657. $code = $this->getCode();
  658. $bit = 0x80;
  659. for ($j=0; $j<8; $j++) {
  660. $addr = $this->getNextPosition();
  661. $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  662. $bit = $bit >> 1;
  663. }
  664. }
  665. // remainder bits
  666. $j = $this->getRemainder($this->version);
  667. for ($i=0; $i<$j; $i++) {
  668. $addr = $this->getNextPosition();
  669. $this->setFrameAt($addr, 0x02);
  670. }
  671. // masking
  672. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  673. if ($mask < 0) {
  674. if (QR_FIND_BEST_MASK) {
  675. $masked = $this->mask($this->width, $this->frame, $this->level);
  676. } else {
  677. $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
  678. }
  679. } else {
  680. $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
  681. }
  682. if ($masked == NULL) {
  683. return NULL;
  684. }
  685. $this->data = $masked;
  686. }
  687. // - - - - - - - - - - - - - - - - - - - - - - - - -
  688. // FrameFiller
  689. /**
  690. * Set frame value at specified position
  691. * @param array $at x,y position
  692. * @param int $val value of the character to set
  693. */
  694. protected function setFrameAt($at, $val) {
  695. $this->frame[$at['y']][$at['x']] = chr($val);
  696. }
  697. /**
  698. * Get frame value at specified position
  699. * @param array $at x,y position
  700. * @return value at specified position
  701. */
  702. protected function getFrameAt($at) {
  703. return ord($this->frame[$at['y']][$at['x']]);
  704. }
  705. /**
  706. * Return the next frame position
  707. * @return array of x,y coordinates
  708. */
  709. protected function getNextPosition() {
  710. do {
  711. if ($this->bit == -1) {
  712. $this->bit = 0;
  713. return array('x'=>$this->x, 'y'=>$this->y);
  714. }
  715. $x = $this->x;
  716. $y = $this->y;
  717. $w = $this->width;
  718. if ($this->bit == 0) {
  719. $x--;
  720. $this->bit++;
  721. } else {
  722. $x++;
  723. $y += $this->dir;
  724. $this->bit--;
  725. }
  726. if ($this->dir < 0) {
  727. if ($y < 0) {
  728. $y = 0;
  729. $x -= 2;
  730. $this->dir = 1;
  731. if ($x == 6) {
  732. $x--;
  733. $y = 9;
  734. }
  735. }
  736. } else {
  737. if ($y == $w) {
  738. $y = $w - 1;
  739. $x -= 2;
  740. $this->dir = -1;
  741. if ($x == 6) {
  742. $x--;
  743. $y -= 8;
  744. }
  745. }
  746. }
  747. if (($x < 0) OR ($y < 0)) {
  748. return NULL;
  749. }
  750. $this->x = $x;
  751. $this->y = $y;
  752. } while(ord($this->frame[$y][$x]) & 0x80);
  753. return array('x'=>$x, 'y'=>$y);
  754. }
  755. // - - - - - - - - - - - - - - - - - - - - - - - - -
  756. // QRrawcode
  757. /**
  758. * Initialize code.
  759. * @param array $spec array of ECC specification
  760. * @return 0 in case of success, -1 in case of error
  761. */
  762. protected function init($spec) {
  763. $dl = $this->rsDataCodes1($spec);
  764. $el = $this->rsEccCodes1($spec);
  765. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  766. $blockNo = 0;
  767. $dataPos = 0;
  768. $eccPos = 0;
  769. $endfor = $this->rsBlockNum1($spec);
  770. for ($i=0; $i < $endfor; ++$i) {
  771. $ecc = array_slice($this->ecccode, $eccPos);
  772. $this->rsblocks[$blockNo] = array();
  773. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  774. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  775. $this->rsblocks[$blockNo]['eccLength'] = $el;
  776. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  777. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  778. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  779. $dataPos += $dl;
  780. $eccPos += $el;
  781. $blockNo++;
  782. }
  783. if ($this->rsBlockNum2($spec) == 0) {
  784. return 0;
  785. }
  786. $dl = $this->rsDataCodes2($spec);
  787. $el = $this->rsEccCodes2($spec);
  788. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  789. if ($rs == NULL) {
  790. return -1;
  791. }
  792. $endfor = $this->rsBlockNum2($spec);
  793. for ($i=0; $i < $endfor; ++$i) {
  794. $ecc = array_slice($this->ecccode, $eccPos);
  795. $this->rsblocks[$blockNo] = array();
  796. $this->rsblocks[$blockNo]['dataLength'] = $dl;
  797. $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
  798. $this->rsblocks[$blockNo]['eccLength'] = $el;
  799. $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
  800. $this->rsblocks[$blockNo]['ecc'] = $ecc;
  801. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  802. $dataPos += $dl;
  803. $eccPos += $el;
  804. $blockNo++;
  805. }
  806. return 0;
  807. }
  808. /**
  809. * Return Reed-Solomon block code.
  810. * @return array rsblocks
  811. */
  812. protected function getCode() {
  813. if ($this->count < $this->dataLength) {
  814. $row = $this->count % $this->blocks;
  815. $col = $this->count / $this->blocks;
  816. if ($col >= $this->rsblocks[0]['dataLength']) {
  817. $row += $this->b1;
  818. }
  819. $ret = $this->rsblocks[$row]['data'][$col];
  820. } elseif ($this->count < $this->dataLength + $this->eccLength) {
  821. $row = ($this->count - $this->dataLength) % $this->blocks;
  822. $col = ($this->count - $this->dataLength) / $this->blocks;
  823. $ret = $this->rsblocks[$row]['ecc'][$col];
  824. } else {
  825. return 0;
  826. }
  827. $this->count++;
  828. return $ret;
  829. }
  830. // - - - - - - - - - - - - - - - - - - - - - - - - -
  831. // QRmask
  832. /**
  833. * Write Format Information on frame and returns the number of black bits
  834. * @param int $width frame width
  835. * @param array $frame frame
  836. * @param array $mask masking mode
  837. * @param int $level error correction level
  838. * @return int blacks
  839. */
  840. protected function writeFormatInformation($width, &$frame, $mask, $level) {
  841. $blacks = 0;
  842. $format = $this->getFormatInfo($mask, $level);
  843. for ($i=0; $i<8; ++$i) {
  844. if ($format & 1) {
  845. $blacks += 2;
  846. $v = 0x85;
  847. } else {
  848. $v = 0x84;
  849. }
  850. $frame[8][$width - 1 - $i] = chr($v);
  851. if ($i < 6) {
  852. $frame[$i][8] = chr($v);
  853. } else {
  854. $frame[$i + 1][8] = chr($v);
  855. }
  856. $format = $format >> 1;
  857. }
  858. for ($i=0; $i<7; ++$i) {
  859. if ($format & 1) {
  860. $blacks += 2;
  861. $v = 0x85;
  862. } else {
  863. $v = 0x84;
  864. }
  865. $frame[$width - 7 + $i][8] = chr($v);
  866. if ($i == 0) {
  867. $frame[8][7] = chr($v);
  868. } else {
  869. $frame[8][6 - $i] = chr($v);
  870. }
  871. $format = $format >> 1;
  872. }
  873. return $blacks;
  874. }
  875. /**
  876. * mask0
  877. * @param int $x X position
  878. * @param int $y Y position
  879. * @return int mask
  880. */
  881. protected function mask0($x, $y) {
  882. return ($x + $y) & 1;
  883. }
  884. /**
  885. * mask1
  886. * @param int $x X position
  887. * @param int $y Y position
  888. * @return int mask
  889. */
  890. protected function mask1($x, $y) {
  891. return ($y & 1);
  892. }
  893. /**
  894. * mask2
  895. * @param int $x X position
  896. * @param int $y Y position
  897. * @return int mask
  898. */
  899. protected function mask2($x, $y) {
  900. return ($x % 3);
  901. }
  902. /**
  903. * mask3
  904. * @param int $x X position
  905. * @param int $y Y position
  906. * @return int mask
  907. */
  908. protected function mask3($x, $y) {
  909. return ($x + $y) % 3;
  910. }
  911. /**
  912. * mask4
  913. * @param int $x X position
  914. * @param int $y Y position
  915. * @return int mask
  916. */
  917. protected function mask4($x, $y) {
  918. return (((int)($y / 2)) + ((int)($x / 3))) & 1;
  919. }
  920. /**
  921. * mask5
  922. * @param int $x X position
  923. * @param int $y Y position
  924. * @return int mask
  925. */
  926. protected function mask5($x, $y) {
  927. return (($x * $y) & 1) + ($x * $y) % 3;
  928. }
  929. /**
  930. * mask6
  931. * @param int $x X position
  932. * @param int $y Y position
  933. * @return int mask
  934. */
  935. protected function mask6($x, $y) {
  936. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  937. }
  938. /**
  939. * mask7
  940. * @param int $x X position
  941. * @param int $y Y position
  942. * @return int mask
  943. */
  944. protected function mask7($x, $y) {
  945. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  946. }
  947. /**
  948. * Return bitmask
  949. * @param int $maskNo mask number
  950. * @param int $width width
  951. * @param array $frame frame
  952. * @return array bitmask
  953. */
  954. protected function generateMaskNo($maskNo, $width, $frame) {
  955. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  956. for ($y=0; $y<$width; ++$y) {
  957. for ($x=0; $x<$width; ++$x) {
  958. if (ord($frame[$y][$x]) & 0x80) {
  959. $bitMask[$y][$x] = 0;
  960. } else {
  961. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  962. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  963. }
  964. }
  965. }
  966. return $bitMask;
  967. }
  968. /**
  969. * makeMaskNo
  970. * @param int $maskNo
  971. * @param int $width
  972. * @param int $s
  973. * @param int $d
  974. * @param boolean $maskGenOnly
  975. * @return int b
  976. */
  977. protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
  978. $b = 0;
  979. $bitMask = array();
  980. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  981. if ($maskGenOnly) {
  982. return;
  983. }
  984. $d = $s;
  985. for ($y=0; $y<$width; ++$y) {
  986. for ($x=0; $x<$width; ++$x) {
  987. if ($bitMask[$y][$x] == 1) {
  988. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  989. }
  990. $b += (int)(ord($d[$y][$x]) & 1);
  991. }
  992. }
  993. return $b;
  994. }
  995. /**
  996. * makeMask
  997. * @param int $width
  998. * @param array $frame
  999. * @param int $maskNo
  1000. * @param int $level
  1001. * @return array mask
  1002. */
  1003. protected function makeMask($width, $frame, $maskNo, $level) {
  1004. $masked = array_fill(0, $width, str_repeat("\0", $width));
  1005. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  1006. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  1007. return $masked;
  1008. }
  1009. /**
  1010. * calcN1N3
  1011. * @param int $length
  1012. * @return int demerit
  1013. */
  1014. protected function calcN1N3($length) {
  1015. $demerit = 0;
  1016. for ($i=0; $i<$length; ++$i) {
  1017. if ($this->runLength[$i] >= 5) {
  1018. $demerit += (N1 + ($this->runLength[$i] - 5));
  1019. }
  1020. if ($i & 1) {
  1021. if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
  1022. $fact = (int)($this->runLength[$i] / 3);
  1023. if (($this->runLength[$i-2] == $fact)
  1024. AND ($this->runLength[$i-1] == $fact)
  1025. AND ($this->runLength[$i+1] == $fact)
  1026. AND ($this->runLength[$i+2] == $fact)) {
  1027. if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
  1028. $demerit += N3;
  1029. } elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
  1030. $demerit += N3;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return $demerit;
  1037. }
  1038. /**
  1039. * evaluateSymbol
  1040. * @param int $width
  1041. * @param array $frame
  1042. * @return int demerit
  1043. */
  1044. protected function evaluateSymbol($width, $frame) {
  1045. $head = 0;
  1046. $demerit = 0;
  1047. for ($y=0; $y<$width; ++$y) {
  1048. $head = 0;
  1049. $this->runLength[0] = 1;
  1050. $frameY = $frame[$y];
  1051. if ($y > 0) {
  1052. $frameYM = $frame[$y-1];
  1053. }
  1054. for ($x=0; $x<$width; ++$x) {
  1055. if (($x > 0) AND ($y > 0)) {
  1056. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  1057. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  1058. if (($b22 | ($w22 ^ 1)) & 1) {
  1059. $demerit += N2;
  1060. }
  1061. }
  1062. if (($x == 0) AND (ord($frameY[$x]) & 1)) {
  1063. $this->runLength[0] = -1;
  1064. $head = 1;
  1065. $this->runLength[$head] = 1;
  1066. } elseif ($x > 0) {
  1067. if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  1068. $head++;
  1069. $this->runLength[$head] = 1;
  1070. } else {
  1071. $this->runLength[$head]++;
  1072. }
  1073. }
  1074. }
  1075. $demerit += $this->calcN1N3($head+1);
  1076. }
  1077. for ($x=0; $x<$width; ++$x) {
  1078. $head = 0;
  1079. $this->runLength[0] = 1;
  1080. for ($y=0; $y<$width; ++$y) {
  1081. if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
  1082. $this->runLength[0] = -1;
  1083. $head = 1;
  1084. $this->runLength[$head] = 1;
  1085. } elseif ($y > 0) {
  1086. if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  1087. $head++;
  1088. $this->runLength[$head] = 1;
  1089. } else {
  1090. $this->runLength[$head]++;
  1091. }
  1092. }
  1093. }
  1094. $demerit += $this->calcN1N3($head+1);
  1095. }
  1096. return $demerit;
  1097. }
  1098. /**
  1099. * mask
  1100. * @param int $width
  1101. * @param array $frame
  1102. * @param int $level
  1103. * @return array best mask
  1104. */
  1105. protected function mask($width, $frame, $level) {
  1106. $minDemerit = PHP_INT_MAX;
  1107. $bestMaskNum = 0;
  1108. $bestMask = array();
  1109. $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
  1110. if (QR_FIND_FROM_RANDOM !== false) {
  1111. $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
  1112. for ($i = 0; $i < $howManuOut; ++$i) {
  1113. $remPos = rand (0, count($checked_masks)-1);
  1114. unset($checked_masks[$remPos]);
  1115. $checked_masks = array_values($checked_masks);
  1116. }
  1117. }
  1118. $bestMask = $frame;
  1119. foreach ($checked_masks as $i) {
  1120. $mask = array_fill(0, $width, str_repeat("\0", $width));
  1121. $demerit = 0;
  1122. $blacks = 0;
  1123. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  1124. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  1125. $blacks = (int)(100 * $blacks / ($width * $width));
  1126. $demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
  1127. $demerit += $this->evaluateSymbol($width, $mask);
  1128. if ($demerit < $minDemerit) {
  1129. $minDemerit = $demerit;
  1130. $bestMask = $mask;
  1131. $bestMaskNum = $i;
  1132. }
  1133. }
  1134. return $bestMask;
  1135. }
  1136. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1137. // QRsplit
  1138. /**
  1139. * Return true if the character at specified position is a number
  1140. * @param string $str string
  1141. * @param int $pos characted position
  1142. * @return boolean true of false
  1143. */
  1144. protected function isdigitat($str, $pos) {
  1145. if ($pos >= strlen($str)) {
  1146. return false;
  1147. }
  1148. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1149. }
  1150. /**
  1151. * Return true if the character at specified position is an alphanumeric character
  1152. * @param string $str string
  1153. * @param int $pos characted position
  1154. * @return boolean true of false
  1155. */
  1156. protected function isalnumat($str, $pos) {
  1157. if ($pos >= strlen($str)) {
  1158. return false;
  1159. }
  1160. return ($this->lookAnTable(ord($str[$pos])) >= 0);
  1161. }
  1162. /**
  1163. * identifyMode
  1164. * @param int $pos
  1165. * @return int mode
  1166. */
  1167. protected function identifyMode($pos) {
  1168. if ($pos >= strlen($this->dataStr)) {
  1169. return QR_MODE_NL;
  1170. }
  1171. $c = $this->dataStr[$pos];
  1172. if ($this->isdigitat($this->dataStr, $pos)) {
  1173. return QR_MODE_NM;
  1174. } elseif ($this->isalnumat($this->dataStr, $pos)) {
  1175. return QR_MODE_AN;
  1176. } elseif ($this->hint == QR_MODE_KJ) {
  1177. if ($pos+1 < strlen($this->dataStr)) {
  1178. $d = $this->dataStr[$pos+1];
  1179. $word = (ord($c) << 8) | ord($d);
  1180. if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
  1181. return QR_MODE_KJ;
  1182. }
  1183. }
  1184. }
  1185. return QR_MODE_8B;
  1186. }
  1187. /**
  1188. * eatNum
  1189. * @return int run
  1190. */
  1191. protected function eatNum() {
  1192. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1193. $p = 0;
  1194. while($this->isdigitat($this->dataStr, $p)) {
  1195. $p++;
  1196. }
  1197. $run = $p;
  1198. $mode = $this->identifyMode($p);
  1199. if ($mode == QR_MODE_8B) {
  1200. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1201. + $this->estimateBitsMode8(1) // + 4 + l8
  1202. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1203. if ($dif > 0) {
  1204. return $this->eat8();
  1205. }
  1206. }
  1207. if ($mode == QR_MODE_AN) {
  1208. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1209. + $this->estimateBitsModeAn(1) // + 4 + la
  1210. - $this->estimateBitsModeAn($run + 1);// - 4 - la
  1211. if ($dif > 0) {
  1212. return $this->eatAn();
  1213. }
  1214. }
  1215. $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
  1216. return $run;
  1217. }
  1218. /**
  1219. * eatAn
  1220. * @return int run
  1221. */
  1222. protected function eatAn() {
  1223. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1224. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1225. $p = 0;
  1226. while($this->isalnumat($this->dataStr, $p)) {
  1227. if ($this->isdigitat($this->dataStr, $p)) {
  1228. $q = $p;
  1229. while($this->isdigitat($this->dataStr, $q)) {
  1230. $q++;
  1231. }
  1232. $dif = $this->estimateBitsModeAn($p) // + 4 + la
  1233. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1234. - $this->estimateBitsModeAn($q); // - 4 - la
  1235. if ($dif < 0) {
  1236. break;
  1237. } else {
  1238. $p = $q;
  1239. }
  1240. } else {
  1241. $p++;
  1242. }
  1243. }
  1244. $run = $p;
  1245. if (!$this->isalnumat($this->dataStr, $p)) {
  1246. $dif = $this->estimateBitsModeAn($run) + 4 + $la
  1247. + $this->estimateBitsMode8(1) // + 4 + l8
  1248. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1249. if ($dif > 0) {
  1250. return $this->eat8();
  1251. }
  1252. }
  1253. $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
  1254. return $run;
  1255. }
  1256. /**
  1257. * eatKanji
  1258. * @return int run
  1259. */
  1260. protected function eatKanji() {
  1261. $p = 0;
  1262. while($this->identifyMode($p) == QR_MODE_KJ) {
  1263. $p += 2;
  1264. }
  1265. $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
  1266. return $run;
  1267. }
  1268. /**
  1269. * eat8
  1270. * @return int run
  1271. */
  1272. protected function eat8() {
  1273. $la = $this->lengthIndicator(QR_MODE_AN, $this->version);
  1274. $ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
  1275. $p = 1;
  1276. $dataStrLen = strlen($this->dataStr);
  1277. while($p < $dataStrLen) {
  1278. $mode = $this->identifyMode($p);
  1279. if ($mode == QR_MODE_KJ) {
  1280. break;
  1281. }
  1282. if ($mode == QR_MODE_NM) {
  1283. $q = $p;
  1284. while($this->isdigitat($this->dataStr, $q)) {
  1285. $q++;
  1286. }
  1287. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1288. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1289. - $this->estimateBitsMode8($q); // - 4 - l8
  1290. if ($dif < 0) {
  1291. break;
  1292. } else {
  1293. $p = $q;
  1294. }
  1295. } elseif ($mode == QR_MODE_AN) {
  1296. $q = $p;
  1297. while($this->isalnumat($this->dataStr, $q)) {
  1298. $q++;
  1299. }
  1300. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1301. + $this->estimateBitsModeAn($q - $p) + 4 + $la
  1302. - $this->estimateBitsMode8($q); // - 4 - l8
  1303. if ($dif < 0) {
  1304. break;
  1305. } else {
  1306. $p = $q;
  1307. }
  1308. } else {
  1309. $p++;
  1310. }
  1311. }
  1312. $run = $p;
  1313. $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
  1314. return $run;
  1315. }
  1316. /**
  1317. * splitString
  1318. */
  1319. protected function splitString() {
  1320. while (strlen($this->dataStr) > 0) {
  1321. if ($this->dataStr == '') {
  1322. return 0;
  1323. }
  1324. $mode = $this->identifyMode(0);
  1325. switch ($mode) {
  1326. case QR_MODE_NM: {
  1327. $length = $this->eatNum();
  1328. break;
  1329. }
  1330. case QR_MODE_AN: {
  1331. $length = $this->eatAn();
  1332. break;
  1333. }
  1334. case QR_MODE_KJ: {
  1335. if ($hint == QR_MODE_KJ) {
  1336. $length = $this->eatKanji();
  1337. } else {
  1338. $length = $this->eat8();
  1339. }
  1340. break;
  1341. }
  1342. default: {
  1343. $length = $this->eat8();
  1344. break;
  1345. }
  1346. }
  1347. if ($length == 0) {
  1348. return 0;
  1349. }
  1350. if ($length < 0) {
  1351. return -1;
  1352. }
  1353. $this->dataStr = substr($this->dataStr, $length);
  1354. }
  1355. }
  1356. /**
  1357. * toUpper
  1358. */
  1359. protected function toUpper() {
  1360. $stringLen = strlen($this->dataStr);
  1361. $p = 0;
  1362. while ($p < $stringLen) {
  1363. $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
  1364. if ($mode == QR_MODE_KJ) {
  1365. $p += 2;
  1366. } else {
  1367. if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
  1368. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1369. }
  1370. $p++;
  1371. }
  1372. }
  1373. return $this->dataStr;
  1374. }
  1375. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1376. // QRinputItem
  1377. /**
  1378. * newInputItem
  1379. * @param int $mode
  1380. * @param int $size
  1381. * @param array $data
  1382. * @param array $bstream
  1383. * @return array input item
  1384. */
  1385. protected function newInputItem($mode, $size, $data, $bstream=null) {
  1386. $setData = array_slice($data, 0, $size);
  1387. if (count($setData) < $size) {
  1388. $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
  1389. }
  1390. if (!$this->check($mode, $size, $setData)) {
  1391. return NULL;
  1392. }
  1393. $inputitem = array();
  1394. $inputitem['mode'] = $mode;
  1395. $inputitem['size'] = $size;
  1396. $inputitem['data'] = $setData;
  1397. $inputitem['bstream'] = $bstream;
  1398. return $inputitem;
  1399. }
  1400. /**
  1401. * encodeModeNum
  1402. * @param array $inputitem
  1403. * @param int $version
  1404. * @return array input item
  1405. */
  1406. protected function encodeModeNum($inputitem, $version) {
  1407. $words = (int)($inputitem['size'] / 3);
  1408. $inputitem['bstream'] = array();
  1409. $val = 0x1;
  1410. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1411. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
  1412. for ($i=0; $i < $words; ++$i) {
  1413. $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
  1414. $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
  1415. $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
  1416. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
  1417. }
  1418. if ($inputitem['size'] - $words * 3 == 1) {
  1419. $val = ord($inputitem['data'][$words*3]) - ord('0');
  1420. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1421. } elseif (($inputitem['size'] - ($words * 3)) == 2) {
  1422. $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
  1423. $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
  1424. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
  1425. }
  1426. return $inputitem;
  1427. }
  1428. /**
  1429. * encodeModeAn
  1430. * @param array $inputitem
  1431. * @param int $version
  1432. * @return array input item
  1433. */
  1434. protected function encodeModeAn($inputitem, $version) {
  1435. $words = (int)($inputitem['size'] / 2);
  1436. $inputitem['bstream'] = array();
  1437. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
  1438. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
  1439. for ($i=0; $i < $words; ++$i) {
  1440. $val = (int)$this->lookAnTable(ord($inputitem['data'][$i*2 ])) * 45;
  1441. $val += (int)$this->lookAnTable(ord($inputitem['data'][$i*2+1]));
  1442. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
  1443. }
  1444. if ($inputitem['size'] & 1) {
  1445. $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
  1446. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
  1447. }
  1448. return $inputitem;
  1449. }
  1450. /**
  1451. * encodeMode8
  1452. * @param array $inputitem
  1453. * @param int $version
  1454. * @return array input item
  1455. */
  1456. protected function encodeMode8($inputitem, $version) {
  1457. $inputitem['bstream'] = array();
  1458. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
  1459. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
  1460. for ($i=0; $i < $inputitem['size']; ++$i) {
  1461. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
  1462. }
  1463. return $inputitem;
  1464. }
  1465. /**
  1466. * encodeModeKanji
  1467. * @param array $inputitem
  1468. * @param …

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