PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/tcpdf/qrcode.php

https://bitbucket.org/speedealing/speedealing
PHP | 2866 lines | 1694 code | 208 blank | 964 comment | 294 complexity | abc2169d27147c89e70e4256a4b6e662 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

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

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