PageRenderTime 49ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/phpqrcode/phpqrcode.php

https://github.com/iarna/Tiny-Tiny-RSS
PHP | 3312 lines | 2160 code | 657 blank | 495 comment | 417 complexity | e5b5b9f80640d4747ad995f43ccaea16 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0

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

  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * This file contains MERGED version of PHP QR Code library.
  6. * It was auto-generated from full version for your convenience.
  7. *
  8. * This merged version was configured to not requre any external files,
  9. * with disabled cache, error loging and weker but faster mask matching.
  10. * If you need tune it up please use non-merged version.
  11. *
  12. * For full version, documentation, examples of use please visit:
  13. *
  14. * http://phpqrcode.sourceforge.net/
  15. * https://sourceforge.net/projects/phpqrcode/
  16. *
  17. * PHP QR Code is distributed under LGPL 3
  18. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  19. *
  20. * This library is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU Lesser General Public
  22. * License as published by the Free Software Foundation; either
  23. * version 3 of the License, or any later version.
  24. *
  25. * This library is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * Lesser General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Lesser General Public
  31. * License along with this library; if not, write to the Free Software
  32. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  33. */
  34. /*
  35. * Version: 1.1.4
  36. * Build: 2010100721
  37. */
  38. //---- qrconst.php -----------------------------
  39. /*
  40. * PHP QR Code encoder
  41. *
  42. * Common constants
  43. *
  44. * Based on libqrencode C library distributed under LGPL 2.1
  45. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  46. *
  47. * PHP QR Code is distributed under LGPL 3
  48. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  49. *
  50. * This library is free software; you can redistribute it and/or
  51. * modify it under the terms of the GNU Lesser General Public
  52. * License as published by the Free Software Foundation; either
  53. * version 3 of the License, or any later version.
  54. *
  55. * This library is distributed in the hope that it will be useful,
  56. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  57. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  58. * Lesser General Public License for more details.
  59. *
  60. * You should have received a copy of the GNU Lesser General Public
  61. * License along with this library; if not, write to the Free Software
  62. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  63. */
  64. // Encoding modes
  65. define('QR_MODE_NUL', -1);
  66. define('QR_MODE_NUM', 0);
  67. define('QR_MODE_AN', 1);
  68. define('QR_MODE_8', 2);
  69. define('QR_MODE_KANJI', 3);
  70. define('QR_MODE_STRUCTURE', 4);
  71. // Levels of error correction.
  72. define('QR_ECLEVEL_L', 0);
  73. define('QR_ECLEVEL_M', 1);
  74. define('QR_ECLEVEL_Q', 2);
  75. define('QR_ECLEVEL_H', 3);
  76. // Supported output formats
  77. define('QR_FORMAT_TEXT', 0);
  78. define('QR_FORMAT_PNG', 1);
  79. class qrstr {
  80. public static function set(&$srctab, $x, $y, $repl, $replLen = false) {
  81. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  82. }
  83. }
  84. //---- merged_config.php -----------------------------
  85. /*
  86. * PHP QR Code encoder
  87. *
  88. * Config file, tuned-up for merged verion
  89. */
  90. define('QR_CACHEABLE', false); // use cache - more disk reads but less CPU power, masks and format templates are stored there
  91. define('QR_CACHE_DIR', false); // used when QR_CACHEABLE === true
  92. define('QR_LOG_DIR', false); // default error logs dir
  93. define('QR_FIND_BEST_MASK', true); // if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
  94. define('QR_FIND_FROM_RANDOM', 2); // if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
  95. define('QR_DEFAULT_MASK', 2); // when QR_FIND_BEST_MASK === false
  96. define('QR_PNG_MAXIMUM_SIZE', 1024); // maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images
  97. //---- qrtools.php -----------------------------
  98. /*
  99. * PHP QR Code encoder
  100. *
  101. * Toolset, handy and debug utilites.
  102. *
  103. * PHP QR Code is distributed under LGPL 3
  104. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  105. *
  106. * This library is free software; you can redistribute it and/or
  107. * modify it under the terms of the GNU Lesser General Public
  108. * License as published by the Free Software Foundation; either
  109. * version 3 of the License, or any later version.
  110. *
  111. * This library is distributed in the hope that it will be useful,
  112. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  113. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  114. * Lesser General Public License for more details.
  115. *
  116. * You should have received a copy of the GNU Lesser General Public
  117. * License along with this library; if not, write to the Free Software
  118. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  119. */
  120. class QRtools {
  121. //----------------------------------------------------------------------
  122. public static function binarize($frame)
  123. {
  124. $len = count($frame);
  125. foreach ($frame as &$frameLine) {
  126. for($i=0; $i<$len; $i++) {
  127. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  128. }
  129. }
  130. return $frame;
  131. }
  132. //----------------------------------------------------------------------
  133. public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037')
  134. {
  135. $barcode_array = array();
  136. if (!is_array($mode))
  137. $mode = explode(',', $mode);
  138. $eccLevel = 'L';
  139. if (count($mode) > 1) {
  140. $eccLevel = $mode[1];
  141. }
  142. $qrTab = QRcode::text($code, false, $eccLevel);
  143. $size = count($qrTab);
  144. $barcode_array['num_rows'] = $size;
  145. $barcode_array['num_cols'] = $size;
  146. $barcode_array['bcode'] = array();
  147. foreach ($qrTab as $line) {
  148. $arrAdd = array();
  149. foreach(str_split($line) as $char)
  150. $arrAdd[] = ($char=='1')?1:0;
  151. $barcode_array['bcode'][] = $arrAdd;
  152. }
  153. return $barcode_array;
  154. }
  155. //----------------------------------------------------------------------
  156. public static function clearCache()
  157. {
  158. self::$frames = array();
  159. }
  160. //----------------------------------------------------------------------
  161. public static function buildCache()
  162. {
  163. QRtools::markTime('before_build_cache');
  164. $mask = new QRmask();
  165. for ($a=1; $a <= QRSPEC_VERSION_MAX; $a++) {
  166. $frame = QRspec::newFrame($a);
  167. if (QR_IMAGE) {
  168. $fileName = QR_CACHE_DIR.'frame_'.$a.'.png';
  169. QRimage::png(self::binarize($frame), $fileName, 1, 0);
  170. }
  171. $width = count($frame);
  172. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  173. for ($maskNo=0; $maskNo<8; $maskNo++)
  174. $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
  175. }
  176. QRtools::markTime('after_build_cache');
  177. }
  178. //----------------------------------------------------------------------
  179. public static function log($outfile, $err)
  180. {
  181. if (QR_LOG_DIR !== false) {
  182. if ($err != '') {
  183. if ($outfile !== false) {
  184. file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  185. } else {
  186. file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  187. }
  188. }
  189. }
  190. }
  191. //----------------------------------------------------------------------
  192. public static function dumpMask($frame)
  193. {
  194. $width = count($frame);
  195. for($y=0;$y<$width;$y++) {
  196. for($x=0;$x<$width;$x++) {
  197. echo ord($frame[$y][$x]).',';
  198. }
  199. }
  200. }
  201. //----------------------------------------------------------------------
  202. public static function markTime($markerId)
  203. {
  204. list($usec, $sec) = explode(" ", microtime());
  205. $time = ((float)$usec + (float)$sec);
  206. if (!isset($GLOBALS['qr_time_bench']))
  207. $GLOBALS['qr_time_bench'] = array();
  208. $GLOBALS['qr_time_bench'][$markerId] = $time;
  209. }
  210. //----------------------------------------------------------------------
  211. public static function timeBenchmark()
  212. {
  213. self::markTime('finish');
  214. $lastTime = 0;
  215. $startTime = 0;
  216. $p = 0;
  217. echo '<table cellpadding="3" cellspacing="1">
  218. <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead>
  219. <tbody>';
  220. foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) {
  221. if ($p > 0) {
  222. echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, 6).'s</td></tr>';
  223. } else {
  224. $startTime = $thisTime;
  225. }
  226. $p++;
  227. $lastTime = $thisTime;
  228. }
  229. echo '</tbody><tfoot>
  230. <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr>
  231. </tfoot>
  232. </table>';
  233. }
  234. }
  235. //##########################################################################
  236. QRtools::markTime('start');
  237. //---- qrspec.php -----------------------------
  238. /*
  239. * PHP QR Code encoder
  240. *
  241. * QR Code specifications
  242. *
  243. * Based on libqrencode C library distributed under LGPL 2.1
  244. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  245. *
  246. * PHP QR Code is distributed under LGPL 3
  247. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  248. *
  249. * The following data / specifications are taken from
  250. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  251. * or
  252. * "Automatic identification and data capture techniques --
  253. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  254. *
  255. * This library is free software; you can redistribute it and/or
  256. * modify it under the terms of the GNU Lesser General Public
  257. * License as published by the Free Software Foundation; either
  258. * version 3 of the License, or any later version.
  259. *
  260. * This library is distributed in the hope that it will be useful,
  261. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  262. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  263. * Lesser General Public License for more details.
  264. *
  265. * You should have received a copy of the GNU Lesser General Public
  266. * License along with this library; if not, write to the Free Software
  267. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  268. */
  269. define('QRSPEC_VERSION_MAX', 40);
  270. define('QRSPEC_WIDTH_MAX', 177);
  271. define('QRCAP_WIDTH', 0);
  272. define('QRCAP_WORDS', 1);
  273. define('QRCAP_REMINDER', 2);
  274. define('QRCAP_EC', 3);
  275. class QRspec {
  276. public static $capacity = array(
  277. array( 0, 0, 0, array( 0, 0, 0, 0)),
  278. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  279. array( 25, 44, 7, array( 10, 16, 22, 28)),
  280. array( 29, 70, 7, array( 15, 26, 36, 44)),
  281. array( 33, 100, 7, array( 20, 36, 52, 64)),
  282. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  283. array( 41, 172, 7, array( 36, 64, 96, 112)),
  284. array( 45, 196, 0, array( 40, 72, 108, 130)),
  285. array( 49, 242, 0, array( 48, 88, 132, 156)),
  286. array( 53, 292, 0, array( 60, 110, 160, 192)),
  287. array( 57, 346, 0, array( 72, 130, 192, 224)), //10
  288. array( 61, 404, 0, array( 80, 150, 224, 264)),
  289. array( 65, 466, 0, array( 96, 176, 260, 308)),
  290. array( 69, 532, 0, array( 104, 198, 288, 352)),
  291. array( 73, 581, 3, array( 120, 216, 320, 384)),
  292. array( 77, 655, 3, array( 132, 240, 360, 432)), //15
  293. array( 81, 733, 3, array( 144, 280, 408, 480)),
  294. array( 85, 815, 3, array( 168, 308, 448, 532)),
  295. array( 89, 901, 3, array( 180, 338, 504, 588)),
  296. array( 93, 991, 3, array( 196, 364, 546, 650)),
  297. array( 97, 1085, 3, array( 224, 416, 600, 700)), //20
  298. array(101, 1156, 4, array( 224, 442, 644, 750)),
  299. array(105, 1258, 4, array( 252, 476, 690, 816)),
  300. array(109, 1364, 4, array( 270, 504, 750, 900)),
  301. array(113, 1474, 4, array( 300, 560, 810, 960)),
  302. array(117, 1588, 4, array( 312, 588, 870, 1050)), //25
  303. array(121, 1706, 4, array( 336, 644, 952, 1110)),
  304. array(125, 1828, 4, array( 360, 700, 1020, 1200)),
  305. array(129, 1921, 3, array( 390, 728, 1050, 1260)),
  306. array(133, 2051, 3, array( 420, 784, 1140, 1350)),
  307. array(137, 2185, 3, array( 450, 812, 1200, 1440)), //30
  308. array(141, 2323, 3, array( 480, 868, 1290, 1530)),
  309. array(145, 2465, 3, array( 510, 924, 1350, 1620)),
  310. array(149, 2611, 3, array( 540, 980, 1440, 1710)),
  311. array(153, 2761, 3, array( 570, 1036, 1530, 1800)),
  312. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), //35
  313. array(161, 3034, 0, array( 600, 1120, 1680, 1980)),
  314. array(165, 3196, 0, array( 630, 1204, 1770, 2100)),
  315. array(169, 3362, 0, array( 660, 1260, 1860, 2220)),
  316. array(173, 3532, 0, array( 720, 1316, 1950, 2310)),
  317. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) //40
  318. );
  319. //----------------------------------------------------------------------
  320. public static function getDataLength($version, $level)
  321. {
  322. return self::$capacity[$version][QRCAP_WORDS] - self::$capacity[$version][QRCAP_EC][$level];
  323. }
  324. //----------------------------------------------------------------------
  325. public static function getECCLength($version, $level)
  326. {
  327. return self::$capacity[$version][QRCAP_EC][$level];
  328. }
  329. //----------------------------------------------------------------------
  330. public static function getWidth($version)
  331. {
  332. return self::$capacity[$version][QRCAP_WIDTH];
  333. }
  334. //----------------------------------------------------------------------
  335. public static function getRemainder($version)
  336. {
  337. return self::$capacity[$version][QRCAP_REMINDER];
  338. }
  339. //----------------------------------------------------------------------
  340. public static function getMinimumVersion($size, $level)
  341. {
  342. for($i=1; $i<= QRSPEC_VERSION_MAX; $i++) {
  343. $words = self::$capacity[$i][QRCAP_WORDS] - self::$capacity[$i][QRCAP_EC][$level];
  344. if($words >= $size)
  345. return $i;
  346. }
  347. return -1;
  348. }
  349. //######################################################################
  350. public static $lengthTableBits = array(
  351. array(10, 12, 14),
  352. array( 9, 11, 13),
  353. array( 8, 16, 16),
  354. array( 8, 10, 12)
  355. );
  356. //----------------------------------------------------------------------
  357. public static function lengthIndicator($mode, $version)
  358. {
  359. if ($mode == QR_MODE_STRUCTURE)
  360. return 0;
  361. if ($version <= 9) {
  362. $l = 0;
  363. } else if ($version <= 26) {
  364. $l = 1;
  365. } else {
  366. $l = 2;
  367. }
  368. return self::$lengthTableBits[$mode][$l];
  369. }
  370. //----------------------------------------------------------------------
  371. public static function maximumWords($mode, $version)
  372. {
  373. if($mode == QR_MODE_STRUCTURE)
  374. return 3;
  375. if($version <= 9) {
  376. $l = 0;
  377. } else if($version <= 26) {
  378. $l = 1;
  379. } else {
  380. $l = 2;
  381. }
  382. $bits = self::$lengthTableBits[$mode][$l];
  383. $words = (1 << $bits) - 1;
  384. if($mode == QR_MODE_KANJI) {
  385. $words *= 2; // the number of bytes is required
  386. }
  387. return $words;
  388. }
  389. // Error correction code -----------------------------------------------
  390. // Table of the error correction code (Reed-Solomon block)
  391. // See Table 12-16 (pp.30-36), JIS X0510:2004.
  392. public static $eccTable = array(
  393. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)),
  394. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  395. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)),
  396. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)),
  397. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)),
  398. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  399. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)),
  400. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)),
  401. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)),
  402. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)),
  403. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), //10
  404. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)),
  405. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)),
  406. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)),
  407. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)),
  408. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), //15
  409. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)),
  410. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)),
  411. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)),
  412. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)),
  413. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), //20
  414. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)),
  415. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)),
  416. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)),
  417. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)),
  418. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), //25
  419. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)),
  420. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)),
  421. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)),
  422. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)),
  423. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), //30
  424. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)),
  425. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)),
  426. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)),
  427. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)),
  428. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), //35
  429. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)),
  430. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)),
  431. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)),
  432. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)),
  433. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)),//40
  434. );
  435. //----------------------------------------------------------------------
  436. // CACHEABLE!!!
  437. public static function getEccSpec($version, $level, array &$spec)
  438. {
  439. if (count($spec) < 5) {
  440. $spec = array(0,0,0,0,0);
  441. }
  442. $b1 = self::$eccTable[$version][$level][0];
  443. $b2 = self::$eccTable[$version][$level][1];
  444. $data = self::getDataLength($version, $level);
  445. $ecc = self::getECCLength($version, $level);
  446. if($b2 == 0) {
  447. $spec[0] = $b1;
  448. $spec[1] = (int)($data / $b1);
  449. $spec[2] = (int)($ecc / $b1);
  450. $spec[3] = 0;
  451. $spec[4] = 0;
  452. } else {
  453. $spec[0] = $b1;
  454. $spec[1] = (int)($data / ($b1 + $b2));
  455. $spec[2] = (int)($ecc / ($b1 + $b2));
  456. $spec[3] = $b2;
  457. $spec[4] = $spec[1] + 1;
  458. }
  459. }
  460. // Alignment pattern ---------------------------------------------------
  461. // Positions of alignment patterns.
  462. // This array includes only the second and the third position of the
  463. // alignment patterns. Rest of them can be calculated from the distance
  464. // between them.
  465. // See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
  466. public static $alignmentPattern = array(
  467. array( 0, 0),
  468. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  469. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  470. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), //11-15
  471. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), //16-20
  472. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), //21-25
  473. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), //26-30
  474. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), //31-35
  475. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58), //35-40
  476. );
  477. /** --------------------------------------------------------------------
  478. * Put an alignment marker.
  479. * @param frame
  480. * @param width
  481. * @param ox,oy center coordinate of the pattern
  482. */
  483. public static function putAlignmentMarker(array &$frame, $ox, $oy)
  484. {
  485. $finder = array(
  486. "\xa1\xa1\xa1\xa1\xa1",
  487. "\xa1\xa0\xa0\xa0\xa1",
  488. "\xa1\xa0\xa1\xa0\xa1",
  489. "\xa1\xa0\xa0\xa0\xa1",
  490. "\xa1\xa1\xa1\xa1\xa1"
  491. );
  492. $yStart = $oy-2;
  493. $xStart = $ox-2;
  494. for($y=0; $y<5; $y++) {
  495. QRstr::set($frame, $xStart, $yStart+$y, $finder[$y]);
  496. }
  497. }
  498. //----------------------------------------------------------------------
  499. public static function putAlignmentPattern($version, &$frame, $width)
  500. {
  501. if($version < 2)
  502. return;
  503. $d = self::$alignmentPattern[$version][1] - self::$alignmentPattern[$version][0];
  504. if($d < 0) {
  505. $w = 2;
  506. } else {
  507. $w = (int)(($width - self::$alignmentPattern[$version][0]) / $d + 2);
  508. }
  509. if($w * $w - 3 == 1) {
  510. $x = self::$alignmentPattern[$version][0];
  511. $y = self::$alignmentPattern[$version][0];
  512. self::putAlignmentMarker($frame, $x, $y);
  513. return;
  514. }
  515. $cx = self::$alignmentPattern[$version][0];
  516. for($x=1; $x<$w - 1; $x++) {
  517. self::putAlignmentMarker($frame, 6, $cx);
  518. self::putAlignmentMarker($frame, $cx, 6);
  519. $cx += $d;
  520. }
  521. $cy = self::$alignmentPattern[$version][0];
  522. for($y=0; $y<$w-1; $y++) {
  523. $cx = self::$alignmentPattern[$version][0];
  524. for($x=0; $x<$w-1; $x++) {
  525. self::putAlignmentMarker($frame, $cx, $cy);
  526. $cx += $d;
  527. }
  528. $cy += $d;
  529. }
  530. }
  531. // Version information pattern -----------------------------------------
  532. // Version information pattern (BCH coded).
  533. // See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
  534. // size: [QRSPEC_VERSION_MAX - 6]
  535. public static $versionPattern = array(
  536. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
  537. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
  538. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
  539. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
  540. 0x27541, 0x28c69
  541. );
  542. //----------------------------------------------------------------------
  543. public static function getVersionPattern($version)
  544. {
  545. if($version < 7 || $version > QRSPEC_VERSION_MAX)
  546. return 0;
  547. return self::$versionPattern[$version -7];
  548. }
  549. // Format information --------------------------------------------------
  550. // See calcFormatInfo in tests/test_qrspec.c (orginal qrencode c lib)
  551. public static $formatInfo = array(
  552. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
  553. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
  554. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
  555. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
  556. );
  557. public static function getFormatInfo($mask, $level)
  558. {
  559. if($mask < 0 || $mask > 7)
  560. return 0;
  561. if($level < 0 || $level > 3)
  562. return 0;
  563. return self::$formatInfo[$level][$mask];
  564. }
  565. // Frame ---------------------------------------------------------------
  566. // Cache of initial frames.
  567. public static $frames = array();
  568. /** --------------------------------------------------------------------
  569. * Put a finder pattern.
  570. * @param frame
  571. * @param width
  572. * @param ox,oy upper-left coordinate of the pattern
  573. */
  574. public static function putFinderPattern(&$frame, $ox, $oy)
  575. {
  576. $finder = array(
  577. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  578. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  579. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  580. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  581. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  582. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  583. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  584. );
  585. for($y=0; $y<7; $y++) {
  586. QRstr::set($frame, $ox, $oy+$y, $finder[$y]);
  587. }
  588. }
  589. //----------------------------------------------------------------------
  590. public static function createFrame($version)
  591. {
  592. $width = self::$capacity[$version][QRCAP_WIDTH];
  593. $frameLine = str_repeat ("\0", $width);
  594. $frame = array_fill(0, $width, $frameLine);
  595. // Finder pattern
  596. self::putFinderPattern($frame, 0, 0);
  597. self::putFinderPattern($frame, $width - 7, 0);
  598. self::putFinderPattern($frame, 0, $width - 7);
  599. // Separator
  600. $yOffset = $width - 7;
  601. for($y=0; $y<7; $y++) {
  602. $frame[$y][7] = "\xc0";
  603. $frame[$y][$width - 8] = "\xc0";
  604. $frame[$yOffset][7] = "\xc0";
  605. $yOffset++;
  606. }
  607. $setPattern = str_repeat("\xc0", 8);
  608. QRstr::set($frame, 0, 7, $setPattern);
  609. QRstr::set($frame, $width-8, 7, $setPattern);
  610. QRstr::set($frame, 0, $width - 8, $setPattern);
  611. // Format info
  612. $setPattern = str_repeat("\x84", 9);
  613. QRstr::set($frame, 0, 8, $setPattern);
  614. QRstr::set($frame, $width - 8, 8, $setPattern, 8);
  615. $yOffset = $width - 8;
  616. for($y=0; $y<8; $y++,$yOffset++) {
  617. $frame[$y][8] = "\x84";
  618. $frame[$yOffset][8] = "\x84";
  619. }
  620. // Timing pattern
  621. for($i=1; $i<$width-15; $i++) {
  622. $frame[6][7+$i] = chr(0x90 | ($i & 1));
  623. $frame[7+$i][6] = chr(0x90 | ($i & 1));
  624. }
  625. // Alignment pattern
  626. self::putAlignmentPattern($version, $frame, $width);
  627. // Version information
  628. if($version >= 7) {
  629. $vinf = self::getVersionPattern($version);
  630. $v = $vinf;
  631. for($x=0; $x<6; $x++) {
  632. for($y=0; $y<3; $y++) {
  633. $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
  634. $v = $v >> 1;
  635. }
  636. }
  637. $v = $vinf;
  638. for($y=0; $y<6; $y++) {
  639. for($x=0; $x<3; $x++) {
  640. $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
  641. $v = $v >> 1;
  642. }
  643. }
  644. }
  645. // and a little bit...
  646. $frame[$width - 8][8] = "\x81";
  647. return $frame;
  648. }
  649. //----------------------------------------------------------------------
  650. public static function debug($frame, $binary_mode = false)
  651. {
  652. if ($binary_mode) {
  653. foreach ($frame as &$frameLine) {
  654. $frameLine = join('<span class="m">&nbsp;&nbsp;</span>', explode('0', $frameLine));
  655. $frameLine = join('&#9608;&#9608;', explode('1', $frameLine));
  656. }
  657. ?>
  658. <style>
  659. .m { background-color: white; }
  660. </style>
  661. <?php
  662. echo '<pre><tt><br/ ><br/ ><br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  663. echo join("<br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $frame);
  664. echo '</tt></pre><br/ ><br/ ><br/ ><br/ ><br/ ><br/ >';
  665. } else {
  666. foreach ($frame as &$frameLine) {
  667. $frameLine = join('<span class="m">&nbsp;</span>', explode("\xc0", $frameLine));
  668. $frameLine = join('<span class="m">&#9618;</span>', explode("\xc1", $frameLine));
  669. $frameLine = join('<span class="p">&nbsp;</span>', explode("\xa0", $frameLine));
  670. $frameLine = join('<span class="p">&#9618;</span>', explode("\xa1", $frameLine));
  671. $frameLine = join('<span class="s">&#9671;</span>', explode("\x84", $frameLine)); //format 0
  672. $frameLine = join('<span class="s">&#9670;</span>', explode("\x85", $frameLine)); //format 1
  673. $frameLine = join('<span class="x">&#9762;</span>', explode("\x81", $frameLine)); //special bit
  674. $frameLine = join('<span class="c">&nbsp;</span>', explode("\x90", $frameLine)); //clock 0
  675. $frameLine = join('<span class="c">&#9719;</span>', explode("\x91", $frameLine)); //clock 1
  676. $frameLine = join('<span class="f">&nbsp;</span>', explode("\x88", $frameLine)); //version
  677. $frameLine = join('<span class="f">&#9618;</span>', explode("\x89", $frameLine)); //version
  678. $frameLine = join('&#9830;', explode("\x01", $frameLine));
  679. $frameLine = join('&#8901;', explode("\0", $frameLine));
  680. }
  681. ?>
  682. <style>
  683. .p { background-color: yellow; }
  684. .m { background-color: #00FF00; }
  685. .s { background-color: #FF0000; }
  686. .c { background-color: aqua; }
  687. .x { background-color: pink; }
  688. .f { background-color: gold; }
  689. </style>
  690. <?php
  691. echo "<pre><tt>";
  692. echo join("<br/ >", $frame);
  693. echo "</tt></pre>";
  694. }
  695. }
  696. //----------------------------------------------------------------------
  697. public static function serial($frame)
  698. {
  699. return gzcompress(join("\n", $frame), 9);
  700. }
  701. //----------------------------------------------------------------------
  702. public static function unserial($code)
  703. {
  704. return explode("\n", gzuncompress($code));
  705. }
  706. //----------------------------------------------------------------------
  707. public static function newFrame($version)
  708. {
  709. if($version < 1 || $version > QRSPEC_VERSION_MAX)
  710. return null;
  711. if(!isset(self::$frames[$version])) {
  712. $fileName = QR_CACHE_DIR.'frame_'.$version.'.dat';
  713. if (QR_CACHEABLE) {
  714. if (file_exists($fileName)) {
  715. self::$frames[$version] = self::unserial(file_get_contents($fileName));
  716. } else {
  717. self::$frames[$version] = self::createFrame($version);
  718. file_put_contents($fileName, self::serial(self::$frames[$version]));
  719. }
  720. } else {
  721. self::$frames[$version] = self::createFrame($version);
  722. }
  723. }
  724. if(is_null(self::$frames[$version]))
  725. return null;
  726. return self::$frames[$version];
  727. }
  728. //----------------------------------------------------------------------
  729. public static function rsBlockNum($spec) { return $spec[0] + $spec[3]; }
  730. public static function rsBlockNum1($spec) { return $spec[0]; }
  731. public static function rsDataCodes1($spec) { return $spec[1]; }
  732. public static function rsEccCodes1($spec) { return $spec[2]; }
  733. public static function rsBlockNum2($spec) { return $spec[3]; }
  734. public static function rsDataCodes2($spec) { return $spec[4]; }
  735. public static function rsEccCodes2($spec) { return $spec[2]; }
  736. public static function rsDataLength($spec) { return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]); }
  737. public static function rsEccLength($spec) { return ($spec[0] + $spec[3]) * $spec[2]; }
  738. }
  739. //---- qrimage.php -----------------------------
  740. /*
  741. * PHP QR Code encoder
  742. *
  743. * Image output of code using GD2
  744. *
  745. * PHP QR Code is distributed under LGPL 3
  746. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  747. *
  748. * This library is free software; you can redistribute it and/or
  749. * modify it under the terms of the GNU Lesser General Public
  750. * License as published by the Free Software Foundation; either
  751. * version 3 of the License, or any later version.
  752. *
  753. * This library is distributed in the hope that it will be useful,
  754. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  755. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  756. * Lesser General Public License for more details.
  757. *
  758. * You should have received a copy of the GNU Lesser General Public
  759. * License along with this library; if not, write to the Free Software
  760. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  761. */
  762. define('QR_IMAGE', true);
  763. class QRimage {
  764. //----------------------------------------------------------------------
  765. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
  766. {
  767. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  768. if ($filename === false) {
  769. Header("Content-type: image/png");
  770. ImagePng($image);
  771. } else {
  772. if($saveandprint===TRUE){
  773. ImagePng($image, $filename);
  774. header("Content-type: image/png");
  775. ImagePng($image);
  776. }else{
  777. ImagePng($image, $filename);
  778. }
  779. }
  780. ImageDestroy($image);
  781. }
  782. //----------------------------------------------------------------------
  783. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
  784. {
  785. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  786. if ($filename === false) {
  787. Header("Content-type: image/jpeg");
  788. ImageJpeg($image, null, $q);
  789. } else {
  790. ImageJpeg($image, $filename, $q);
  791. }
  792. ImageDestroy($image);
  793. }
  794. //----------------------------------------------------------------------
  795. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
  796. {
  797. $h = count($frame);
  798. $w = strlen($frame[0]);
  799. $imgW = $w + 2*$outerFrame;
  800. $imgH = $h + 2*$outerFrame;
  801. $base_image =ImageCreate($imgW, $imgH);
  802. $col[0] = ImageColorAllocate($base_image,255,255,255);
  803. $col[1] = ImageColorAllocate($base_image,0,0,0);
  804. imagefill($base_image, 0, 0, $col[0]);
  805. for($y=0; $y<$h; $y++) {
  806. for($x=0; $x<$w; $x++) {
  807. if ($frame[$y][$x] == '1') {
  808. ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
  809. }
  810. }
  811. }
  812. $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  813. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  814. ImageDestroy($base_image);
  815. return $target_image;
  816. }
  817. }
  818. //---- qrinput.php -----------------------------
  819. /*
  820. * PHP QR Code encoder
  821. *
  822. * Input encoding class
  823. *
  824. * Based on libqrencode C library distributed under LGPL 2.1
  825. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  826. *
  827. * PHP QR Code is distributed under LGPL 3
  828. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  829. *
  830. * This library is free software; you can redistribute it and/or
  831. * modify it under the terms of the GNU Lesser General Public
  832. * License as published by the Free Software Foundation; either
  833. * version 3 of the License, or any later version.
  834. *
  835. * This library is distributed in the hope that it will be useful,
  836. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  837. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  838. * Lesser General Public License for more details.
  839. *
  840. * You should have received a copy of the GNU Lesser General Public
  841. * License along with this library; if not, write to the Free Software
  842. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  843. */
  844. define('STRUCTURE_HEADER_BITS', 20);
  845. define('MAX_STRUCTURED_SYMBOLS', 16);
  846. class QRinputItem {
  847. public $mode;
  848. public $size;
  849. public $data;
  850. public $bstream;
  851. public function __construct($mode, $size, $data, $bstream = null)
  852. {
  853. $setData = array_slice($data, 0, $size);
  854. if (count($setData) < $size) {
  855. $setData = array_merge($setData, array_fill(0,$size-count($setData),0));
  856. }
  857. if(!QRinput::check($mode, $size, $setData)) {
  858. throw new Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',',$setData));
  859. return null;
  860. }
  861. $this->mode = $mode;
  862. $this->size = $size;
  863. $this->data = $setData;
  864. $this->bstream = $bstream;
  865. }
  866. //----------------------------------------------------------------------
  867. public function encodeModeNum($version)
  868. {
  869. try {
  870. $words = (int)($this->size / 3);
  871. $bs = new QRbitstream();
  872. $val = 0x1;
  873. $bs->appendNum(4, $val);
  874. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size);
  875. for($i=0; $i<$words; $i++) {
  876. $val = (ord($this->data[$i*3 ]) - ord('0')) * 100;
  877. $val += (ord($this->data[$i*3+1]) - ord('0')) * 10;
  878. $val += (ord($this->data[$i*3+2]) - ord('0'));
  879. $bs->appendNum(10, $val);
  880. }
  881. if($this->size - $words * 3 == 1) {
  882. $val = ord($this->data[$words*3]) - ord('0');
  883. $bs->appendNum(4, $val);
  884. } else if($this->size - $words * 3 == 2) {
  885. $val = (ord($this->data[$words*3 ]) - ord('0')) * 10;
  886. $val += (ord($this->data[$words*3+1]) - ord('0'));
  887. $bs->appendNum(7, $val);
  888. }
  889. $this->bstream = $bs;
  890. return 0;
  891. } catch (Exception $e) {
  892. return -1;
  893. }
  894. }
  895. //----------------------------------------------------------------------
  896. public function encodeModeAn($version)
  897. {
  898. try {
  899. $words = (int)($this->size / 2);
  900. $bs = new QRbitstream();
  901. $bs->appendNum(4, 0x02);
  902. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size);
  903. for($i=0; $i<$words; $i++) {
  904. $val = (int)QRinput::lookAnTable(ord($this->data[$i*2 ])) * 45;
  905. $val += (int)QRinput::lookAnTable(ord($this->data[$i*2+1]));
  906. $bs->appendNum(11, $val);
  907. }
  908. if($this->size & 1) {
  909. $val = QRinput::lookAnTable(ord($this->data[$words * 2]));
  910. $bs->appendNum(6, $val);
  911. }
  912. $this->bstream = $bs;
  913. return 0;
  914. } catch (Exception $e) {
  915. return -1;
  916. }
  917. }
  918. //----------------------------------------------------------------------
  919. public function encodeMode8($version)
  920. {
  921. try {
  922. $bs = new QRbitstream();
  923. $bs->appendNum(4, 0x4);
  924. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size);
  925. for($i=0; $i<$this->size; $i++) {
  926. $bs->appendNum(8, ord($this->data[$i]));
  927. }
  928. $this->bstream = $bs;
  929. return 0;
  930. } catch (Exception $e) {
  931. return -1;
  932. }
  933. }
  934. //----------------------------------------------------------------------
  935. public function encodeModeKanji($version)
  936. {
  937. try {
  938. $bs = new QRbitrtream();
  939. $bs->appendNum(4, 0x8);
  940. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int)($this->size / 2));
  941. for($i=0; $i<$this->size; $i+=2) {
  942. $val = (ord($this->data[$i]) << 8) | ord($this->data[$i+1]);
  943. if($val <= 0x9ffc) {
  944. $val -= 0x8140;
  945. } else {
  946. $val -= 0xc140;
  947. }
  948. $h = ($val >> 8) * 0xc0;
  949. $val = ($val & 0xff) + $h;
  950. $bs->appendNum(13, $val);
  951. }
  952. $this->bstream = $bs;
  953. return 0;
  954. } catch (Exception $e) {
  955. return -1;
  956. }
  957. }
  958. //----------------------------------------------------------------------
  959. public function encodeModeStructure()
  960. {
  961. try {
  962. $bs = new QRbitstream();
  963. $bs->appendNum(4, 0x03);
  964. $bs->appendNum(4, ord($this->data[1]) - 1);
  965. $bs->appendNum(4, ord($this->data[0]) - 1);
  966. $bs->appendNum(8, ord($this->data[2]));
  967. $this->bstream = $bs;
  968. return 0;
  969. } catch (Exception $e) {
  970. return -1;
  971. }
  972. }
  973. //----------------------------------------------------------------------
  974. public function estimateBitStreamSizeOfEntry($version)
  975. {
  976. $bits = 0;
  977. if($version == 0)
  978. $version = 1;
  979. switch($this->mode) {
  980. case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size); break;
  981. case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size); break;
  982. case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size); break;
  983. case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);break;
  984. case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS;
  985. default:
  986. return 0;
  987. }
  988. $l = QRspec::lengthIndicator($this->mode, $version);
  989. $m = 1 << $l;
  990. $num = (int)(($this->size + $m - 1) / $m);
  991. $bits += $num * (4 + $l);
  992. return $bits;
  993. }
  994. //----------------------------------------------------------------------
  995. public function encodeBitStream($version)
  996. {
  997. try {
  998. unset($this->bstream);
  999. $words = QRspec::maximumWords($this->mode, $version);
  1000. if($this->size > $words) {
  1001. $st1 = new QRinputItem($this->mode, $words, $this->data);
  1002. $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words));
  1003. $st1->encodeBitStream($version);
  1004. $st2->encodeBitStream($version);
  1005. $this->bstream = new QRbitstream();

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