QRCode.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /**
  3. * This file provides the main QRCode class.
  4. *
  5. * The project is a rewrite from jQuery extension available at
  6. * @link https://github.com/jeromeetienne/jquery-qrcode
  7. *
  8. * For a detailed description of QRCode and its features please check
  9. * @link http://www.qrcode.com/
  10. *
  11. * QR Code is registered trademark of
  12. * DENSO WAVE INCORPORATED
  13. * http://www.denso-wave.com/qrcode/faqpatent-e.html
  14. *
  15. * All files in the package have the same license:
  16. * http://opensource.org/licenses/BSD-2-Clause
  17. *
  18. * @copyright BSD2
  19. * @author Maik Greubel <[email protected]>
  20. */
  21. /**
  22. * Import necessary dependencies
  23. */
  24. require_once 'QR8bitByte.php';
  25. require_once 'QRBitBuffer.php';
  26. require_once 'QRRSBlock.php';
  27. require_once 'QRUtil.php';
  28. require_once 'QRCodeException.php';
  29. /**
  30. * This is the main class
  31. *
  32. * It provides the functionality to generate a QRCode bitmap
  33. * out of appended data elements.
  34. *
  35. * @package phpQr
  36. * @author Maik Greubel <[email protected]>
  37. */
  38. class QRCode
  39. {
  40. /**
  41. * Needed for padding
  42. *
  43. * @final
  44. *
  45. */
  46. const PAD0 = 0xec;
  47. /**
  48. * Needed for padding
  49. *
  50. * @final
  51. *
  52. */
  53. const PAD1 = 0x11;
  54. /**
  55. * The type number of qrcode
  56. *
  57. * @var int
  58. */
  59. private $typeNumber;
  60. /**
  61. * Level of error correction
  62. *
  63. * @see QRErrorCorrectLevel
  64. *
  65. * @var int
  66. */
  67. private $errorCorrectLevel;
  68. /**
  69. * Bitmap
  70. *
  71. * @var array
  72. */
  73. private $modules;
  74. /**
  75. * Amount of modules in bitmap
  76. *
  77. * @var int
  78. */
  79. private $moduleCount;
  80. /**
  81. * The data as array
  82. *
  83. * @var array
  84. */
  85. private $dataCache;
  86. /**
  87. * All append data elements
  88. *
  89. * @var array
  90. */
  91. private $dataList;
  92. /**
  93. * Create a new instance of QRCode
  94. *
  95. * @param int $typeNumber
  96. * The type of QRCode
  97. * @param int $errorCorrectLevel
  98. * The error correction level
  99. */
  100. public function __construct($typeNumber, $errorCorrectLevel)
  101. {
  102. $this->typeNumber = $typeNumber;
  103. $this->errorCorrectLevel = $errorCorrectLevel;
  104. $this->modules = null;
  105. $this->moduleCount = 0;
  106. $this->dataCache = null;
  107. $this->dataList = array ();
  108. }
  109. /**
  110. * This function is only needed for debugging purposes and returns the bitmap
  111. * DONT USE THIS TO MANIPULATE THE BITMAP!
  112. *
  113. * @return array
  114. */
  115. public function getModules()
  116. {
  117. return $this->modules;
  118. }
  119. /**
  120. * Add a new data element to the QRCode
  121. *
  122. * @param string $data
  123. */
  124. public function addData($data)
  125. {
  126. $newData = new QR8bitByte ( $data );
  127. array_push ( $this->dataList, $newData );
  128. $this->dataCache = null;
  129. }
  130. /**
  131. * Returns whether a given bitmap entry is dark or not
  132. *
  133. * @param int $row
  134. * The row in bitmap
  135. * @param int $col
  136. * The column in bitmap
  137. *
  138. * @throws QRCodeException
  139. * @return true in case of its a dark bit, false otherwise
  140. */
  141. public function isDark($row, $col)
  142. {
  143. if ($row < 0 || $this->moduleCount <= $row || $col < 0 || $this->moduleCount <= $col)
  144. {
  145. throw new QRCodeException ( "$row,$col" );
  146. }
  147. return $this->modules [$row] [$col];
  148. }
  149. /**
  150. * Get the amount of modules in bitmap
  151. *
  152. * @return int
  153. */
  154. public function getModuleCount()
  155. {
  156. return $this->moduleCount;
  157. }
  158. /**
  159. * Generate the QRCode bitmap
  160. */
  161. public function make()
  162. {
  163. if ($this->typeNumber < 1)
  164. {
  165. $typeNumber = 1;
  166. for($typeNumber = 1; $typeNumber < 40; $typeNumber ++)
  167. {
  168. $rsBlocks = QRRSBlock::getInstance ()->getRSBlocks ( $typeNumber, $this->errorCorrectLevel );
  169. $buffer = new QRBitBuffer ();
  170. $totalDataCount = 0;
  171. for($i = 0; $i < sizeof ( $rsBlocks ); $i ++)
  172. {
  173. $totalDataCount += $rsBlocks [$i]->getDataCount ();
  174. }
  175. for($i = 0; $i < sizeof ( $this->dataList ); $i ++)
  176. {
  177. $data = $this->dataList [$i];
  178. assert ( $data instanceof QRByte );
  179. $buffer->put ( $data->getMode (), 4 );
  180. $buffer->put ( $data->getLength (), QRUtil::getInstance ()->getLengthInBits ( $data->getMode (), $typeNumber ) );
  181. $data->write ( $buffer );
  182. }
  183. if ($buffer->getLengthInBits () <= $totalDataCount * 8)
  184. break;
  185. }
  186. $this->typeNumber = $typeNumber;
  187. }
  188. $this->makeImpl ( false, $this->getBestMaskPattern () );
  189. }
  190. /**
  191. * Generates the bitmap (really)
  192. *
  193. * @param boolean $test
  194. * @param int $maskPattern
  195. */
  196. private function makeImpl($test, $maskPattern)
  197. {
  198. $this->moduleCount = $this->typeNumber * 4 + 17;
  199. $this->modules = QRUtil::getInstance ()->createEmptyArray ( $this->moduleCount );
  200. for($row = 0; $row < $this->moduleCount; $row ++)
  201. {
  202. $this->modules [$row] = QRUtil::getInstance ()->createEmptyArray ( $this->moduleCount );
  203. for($col = 0; $col < $this->moduleCount; $col ++)
  204. {
  205. $this->modules [$row] [$col] = null;
  206. }
  207. }
  208. $this->setupPositionProbePattern ( 0, 0 );
  209. $this->setupPositionProbePattern ( $this->moduleCount - 7, 0 );
  210. $this->setupPositionProbePattern ( 0, $this->moduleCount - 7 );
  211. $this->setupPositionAdjustPattern ();
  212. $this->setupTimingPattern ();
  213. $this->setupTypeInfo ( $test, $maskPattern );
  214. if ($this->typeNumber >= 7)
  215. {
  216. $this->setTypeNumber ( $test );
  217. }
  218. if ($this->dataCache == null)
  219. {
  220. $this->dataCache = self::createData ( $this->typeNumber, $this->errorCorrectLevel, $this->dataList );
  221. }
  222. $this->mapData ( $this->dataCache, $maskPattern );
  223. }
  224. /**
  225. * Add the position probes to the bitmap
  226. *
  227. * @param int $row
  228. * @param int $col
  229. */
  230. private function setupPositionProbePattern($row, $col)
  231. {
  232. for($r = - 1; $r <= 7; $r ++)
  233. {
  234. if ($row + $r <= - 1 || $this->moduleCount <= $row + $r)
  235. continue;
  236. for($c = - 1; $c <= 7; $c ++)
  237. {
  238. if ($col + $c <= - 1 || $this->moduleCount <= $col + $c)
  239. continue;
  240. if ((0 <= $r && $r <= 6 && ($c == 0 || $c == 6)) || (0 <= $c && $c <= 6 && ($r == 0 || $r == 6)) || (2 <= $r && $r <= 4 && 2 <= $c && $c <= 4))
  241. {
  242. $this->modules [$row + $r] [$col + $c] = true;
  243. }
  244. else
  245. {
  246. $this->modules [$row + $r] [$col + $c] = false;
  247. }
  248. }
  249. }
  250. }
  251. /**
  252. * Get the best mask pattern for this QRCode
  253. *
  254. * @return int
  255. */
  256. private function getBestMaskPattern()
  257. {
  258. $minLostPoint = 0;
  259. $pattern = 0;
  260. for($i = 0; $i < 8; $i ++)
  261. {
  262. $this->makeImpl ( true, $i );
  263. $lostPoint = QRUtil::getInstance ()->getLostPoint ( $this );
  264. if ($i == 0 || $minLostPoint > $lostPoint)
  265. {
  266. $minLostPoint = $lostPoint;
  267. $pattern = $i;
  268. }
  269. }
  270. return $pattern;
  271. }
  272. /**
  273. * Add the timing pattern to bitmap
  274. */
  275. private function setupTimingPattern()
  276. {
  277. for($r = 8; $r < $this->moduleCount - 8; $r ++)
  278. {
  279. if ($this->modules [$r] [6] != null)
  280. {
  281. continue;
  282. }
  283. $this->modules [$r] [6] = ($r % 2 == 0);
  284. }
  285. for($c = 8; $c < $this->moduleCount - 8; $c ++)
  286. {
  287. if ($this->modules [6] [$c] != null)
  288. {
  289. continue;
  290. }
  291. $this->modules [6] [$c] = ($c % 2 == 0);
  292. }
  293. }
  294. /**
  295. * Add the position adjust pattern to bitmap
  296. */
  297. private function setupPositionAdjustPattern()
  298. {
  299. $pos = QRUtil::getInstance ()->getPatternPosition ( $this->typeNumber );
  300. for($i = 0; $i < sizeof ( $pos ); $i ++)
  301. {
  302. for($j = 0; $j < sizeof ( $pos ); $j ++)
  303. {
  304. $row = $pos [$i];
  305. $col = $pos [$j];
  306. if ($this->modules [$row] [$col] != null)
  307. {
  308. continue;
  309. }
  310. for($r = - 2; $r <= 2; $r ++)
  311. {
  312. for($c = - 2; $c <= 2; $c ++)
  313. {
  314. if ($r == - 2 || $r == 2 || $c == - 2 || $c == 2 || ($r == 0 && $c == 0))
  315. {
  316. $this->modules [$row + $r] [$col + $c] = true;
  317. }
  318. else
  319. {
  320. $this->modules [$row + $r] [$col + $c] = false;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * Add the type number to bitmap
  329. *
  330. * @param boolean $test
  331. */
  332. private function setTypeNumber($test)
  333. {
  334. $bits = QRUtil::getInstance ()->getBCHTypeNumber ( $this->typeNumber );
  335. for($i = 0; $i < 18; $i ++)
  336. {
  337. $mod = (! $test && (($bits >> $i) & 1) == 1);
  338. $this->modules [floor ( $i / 3 )] [$i % 3 + $this->moduleCount - 8 - 3] = $mod;
  339. }
  340. for($i = 0; $i < 18; $i ++)
  341. {
  342. $mod = (! $test && (($bits >> $i) & 1) == 1);
  343. $this->modules [$i % 3 + $this->moduleCount - 8 - 3] [floor ( $i / 3 )] = $mod;
  344. }
  345. }
  346. /**
  347. * Add the type info to bitmap
  348. *
  349. * @param boolean $test
  350. * @param int $maskPattern
  351. */
  352. private function setupTypeInfo($test, $maskPattern)
  353. {
  354. $data = ($this->errorCorrectLevel << 3) | $maskPattern;
  355. $bits = QRUtil::getInstance ()->getBCHTypeInfo ( $data );
  356. // vertical
  357. for($i = 0; $i < 15; $i ++)
  358. {
  359. $mod = (! $test && (($bits >> $i) & 1) == 1);
  360. if ($i < 6)
  361. {
  362. $this->modules [$i] [8] = $mod;
  363. }
  364. else if ($i < 8)
  365. {
  366. $this->modules [$i + 1] [8] = $mod;
  367. }
  368. else
  369. {
  370. $this->modules [$this->moduleCount - 15 + $i] [8] = $mod;
  371. }
  372. }
  373. // horizontal
  374. for($i = 0; $i < 15; $i ++)
  375. {
  376. $mod = (! $test && (($bits >> $i) & 1) == 1);
  377. if ($i < 8)
  378. {
  379. $this->modules [8] [$this->moduleCount - $i - 1] = $mod;
  380. }
  381. else if ($i < 9)
  382. {
  383. $this->modules [8] [15 - $i - 1 + 1] = $mod;
  384. }
  385. else
  386. {
  387. $this->modules [8] [15 - $i - 1] = $mod;
  388. }
  389. }
  390. // fixed module
  391. $this->modules [$this->moduleCount - 8] [8] = (! $test);
  392. }
  393. /**
  394. * Add the data to bitmap
  395. *
  396. * @param array $data
  397. * @param int $maskPattern
  398. */
  399. private function mapData($data, $maskPattern)
  400. {
  401. $inc = - 1;
  402. $row = $this->moduleCount - 1;
  403. $bitIndex = 7;
  404. $byteIndex = 0;
  405. for($col = $this->moduleCount - 1; $col > 0; $col -= 2)
  406. {
  407. if ($col == 6)
  408. $col --;
  409. while ( true )
  410. {
  411. for($c = 0; $c < 2; $c ++)
  412. {
  413. if ($this->modules [$row] [$col - $c] === null)
  414. {
  415. $dark = false;
  416. if ($byteIndex < sizeof ( $data ))
  417. {
  418. $dark = ((($data [$byteIndex] >> $bitIndex) & 1) == 1);
  419. }
  420. $mask = QRUtil::getInstance ()->getMask ( $maskPattern, $row, $col - $c );
  421. if ($mask)
  422. $dark = ! $dark;
  423. $this->modules [$row] [$col - $c] = $dark;
  424. $bitIndex --;
  425. if ($bitIndex == - 1)
  426. {
  427. $byteIndex ++;
  428. $bitIndex = 7;
  429. }
  430. }
  431. }
  432. $row += $inc;
  433. if ($row < 0 || $this->moduleCount <= $row)
  434. {
  435. $row -= $inc;
  436. $inc = - $inc;
  437. break;
  438. }
  439. }
  440. }
  441. }
  442. /**
  443. * Create a bitmap out of all append data elements
  444. *
  445. * @param int $typeNumber
  446. * @param int $errorCorrectLevel
  447. * @param array $dataList
  448. *
  449. * @throws QRCodeException
  450. *
  451. * @return array
  452. */
  453. private function createData($typeNumber, $errorCorrectLevel, $dataList)
  454. {
  455. $rsBlocks = QRRSBlock::getInstance ()->getRSBlocks ( $typeNumber, $errorCorrectLevel );
  456. $buffer = new QRBitBuffer ();
  457. for($i = 0; $i < sizeof ( $dataList ); $i ++)
  458. {
  459. $data = $dataList [$i];
  460. assert ( $data instanceof QRByte );
  461. $buffer->put ( $data->getMode (), 4 );
  462. $buffer->put ( $data->getLength (), QRUtil::getInstance ()->getLengthInBits ( $data->getMode (), $typeNumber ) );
  463. $data->write ( $buffer );
  464. }
  465. // calc num max data
  466. $totalDataCount = 0;
  467. for($i = 0; $i < sizeof ( $rsBlocks ); $i ++)
  468. {
  469. $totalDataCount += $rsBlocks [$i]->getDataCount ();
  470. }
  471. if ($buffer->getLengthInBits () > $totalDataCount * 8)
  472. {
  473. throw new QRCodeException ( "code length overflow (" . $buffer->getLengthInBits () . " > " . ($totalDataCount * 8) . ")" );
  474. }
  475. // end code
  476. if ($buffer->getLengthInBits () + 4 <= $totalDataCount * 8)
  477. {
  478. $buffer->put ( 0, 4 );
  479. }
  480. // padding
  481. while ( $buffer->getLengthInBits () % 8 != 0 )
  482. {
  483. $buffer->putBit ( false );
  484. }
  485. // padding
  486. while ( true )
  487. {
  488. if ($buffer->getLengthInBits () >= $totalDataCount * 8)
  489. {
  490. break;
  491. }
  492. $buffer->put ( QRCode::PAD0, 8 );
  493. if ($buffer->getLengthInBits () >= $totalDataCount * 8)
  494. {
  495. break;
  496. }
  497. $buffer->put ( QRCode::PAD1, 8 );
  498. }
  499. return $this->createBytes ( $buffer, $rsBlocks );
  500. }
  501. /**
  502. * Create bitmap out of the bit buffer using reed solomon blocks
  503. *
  504. * @param QRBitBuffer $buffer
  505. * @param array $rsBlocks
  506. * @return array
  507. */
  508. public function createBytes(QRBitBuffer $buffer, $rsBlocks)
  509. {
  510. $offset = 0;
  511. $maxDcCount = 0;
  512. $maxEcCount = 0;
  513. $dcdata = QRUtil::getInstance ()->createEmptyArray ( sizeof ( $rsBlocks ) );
  514. $ecdata = QRUtil::getInstance ()->createEmptyArray ( sizeof ( $rsBlocks ) );
  515. for($r = 0; $r < sizeof ( $rsBlocks ); $r ++)
  516. {
  517. $dcCount = $rsBlocks [$r]->getDataCount ();
  518. $ecCount = $rsBlocks [$r]->getTotalCount () - $dcCount;
  519. $maxDcCount = max ( array (
  520. $maxDcCount,
  521. $dcCount
  522. ) );
  523. $maxEcCount = max ( array (
  524. $maxEcCount,
  525. $ecCount
  526. ) );
  527. $dcdata [$r] = QRUtil::getInstance ()->createEmptyArray ( $dcCount );
  528. for($i = 0; $i < sizeof ( $dcdata [$r] ); $i ++)
  529. {
  530. $dcdata [$r] [$i] = 0xff & $buffer->getAt ( $i + $offset );
  531. }
  532. $offset += $dcCount;
  533. $rsPoly = QRUtil::getInstance ()->getErrorCorrectPolynominal ( $ecCount );
  534. $rawPoly = new QRPolynominal ( $dcdata [$r], $rsPoly->getLength () - 1 );
  535. $modPoly = $rawPoly->mod ( $rsPoly );
  536. $ecdata [$r] = QRUtil::getInstance ()->createEmptyArray ( $rsPoly->getLength () - 1 );
  537. for($i = 0; $i < sizeof ( $ecdata [$r] ); $i ++)
  538. {
  539. $modIndex = $i + $modPoly->getLength () - sizeof ( $ecdata [$r] );
  540. $ecdata [$r] [$i] = ($modIndex >= 0) ? $modPoly->get ( $modIndex ) : 0;
  541. }
  542. }
  543. $totalCodeCount = 0;
  544. for($i = 0; $i < sizeof ( $rsBlocks ); $i ++)
  545. {
  546. $totalCodeCount += $rsBlocks [$i]->getTotalCount ();
  547. }
  548. $data = QRUtil::getInstance ()->createEmptyArray ( $totalCodeCount );
  549. $index = 0;
  550. for($i = 0; $i < $maxDcCount; $i ++)
  551. {
  552. for($r = 0; $r < sizeof ( $rsBlocks ); $r ++)
  553. {
  554. if ($i < sizeof ( $dcdata [$r] ))
  555. {
  556. $data [$index ++] = $dcdata [$r] [$i];
  557. }
  558. }
  559. }
  560. for($i = 0; $i < $maxEcCount; $i ++)
  561. {
  562. for($r = 0; $r < sizeof ( $rsBlocks ); $r ++)
  563. {
  564. if ($i < sizeof ( $ecdata [$r] ))
  565. {
  566. $data [$index ++] = $ecdata [$r] [$i];
  567. }
  568. }
  569. }
  570. return $data;
  571. }
  572. }