PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/cookbook/pmwiki2pdf-v2/html2pdf_v4.03/_tcpdf_5.0.002/qrcode.php

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

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