QRCode.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * Class QRCode
  4. *
  5. * @filesource QRCode.php
  6. * @created 26.11.2015
  7. * @package chillerlan\QRCode
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. use chillerlan\QRCode\Output\QROutputInterface;
  14. /**
  15. * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  16. */
  17. class QRCode{
  18. /**
  19. * @var array
  20. */
  21. protected $matrix = [];
  22. /**
  23. * @var int
  24. */
  25. protected $pixelCount = 0;
  26. /**
  27. * @var int
  28. */
  29. protected $typeNumber;
  30. /**
  31. * @var int
  32. */
  33. protected $errorCorrectLevel;
  34. /**
  35. * @var \chillerlan\QRCode\BitBuffer
  36. */
  37. protected $bitBuffer;
  38. /**
  39. * @var \chillerlan\QRCode\Data\QRDataInterface
  40. */
  41. protected $qrDataInterface;
  42. /**
  43. * @var \chillerlan\QRCode\Output\QROutputInterface
  44. */
  45. protected $qrOutputInterface;
  46. /**
  47. * QRCode constructor.
  48. *
  49. * @param string $data
  50. * @param \chillerlan\QRCode\QROptions $options
  51. *
  52. * @throws \chillerlan\QRCode\QRCodeException
  53. */
  54. public function __construct($data, QROptions $options){
  55. $this->bitBuffer = new BitBuffer;
  56. $this->setData($data, $options);
  57. }
  58. /**
  59. * @param string $data
  60. * @param \chillerlan\QRCode\QROptions $options
  61. *
  62. * @return $this
  63. * @throws \chillerlan\QRCode\QRCodeException
  64. */
  65. public function setData($data, QROptions $options){
  66. $data = trim($data);
  67. if(empty($data)){
  68. throw new QRCodeException('No data given.');
  69. }
  70. if(!array_key_exists($options->errorCorrectLevel, QRConst::RSBLOCK)){
  71. throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel);
  72. }
  73. if(!$options->output instanceof QROutputInterface){
  74. throw new QRCodeException('$options->output is no instance of QROutputInterface');
  75. }
  76. $this->qrOutputInterface = $options->output;
  77. $mode = Util::getMode($data);
  78. $qrDataInterface = __NAMESPACE__.'\\Data\\'.[
  79. QRConst::MODE_ALPHANUM => 'AlphaNum',
  80. QRConst::MODE_BYTE => 'Byte',
  81. QRConst::MODE_KANJI => 'Kanji',
  82. QRConst::MODE_NUMBER => 'Number',
  83. ][$mode];
  84. $this->errorCorrectLevel = $options->errorCorrectLevel;
  85. $this->typeNumber = intval($options->typeNumber);
  86. $this->qrDataInterface = new $qrDataInterface($data);
  87. if($this->typeNumber < 1 || $this->typeNumber > 10){
  88. /** @noinspection PhpUndefinedFieldInspection */
  89. $length = $this->qrDataInterface->mode === QRConst::MODE_KANJI
  90. ? floor($this->qrDataInterface->dataLength / 2)
  91. : $this->qrDataInterface->dataLength;
  92. for($type = 1; $type <= 10; $type++){
  93. if($length <= Util::getMaxLength($type, $mode, $this->errorCorrectLevel)){
  94. $this->typeNumber = $type;
  95. return $this;
  96. }
  97. }
  98. }
  99. return $this;
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function output(){
  105. $this->qrOutputInterface->setMatrix($this->getRawData());
  106. return $this->qrOutputInterface->dump();
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function getRawData(){
  112. $minLostPoint = 0;
  113. $pattern = 0;
  114. for($i = 0; $i < 8; $i++){
  115. $this->getMatrix(true, $i);
  116. $lostPoint = 0;
  117. // LEVEL1
  118. for($row = 0; $row < $this->pixelCount; $row++){
  119. for($col = 0; $col < $this->pixelCount; $col++){
  120. $sameCount = 0;
  121. $dark = $this->matrix[$row][$col];
  122. for($r = -1; $r <= 1; $r++){
  123. if($row + $r < 0 || $this->pixelCount <= $row + $r){
  124. continue;
  125. }
  126. for($c = -1; $c <= 1; $c++){
  127. if(($r === 0 && $c === 0) || ($col + $c < 0 || $this->pixelCount <= $col + $c)){
  128. continue;
  129. }
  130. if($this->matrix[$row + $r][$col + $c] === $dark){
  131. $sameCount++;
  132. }
  133. }
  134. }
  135. if($sameCount > 5){
  136. $lostPoint += (3 + $sameCount - 5);
  137. }
  138. }
  139. }
  140. // LEVEL2
  141. for($row = 0; $row < $this->pixelCount - 1; $row++){
  142. for($col = 0; $col < $this->pixelCount - 1; $col++){
  143. $count = 0;
  144. if(
  145. $this->matrix[$row ][$col ]
  146. || $this->matrix[$row ][$col + 1]
  147. || $this->matrix[$row + 1][$col ]
  148. || $this->matrix[$row + 1][$col + 1]
  149. ){
  150. $count++;
  151. }
  152. if($count === 0 || $count === 4){
  153. $lostPoint += 3;
  154. }
  155. }
  156. }
  157. // LEVEL3
  158. for($row = 0; $row < $this->pixelCount; $row++){
  159. for($col = 0; $col < $this->pixelCount - 6; $col++){
  160. if(
  161. $this->matrix[$row][$col ]
  162. && !$this->matrix[$row][$col + 1]
  163. && $this->matrix[$row][$col + 2]
  164. && $this->matrix[$row][$col + 3]
  165. && $this->matrix[$row][$col + 4]
  166. && !$this->matrix[$row][$col + 5]
  167. && $this->matrix[$row][$col + 6]
  168. ){
  169. $lostPoint += 40;
  170. }
  171. }
  172. }
  173. for($col = 0; $col < $this->pixelCount; $col++){
  174. for($row = 0; $row < $this->pixelCount - 6; $row++){
  175. if(
  176. $this->matrix[$row ][$col]
  177. && !$this->matrix[$row + 1][$col]
  178. && $this->matrix[$row + 2][$col]
  179. && $this->matrix[$row + 3][$col]
  180. && $this->matrix[$row + 4][$col]
  181. && !$this->matrix[$row + 5][$col]
  182. && $this->matrix[$row + 6][$col]
  183. ){
  184. $lostPoint += 40;
  185. }
  186. }
  187. }
  188. // LEVEL4
  189. $darkCount = 0;
  190. for($col = 0; $col < $this->pixelCount; $col++){
  191. for($row = 0; $row < $this->pixelCount; $row++){
  192. if($this->matrix[$row][$col]){
  193. $darkCount++;
  194. }
  195. }
  196. }
  197. $ratio = abs(100 * $darkCount / $this->pixelCount / $this->pixelCount - 50) / 5;
  198. $lostPoint += $ratio * 10;
  199. if($i === 0 || $minLostPoint > $lostPoint){
  200. $minLostPoint = $lostPoint;
  201. $pattern = $i;
  202. }
  203. }
  204. $this->getMatrix(false, $pattern);
  205. return $this->matrix;
  206. }
  207. /**
  208. * @param bool $test
  209. * @param int $pattern
  210. */
  211. protected function setTypeInfo($test, $pattern){
  212. $this->setPattern();
  213. $bits = Util::getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern);
  214. for($i = 0; $i < 15; $i++){
  215. $mod = !$test && (($bits >> $i) & 1) === 1;
  216. switch(true){
  217. case $i < 6:$this->matrix[$i ][8] = $mod; break;
  218. case $i < 8:$this->matrix[$i + 1][8] = $mod; break;
  219. default:
  220. $this->matrix[$this->pixelCount - 15 + $i][8] = $mod;
  221. }
  222. switch(true){
  223. case $i < 8:$this->matrix[8][$this->pixelCount - $i - 1] = $mod; break;
  224. case $i < 9:$this->matrix[8][ 15 + 1 - $i - 1] = $mod; break;
  225. default:
  226. $this->matrix[8][15 - $i - 1] = $mod;
  227. }
  228. }
  229. $this->matrix[$this->pixelCount - 8][8] = !$test;
  230. }
  231. /**
  232. * @throws \chillerlan\QRCode\QRCodeException
  233. */
  234. protected function setPattern(){
  235. // setupPositionProbePattern
  236. foreach([[0, 0], [$this->pixelCount - 7, 0], [0, $this->pixelCount - 7]] as $grid){
  237. $row = $grid[0];
  238. $col = $grid[1];
  239. for($r = -1; $r <= 7; $r++){
  240. for($c = -1; $c <= 7; $c++){
  241. if($row + $r <= -1 || $this->pixelCount <= $row + $r || $col + $c <= -1 || $this->pixelCount <= $col + $c){
  242. continue;
  243. }
  244. $this->matrix[$row + $r][$col + $c] =
  245. (0 <= $r && $r <= 6 && ($c === 0 || $c === 6))
  246. || (0 <= $c && $c <= 6 && ($r === 0 || $r === 6))
  247. || (2 <= $c && $c <= 4 && 2 <= $r && $r <= 4);
  248. }
  249. }
  250. }
  251. // setupPositionAdjustPattern
  252. $PATTERN_POSITION = QRConst::PATTERN_POSITION; // PHP5 compat
  253. $pos = $PATTERN_POSITION[$this->typeNumber - 1];
  254. foreach($pos as $i => $posI){
  255. foreach($pos as $j => $posJ){
  256. if($this->matrix[$posI][$posJ] !== null){
  257. continue;
  258. }
  259. for($row = -2; $row <= 2; $row++){
  260. for($col = -2; $col <= 2; $col++){
  261. $this->matrix[$posI + $row][$posJ + $col] =
  262. $row === -2 || $row === 2
  263. || $col === -2 || $col === 2
  264. ||($row === 0 && $col === 0);
  265. }
  266. }
  267. }
  268. }
  269. // setupTimingPattern
  270. for($i = 8; $i < $this->pixelCount - 8; $i++){
  271. if($this->matrix[$i][6] !== null){
  272. continue;
  273. }
  274. $this->matrix[$i][6] = $this->matrix[6][$i] = $i % 2 === 0;
  275. }
  276. }
  277. /**
  278. * @param bool $test
  279. *
  280. * @return $this
  281. */
  282. protected function setTypeNumber($test){
  283. $bits = Util::getBCHTypeNumber($this->typeNumber);
  284. for($i = 0; $i < 18; $i++){
  285. $a = (int)floor($i / 3);
  286. $b = $i % 3 + $this->pixelCount - 8 - 3;
  287. $this->matrix[$a][$b] = $this->matrix[$b][$a] = !$test && (($bits >> $i) & 1) === 1;
  288. }
  289. }
  290. /**
  291. * @param bool $test
  292. * @param int $pattern
  293. *
  294. * @throws \chillerlan\QRCode\QRCodeException
  295. */
  296. protected function getMatrix($test, $pattern){
  297. if($this->typeNumber < 1 || $this->typeNumber > 10){
  298. throw new QRCodeException('Invalid type number '.$this->typeNumber);
  299. }
  300. $this->pixelCount = $this->typeNumber * 4 + 17;
  301. $this->matrix = array_fill(0, $this->pixelCount, array_fill(0, $this->pixelCount, null));
  302. $this->setTypeInfo($test, $pattern);
  303. if($this->typeNumber >= 7){
  304. $this->setTypeNumber($test);
  305. }
  306. $this->mapData($pattern);
  307. }
  308. /**
  309. * @param int $pattern
  310. *
  311. * @throws \chillerlan\QRCode\QRCodeException
  312. */
  313. protected function mapData($pattern){
  314. $this->createData();
  315. $data = $this->createBytes();
  316. $inc = -1;
  317. $row = $this->pixelCount - 1;
  318. $bitIndex = 7;
  319. $byteIndex = 0;
  320. $dataCount = count($data);
  321. for($col = $this->pixelCount - 1; $col > 0; $col -= 2){
  322. if($col === 6){
  323. $col--;
  324. }
  325. while(true){
  326. for($c = 0; $c < 2; $c++){
  327. $_col = $col - $c;
  328. if($this->matrix[$row][$_col] === null){
  329. $dark = false;
  330. if($byteIndex < $dataCount){
  331. $dark = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
  332. }
  333. $a = $row + $_col;
  334. $m = $row * $_col;
  335. $MASK_PATTERN = [
  336. QRConst::MASK_PATTERN000 => $a % 2,
  337. QRConst::MASK_PATTERN001 => $row % 2,
  338. QRConst::MASK_PATTERN010 => $_col % 3,
  339. QRConst::MASK_PATTERN011 => $a % 3,
  340. QRConst::MASK_PATTERN100 => (floor($row / 2) + floor($_col / 3)) % 2,
  341. QRConst::MASK_PATTERN101 => $m % 2 + $m % 3,
  342. QRConst::MASK_PATTERN110 => ($m % 2 + $m % 3) % 2,
  343. QRConst::MASK_PATTERN111 => ($m % 3 + $a % 2) % 2,
  344. ][$pattern];
  345. if($MASK_PATTERN === 0){
  346. $dark = !$dark;
  347. }
  348. $this->matrix[$row][$_col] = $dark;
  349. $bitIndex--;
  350. if($bitIndex === -1){
  351. $byteIndex++;
  352. $bitIndex = 7;
  353. }
  354. }
  355. }
  356. $row += $inc;
  357. if($row < 0 || $this->pixelCount <= $row){
  358. $row -= $inc;
  359. $inc = -$inc;
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. /**
  366. * @return $this
  367. * @throws \chillerlan\QRCode\QRCodeException
  368. */
  369. protected function createData(){
  370. if(!isset($this->errorCorrectLevel)){
  371. throw new QRCodeException('Invalid error correct level '.$this->errorCorrectLevel);
  372. }
  373. $this->bitBuffer->clear();
  374. $MAX_BITS = QRConst::MAX_BITS; // php5 compat
  375. $MAX_BITS = $MAX_BITS[$this->typeNumber][$this->errorCorrectLevel];
  376. /** @noinspection PhpUndefinedFieldInspection */
  377. $this->bitBuffer->put($this->qrDataInterface->mode, 4);
  378. /** @noinspection PhpUndefinedFieldInspection */
  379. $this->bitBuffer->put(
  380. $this->qrDataInterface->mode === QRConst::MODE_KANJI ? floor($this->qrDataInterface->dataLength / 2) : $this->qrDataInterface->dataLength,
  381. $this->qrDataInterface->getLengthInBits($this->typeNumber)
  382. );
  383. $this->qrDataInterface->write($this->bitBuffer);
  384. if($this->bitBuffer->length > $MAX_BITS){
  385. throw new QRCodeException('code length overflow. ('.$this->bitBuffer->length.' > '.$MAX_BITS.'bit)');
  386. }
  387. // end code.
  388. if($this->bitBuffer->length + 4 <= $MAX_BITS){
  389. $this->bitBuffer->put(0, 4);
  390. }
  391. // padding
  392. while($this->bitBuffer->length % 8 !== 0){
  393. $this->bitBuffer->putBit(false);
  394. }
  395. // padding
  396. while(true){
  397. if($this->bitBuffer->length >= $MAX_BITS){
  398. break;
  399. }
  400. $this->bitBuffer->put(QRConst::PAD0, 8);
  401. if($this->bitBuffer->length >= $MAX_BITS){
  402. break;
  403. }
  404. $this->bitBuffer->put(QRConst::PAD1, 8);
  405. }
  406. }
  407. /**
  408. * @return array
  409. * @throws \chillerlan\QRCode\QRCodeException
  410. */
  411. protected function createBytes(){
  412. $totalCodeCount = $maxDcCount = $maxEcCount = $offset = $index = 0;
  413. $rsBlocks = Util::getRSBlocks($this->typeNumber, $this->errorCorrectLevel);
  414. $rsBlockCount = count($rsBlocks);
  415. $dcdata = $ecdata = array_fill(0, $rsBlockCount, null);
  416. foreach($rsBlocks as $key => $value){
  417. $rsBlockTotal = $value[0];
  418. $rsBlockDataCount = $value[1];
  419. $maxDcCount = max($maxDcCount, $rsBlockDataCount);
  420. $maxEcCount = max($maxEcCount, $rsBlockTotal - $rsBlockDataCount);
  421. $dcdata[$key] = array_fill(0, $rsBlockDataCount, null);
  422. foreach($dcdata[$key] as $i => &$_dcdata){
  423. $bdata = $this->bitBuffer->buffer;
  424. $_dcdata = 0xff & $bdata[$i + $offset];
  425. }
  426. $offset += $rsBlockDataCount;
  427. $rsPoly = new Polynomial;
  428. $modPoly = new Polynomial;
  429. for($i = 0; $i < $rsBlockTotal - $rsBlockDataCount; $i++){
  430. $modPoly->setNum([1, $modPoly->gexp($i)]);
  431. $rsPoly->multiply($modPoly->num);
  432. }
  433. $rsPolyCount = count($rsPoly->num);
  434. $modPoly->setNum($dcdata[$key], $rsPolyCount - 1)->mod($rsPoly->num);
  435. $ecdata[$key] = array_fill(0, $rsPolyCount - 1, null);
  436. $add = count($modPoly->num) - count($ecdata[$key]);
  437. foreach($ecdata[$key] as $i => &$_ecdata){
  438. $modIndex = $i + $add;
  439. $_ecdata = $modIndex >= 0 ? $modPoly->num[$modIndex] : 0;
  440. }
  441. $totalCodeCount += $rsBlockTotal;
  442. }
  443. $data = array_fill(0, $totalCodeCount, null);
  444. for($i = 0; $i < $maxDcCount; $i++){
  445. for($key = 0; $key < $rsBlockCount; $key++){
  446. if($i < count($dcdata[$key])){
  447. $data[$index++] = $dcdata[$key][$i];
  448. }
  449. }
  450. }
  451. for($i = 0; $i < $maxEcCount; $i++){
  452. for($key = 0; $key < $rsBlockCount; $key++){
  453. if($i < count($ecdata[$key])){
  454. $data[$index++] = $ecdata[$key][$i];
  455. }
  456. }
  457. }
  458. return $data;
  459. }
  460. }