PageRenderTime 38ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/gravityforms/includes/phpqrcode/phpqrcode.php

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