QRCode.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. <?php
  2. namespace addons\epay\library;
  3. //---------------------------------------------------------------
  4. // QRCode for PHP5
  5. //
  6. // Copyright (c) 2009 Kazuhiko Arase
  7. //
  8. // URL: http://www.d-project.com/
  9. //
  10. // Licensed under the MIT license:
  11. // http://www.opensource.org/licenses/mit-license.php
  12. //
  13. // The word "QR Code" is registered trademark of
  14. // DENSO WAVE INCORPORATED
  15. // http://www.denso-wave.com/qrcode/faqpatent-e.html
  16. //
  17. //---------------------------------------------------------------------
  18. //---------------------------------------------------------------
  19. // QRCode
  20. //---------------------------------------------------------------
  21. define("QR_PAD0", 0xEC);
  22. define("QR_PAD1", 0x11);
  23. class QRCode
  24. {
  25. var $typeNumber;
  26. var $modules;
  27. var $moduleCount;
  28. var $errorCorrectLevel;
  29. var $qrDataList;
  30. function __construct()
  31. {
  32. $this->typeNumber = 1;
  33. $this->errorCorrectLevel = QR_ERROR_CORRECT_LEVEL_H;
  34. $this->qrDataList = array();
  35. }
  36. function getTypeNumber()
  37. {
  38. return $this->typeNumber;
  39. }
  40. function setTypeNumber($typeNumber)
  41. {
  42. $this->typeNumber = $typeNumber;
  43. }
  44. function getErrorCorrectLevel()
  45. {
  46. return $this->errorCorrectLevel;
  47. }
  48. function setErrorCorrectLevel($errorCorrectLevel)
  49. {
  50. $this->errorCorrectLevel = $errorCorrectLevel;
  51. }
  52. function addData($data, $mode = 0)
  53. {
  54. if ($mode == 0) {
  55. $mode = QRUtil::getMode($data);
  56. }
  57. switch ($mode) {
  58. case QR_MODE_NUMBER :
  59. $this->addDataImpl(new QRNumber($data));
  60. break;
  61. case QR_MODE_ALPHA_NUM :
  62. $this->addDataImpl(new QRAlphaNum($data));
  63. break;
  64. case QR_MODE_8BIT_BYTE :
  65. $this->addDataImpl(new QR8BitByte($data));
  66. break;
  67. case QR_MODE_KANJI :
  68. $this->addDataImpl(new QRKanji($data));
  69. break;
  70. default :
  71. trigger_error("mode:$mode", E_USER_ERROR);
  72. }
  73. }
  74. function clearData()
  75. {
  76. $this->qrDataList = array();
  77. }
  78. function addDataImpl($qrData)
  79. {
  80. $this->qrDataList[] = $qrData;
  81. }
  82. function getDataCount()
  83. {
  84. return count($this->qrDataList);
  85. }
  86. function getData($index)
  87. {
  88. return $this->qrDataList[$index];
  89. }
  90. function isDark($row, $col)
  91. {
  92. if ($this->modules[$row][$col] !== null) {
  93. return $this->modules[$row][$col];
  94. } else {
  95. return false;
  96. }
  97. }
  98. function getModuleCount()
  99. {
  100. return $this->moduleCount;
  101. }
  102. // used for converting fg/bg colors (e.g. #0000ff = 0x0000FF)
  103. // added 2015.07.27 ~ DoktorJ
  104. function hex2rgb($hex = 0x0)
  105. {
  106. return array(
  107. 'r' => floor($hex / 65536),
  108. 'g' => floor($hex / 256) % 256,
  109. 'b' => $hex % 256
  110. );
  111. }
  112. function make()
  113. {
  114. $this->makeImpl(false, $this->getBestMaskPattern());
  115. }
  116. function getBestMaskPattern()
  117. {
  118. $minLostPoint = 0;
  119. $pattern = 0;
  120. for ($i = 0; $i < 8; $i++) {
  121. $this->makeImpl(true, $i);
  122. $lostPoint = QRUtil::getLostPoint($this);
  123. if ($i == 0 || $minLostPoint > $lostPoint) {
  124. $minLostPoint = $lostPoint;
  125. $pattern = $i;
  126. }
  127. }
  128. return $pattern;
  129. }
  130. function createNullArray($length)
  131. {
  132. $nullArray = array();
  133. for ($i = 0; $i < $length; $i++) {
  134. $nullArray[] = null;
  135. }
  136. return $nullArray;
  137. }
  138. function makeImpl($test, $maskPattern)
  139. {
  140. $this->moduleCount = $this->typeNumber * 4 + 17;
  141. $this->modules = array();
  142. for ($i = 0; $i < $this->moduleCount; $i++) {
  143. $this->modules[] = QRCode::createNullArray($this->moduleCount);
  144. }
  145. $this->setupPositionProbePattern(0, 0);
  146. $this->setupPositionProbePattern($this->moduleCount - 7, 0);
  147. $this->setupPositionProbePattern(0, $this->moduleCount - 7);
  148. $this->setupPositionAdjustPattern();
  149. $this->setupTimingPattern();
  150. $this->setupTypeInfo($test, $maskPattern);
  151. if ($this->typeNumber >= 7) {
  152. $this->setupTypeNumber($test);
  153. }
  154. $dataArray = $this->qrDataList;
  155. $data = QRCode::createData($this->typeNumber, $this->errorCorrectLevel, $dataArray);
  156. $this->mapData($data, $maskPattern);
  157. }
  158. function mapData(&$data, $maskPattern)
  159. {
  160. $inc = -1;
  161. $row = $this->moduleCount - 1;
  162. $bitIndex = 7;
  163. $byteIndex = 0;
  164. for ($col = $this->moduleCount - 1; $col > 0; $col -= 2) {
  165. if ($col == 6) $col--;
  166. while (true) {
  167. for ($c = 0; $c < 2; $c++) {
  168. if ($this->modules[$row][$col - $c] === null) {
  169. $dark = false;
  170. if ($byteIndex < count($data)) {
  171. $dark = ((($data[$byteIndex] >> $bitIndex) & 1) == 1);
  172. }
  173. if (QRUtil::getMask($maskPattern, $row, $col - $c)) {
  174. $dark = !$dark;
  175. }
  176. $this->modules[$row][$col - $c] = $dark;
  177. $bitIndex--;
  178. if ($bitIndex == -1) {
  179. $byteIndex++;
  180. $bitIndex = 7;
  181. }
  182. }
  183. }
  184. $row += $inc;
  185. if ($row < 0 || $this->moduleCount <= $row) {
  186. $row -= $inc;
  187. $inc = -$inc;
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. function setupPositionAdjustPattern()
  194. {
  195. $pos = QRUtil::getPatternPosition($this->typeNumber);
  196. for ($i = 0; $i < count($pos); $i++) {
  197. for ($j = 0; $j < count($pos); $j++) {
  198. $row = $pos[$i];
  199. $col = $pos[$j];
  200. if ($this->modules[$row][$col] !== null) {
  201. continue;
  202. }
  203. for ($r = -2; $r <= 2; $r++) {
  204. for ($c = -2; $c <= 2; $c++) {
  205. $this->modules[$row + $r][$col + $c] =
  206. $r == -2 || $r == 2 || $c == -2 || $c == 2 || ($r == 0 && $c == 0);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. function setupPositionProbePattern($row, $col)
  213. {
  214. for ($r = -1; $r <= 7; $r++) {
  215. for ($c = -1; $c <= 7; $c++) {
  216. if ($row + $r <= -1 || $this->moduleCount <= $row + $r
  217. || $col + $c <= -1 || $this->moduleCount <= $col + $c) {
  218. continue;
  219. }
  220. $this->modules[$row + $r][$col + $c] =
  221. (0 <= $r && $r <= 6 && ($c == 0 || $c == 6))
  222. || (0 <= $c && $c <= 6 && ($r == 0 || $r == 6))
  223. || (2 <= $r && $r <= 4 && 2 <= $c && $c <= 4);
  224. }
  225. }
  226. }
  227. function setupTimingPattern()
  228. {
  229. for ($i = 8; $i < $this->moduleCount - 8; $i++) {
  230. if ($this->modules[$i][6] !== null || $this->modules[6][$i] !== null) {
  231. continue;
  232. }
  233. $this->modules[$i][6] = ($i % 2 == 0);
  234. $this->modules[6][$i] = ($i % 2 == 0);
  235. }
  236. }
  237. function setupTypeNumber($test)
  238. {
  239. $bits = QRUtil::getBCHTypeNumber($this->typeNumber);
  240. for ($i = 0; $i < 18; $i++) {
  241. $mod = (!$test && (($bits >> $i) & 1) == 1);
  242. $this->modules[(int)floor($i / 3)][$i % 3 + $this->moduleCount - 8 - 3] = $mod;
  243. $this->modules[$i % 3 + $this->moduleCount - 8 - 3][floor($i / 3)] = $mod;
  244. }
  245. }
  246. function setupTypeInfo($test, $maskPattern)
  247. {
  248. $data = ($this->errorCorrectLevel << 3) | $maskPattern;
  249. $bits = QRUtil::getBCHTypeInfo($data);
  250. for ($i = 0; $i < 15; $i++) {
  251. $mod = (!$test && (($bits >> $i) & 1) == 1);
  252. if ($i < 6) {
  253. $this->modules[$i][8] = $mod;
  254. } else if ($i < 8) {
  255. $this->modules[$i + 1][8] = $mod;
  256. } else {
  257. $this->modules[$this->moduleCount - 15 + $i][8] = $mod;
  258. }
  259. if ($i < 8) {
  260. $this->modules[8][$this->moduleCount - $i - 1] = $mod;
  261. } else if ($i < 9) {
  262. $this->modules[8][15 - $i - 1 + 1] = $mod;
  263. } else {
  264. $this->modules[8][15 - $i - 1] = $mod;
  265. }
  266. }
  267. $this->modules[$this->moduleCount - 8][8] = !$test;
  268. }
  269. function createData($typeNumber, $errorCorrectLevel, $dataArray)
  270. {
  271. $rsBlocks = QRRSBlock::getRSBlocks($typeNumber, $errorCorrectLevel);
  272. $buffer = new QRBitBuffer();
  273. for ($i = 0; $i < count($dataArray); $i++) {
  274. /** @var \QRData $data */
  275. $data = $dataArray[$i];
  276. $buffer->put($data->getMode(), 4);
  277. $buffer->put($data->getLength(), $data->getLengthInBits($typeNumber));
  278. $data->write($buffer);
  279. }
  280. $totalDataCount = 0;
  281. for ($i = 0; $i < count($rsBlocks); $i++) {
  282. $totalDataCount += $rsBlocks[$i]->getDataCount();
  283. }
  284. if ($buffer->getLengthInBits() > $totalDataCount * 8) {
  285. trigger_error("code length overflow. ("
  286. . $buffer->getLengthInBits()
  287. . ">"
  288. . $totalDataCount * 8
  289. . ")", E_USER_ERROR);
  290. }
  291. // end code.
  292. if ($buffer->getLengthInBits() + 4 <= $totalDataCount * 8) {
  293. $buffer->put(0, 4);
  294. }
  295. // padding
  296. while ($buffer->getLengthInBits() % 8 != 0) {
  297. $buffer->putBit(false);
  298. }
  299. // padding
  300. while (true) {
  301. if ($buffer->getLengthInBits() >= $totalDataCount * 8) {
  302. break;
  303. }
  304. $buffer->put(QR_PAD0, 8);
  305. if ($buffer->getLengthInBits() >= $totalDataCount * 8) {
  306. break;
  307. }
  308. $buffer->put(QR_PAD1, 8);
  309. }
  310. return QRCode::createBytes($buffer, $rsBlocks);
  311. }
  312. /**
  313. * @param \QRBitBuffer $buffer
  314. * @param \QRRSBlock[] $rsBlocks
  315. *
  316. * @return array
  317. */
  318. function createBytes(&$buffer, &$rsBlocks)
  319. {
  320. $offset = 0;
  321. $maxDcCount = 0;
  322. $maxEcCount = 0;
  323. $dcdata = QRCode::createNullArray(count($rsBlocks));
  324. $ecdata = QRCode::createNullArray(count($rsBlocks));
  325. $rsBlockCount = count($rsBlocks);
  326. for ($r = 0; $r < $rsBlockCount; $r++) {
  327. $dcCount = $rsBlocks[$r]->getDataCount();
  328. $ecCount = $rsBlocks[$r]->getTotalCount() - $dcCount;
  329. $maxDcCount = max($maxDcCount, $dcCount);
  330. $maxEcCount = max($maxEcCount, $ecCount);
  331. $dcdata[$r] = QRCode::createNullArray($dcCount);
  332. $dcDataCount = count($dcdata[$r]);
  333. for ($i = 0; $i < $dcDataCount; $i++) {
  334. $bdata = $buffer->getBuffer();
  335. $dcdata[$r][$i] = 0xff & $bdata[$i + $offset];
  336. }
  337. $offset += $dcCount;
  338. $rsPoly = QRUtil::getErrorCorrectPolynomial($ecCount);
  339. $rawPoly = new QRPolynomial($dcdata[$r], $rsPoly->getLength() - 1);
  340. $modPoly = $rawPoly->mod($rsPoly);
  341. $ecdata[$r] = QRCode::createNullArray($rsPoly->getLength() - 1);
  342. $ecDataCount = count($ecdata[$r]);
  343. for ($i = 0; $i < $ecDataCount; $i++) {
  344. $modIndex = $i + $modPoly->getLength() - count($ecdata[$r]);
  345. $ecdata[$r][$i] = ($modIndex >= 0) ? $modPoly->get($modIndex) : 0;
  346. }
  347. }
  348. $totalCodeCount = 0;
  349. for ($i = 0; $i < $rsBlockCount; $i++) {
  350. $totalCodeCount += $rsBlocks[$i]->getTotalCount();
  351. }
  352. $data = QRCode::createNullArray($totalCodeCount);
  353. $index = 0;
  354. for ($i = 0; $i < $maxDcCount; $i++) {
  355. for ($r = 0; $r < $rsBlockCount; $r++) {
  356. if ($i < count($dcdata[$r])) {
  357. $data[$index++] = $dcdata[$r][$i];
  358. }
  359. }
  360. }
  361. for ($i = 0; $i < $maxEcCount; $i++) {
  362. for ($r = 0; $r < $rsBlockCount; $r++) {
  363. if ($i < count($ecdata[$r])) {
  364. $data[$index++] = $ecdata[$r][$i];
  365. }
  366. }
  367. }
  368. return $data;
  369. }
  370. static function getMinimumQRCode($data, $errorCorrectLevel = QR_ERROR_CORRECT_LEVEL_L)
  371. {
  372. $mode = QRUtil::getMode($data);
  373. $qr = new QRCode();
  374. $qr->setErrorCorrectLevel($errorCorrectLevel);
  375. $qr->addData($data, $mode);
  376. $qrData = $qr->getData(0);
  377. $length = $qrData->getLength();
  378. for ($typeNumber = 1; $typeNumber <= 40; $typeNumber++) {
  379. if ($length <= QRUtil::getMaxLength($typeNumber, $mode, $errorCorrectLevel)) {
  380. $qr->setTypeNumber($typeNumber);
  381. break;
  382. }
  383. }
  384. $qr->make();
  385. return $qr;
  386. }
  387. // added $fg (foreground), $bg (background), and $bgtrans (use transparent bg) parameters
  388. // also added some simple error checking on parameters
  389. // updated 2015.07.27 ~ DoktorJ
  390. function createImage($size = 2, $margin = 2, $fg = 0x000000, $bg = 0xFFFFFF, $bgtrans = false)
  391. {
  392. // size/margin EC
  393. if (!is_numeric($size)) $size = 2;
  394. if (!is_numeric($margin)) $margin = 2;
  395. if ($size < 1) $size = 1;
  396. if ($margin < 0) $margin = 0;
  397. $image_size = $this->getModuleCount() * $size + $margin * 2;
  398. $image = imagecreatetruecolor($image_size, $image_size);
  399. // fg/bg EC
  400. if ($fg < 0 || $fg > 0xFFFFFF) $fg = 0x0;
  401. if ($bg < 0 || $bg > 0xFFFFFF) $bg = 0xFFFFFF;
  402. // convert hexadecimal RGB to arrays for imagecolorallocate
  403. $fgrgb = $this->hex2rgb($fg);
  404. $bgrgb = $this->hex2rgb($bg);
  405. // replace $black and $white with $fgc and $bgc
  406. $fgc = imagecolorallocate($image, $fgrgb['r'], $fgrgb['g'], $fgrgb['b']);
  407. $bgc = imagecolorallocate($image, $bgrgb['r'], $bgrgb['g'], $bgrgb['b']);
  408. if ($bgtrans) imagecolortransparent($image, $bgc);
  409. // update $white to $bgc
  410. imagefilledrectangle($image, 0, 0, $image_size, $image_size, $bgc);
  411. for ($r = 0; $r < $this->getModuleCount(); $r++) {
  412. for ($c = 0; $c < $this->getModuleCount(); $c++) {
  413. if ($this->isDark($r, $c)) {
  414. // update $black to $fgc
  415. imagefilledrectangle($image,
  416. $margin + $c * $size,
  417. $margin + $r * $size,
  418. $margin + ($c + 1) * $size - 1,
  419. $margin + ($r + 1) * $size - 1,
  420. $fgc);
  421. }
  422. }
  423. }
  424. return $image;
  425. }
  426. function printHTML($size = "2px")
  427. {
  428. $style = "border-style:none;border-collapse:collapse;margin:0px;padding:0px;";
  429. print("<table style='$style'>");
  430. for ($r = 0; $r < $this->getModuleCount(); $r++) {
  431. print("<tr style='$style'>");
  432. for ($c = 0; $c < $this->getModuleCount(); $c++) {
  433. $color = $this->isDark($r, $c) ? "#000000" : "#ffffff";
  434. print("<td style='$style;width:$size;height:$size;background-color:$color'></td>");
  435. }
  436. print("</tr>");
  437. }
  438. print("</table>");
  439. }
  440. }
  441. //---------------------------------------------------------------
  442. // QRUtil
  443. //---------------------------------------------------------------
  444. define("QR_G15", (1 << 10) | (1 << 8) | (1 << 5)
  445. | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0));
  446. define("QR_G18", (1 << 12) | (1 << 11) | (1 << 10)
  447. | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0));
  448. define("QR_G15_MASK", (1 << 14) | (1 << 12) | (1 << 10)
  449. | (1 << 4) | (1 << 1));
  450. class QRUtil
  451. {
  452. static $QR_MAX_LENGTH = array(
  453. array(array(41, 25, 17, 10), array(34, 20, 14, 8), array(27, 16, 11, 7), array(17, 10, 7, 4)),
  454. array(array(77, 47, 32, 20), array(63, 38, 26, 16), array(48, 29, 20, 12), array(34, 20, 14, 8)),
  455. array(array(127, 77, 53, 32), array(101, 61, 42, 26), array(77, 47, 32, 20), array(58, 35, 24, 15)),
  456. array(array(187, 114, 78, 48), array(149, 90, 62, 38), array(111, 67, 46, 28), array(82, 50, 34, 21)),
  457. array(array(255, 154, 106, 65), array(202, 122, 84, 52), array(144, 87, 60, 37), array(106, 64, 44, 27)),
  458. array(array(322, 195, 134, 82), array(255, 154, 106, 65), array(178, 108, 74, 45), array(139, 84, 58, 36)),
  459. array(array(370, 224, 154, 95), array(293, 178, 122, 75), array(207, 125, 86, 53), array(154, 93, 64, 39)),
  460. array(array(461, 279, 192, 118), array(365, 221, 152, 93), array(259, 157, 108, 66), array(202, 122, 84, 52)),
  461. array(array(552, 335, 230, 141), array(432, 262, 180, 111), array(312, 189, 130, 80), array(235, 143, 98, 60)),
  462. array(array(652, 395, 271, 167), array(513, 311, 213, 131), array(364, 221, 151, 93), array(288, 174, 119, 74))
  463. );
  464. static $QR_PATTERN_POSITION_TABLE = array(
  465. array(),
  466. array(6, 18),
  467. array(6, 22),
  468. array(6, 26),
  469. array(6, 30),
  470. array(6, 34),
  471. array(6, 22, 38),
  472. array(6, 24, 42),
  473. array(6, 26, 46),
  474. array(6, 28, 50),
  475. array(6, 30, 54),
  476. array(6, 32, 58),
  477. array(6, 34, 62),
  478. array(6, 26, 46, 66),
  479. array(6, 26, 48, 70),
  480. array(6, 26, 50, 74),
  481. array(6, 30, 54, 78),
  482. array(6, 30, 56, 82),
  483. array(6, 30, 58, 86),
  484. array(6, 34, 62, 90),
  485. array(6, 28, 50, 72, 94),
  486. array(6, 26, 50, 74, 98),
  487. array(6, 30, 54, 78, 102),
  488. array(6, 28, 54, 80, 106),
  489. array(6, 32, 58, 84, 110),
  490. array(6, 30, 58, 86, 114),
  491. array(6, 34, 62, 90, 118),
  492. array(6, 26, 50, 74, 98, 122),
  493. array(6, 30, 54, 78, 102, 126),
  494. array(6, 26, 52, 78, 104, 130),
  495. array(6, 30, 56, 82, 108, 134),
  496. array(6, 34, 60, 86, 112, 138),
  497. array(6, 30, 58, 86, 114, 142),
  498. array(6, 34, 62, 90, 118, 146),
  499. array(6, 30, 54, 78, 102, 126, 150),
  500. array(6, 24, 50, 76, 102, 128, 154),
  501. array(6, 28, 54, 80, 106, 132, 158),
  502. array(6, 32, 58, 84, 110, 136, 162),
  503. array(6, 26, 54, 82, 110, 138, 166),
  504. array(6, 30, 58, 86, 114, 142, 170)
  505. );
  506. static function getPatternPosition($typeNumber)
  507. {
  508. return self::$QR_PATTERN_POSITION_TABLE[$typeNumber - 1];
  509. }
  510. static function getMaxLength($typeNumber, $mode, $errorCorrectLevel)
  511. {
  512. $t = $typeNumber - 1;
  513. $e = 0;
  514. $m = 0;
  515. switch ($errorCorrectLevel) {
  516. case QR_ERROR_CORRECT_LEVEL_L :
  517. $e = 0;
  518. break;
  519. case QR_ERROR_CORRECT_LEVEL_M :
  520. $e = 1;
  521. break;
  522. case QR_ERROR_CORRECT_LEVEL_Q :
  523. $e = 2;
  524. break;
  525. case QR_ERROR_CORRECT_LEVEL_H :
  526. $e = 3;
  527. break;
  528. default :
  529. trigger_error("e:$errorCorrectLevel", E_USER_ERROR);
  530. }
  531. switch ($mode) {
  532. case QR_MODE_NUMBER :
  533. $m = 0;
  534. break;
  535. case QR_MODE_ALPHA_NUM :
  536. $m = 1;
  537. break;
  538. case QR_MODE_8BIT_BYTE :
  539. $m = 2;
  540. break;
  541. case QR_MODE_KANJI :
  542. $m = 3;
  543. break;
  544. default :
  545. trigger_error("m:$mode", E_USER_ERROR);
  546. }
  547. return self::$QR_MAX_LENGTH[$t][$e][$m];
  548. }
  549. static function getErrorCorrectPolynomial($errorCorrectLength)
  550. {
  551. $a = new QRPolynomial(array(1));
  552. for ($i = 0; $i < $errorCorrectLength; $i++) {
  553. $a = $a->multiply(new QRPolynomial(array(1, QRMath::gexp($i))));
  554. }
  555. return $a;
  556. }
  557. static function getMask($maskPattern, $i, $j)
  558. {
  559. switch ($maskPattern) {
  560. case QR_MASK_PATTERN000 :
  561. return ($i + $j) % 2 == 0;
  562. case QR_MASK_PATTERN001 :
  563. return $i % 2 == 0;
  564. case QR_MASK_PATTERN010 :
  565. return $j % 3 == 0;
  566. case QR_MASK_PATTERN011 :
  567. return ($i + $j) % 3 == 0;
  568. case QR_MASK_PATTERN100 :
  569. return (floor($i / 2) + floor($j / 3)) % 2 == 0;
  570. case QR_MASK_PATTERN101 :
  571. return ($i * $j) % 2 + ($i * $j) % 3 == 0;
  572. case QR_MASK_PATTERN110 :
  573. return (($i * $j) % 2 + ($i * $j) % 3) % 2 == 0;
  574. case QR_MASK_PATTERN111 :
  575. return (($i * $j) % 3 + ($i + $j) % 2) % 2 == 0;
  576. default :
  577. trigger_error("mask:$maskPattern", E_USER_ERROR);
  578. }
  579. }
  580. /**
  581. * @param \QRCode $qrCode
  582. *
  583. * @return float|int
  584. */
  585. static function getLostPoint($qrCode)
  586. {
  587. $moduleCount = $qrCode->getModuleCount();
  588. $lostPoint = 0;
  589. // LEVEL1
  590. for ($row = 0; $row < $moduleCount; $row++) {
  591. for ($col = 0; $col < $moduleCount; $col++) {
  592. $sameCount = 0;
  593. $dark = $qrCode->isDark($row, $col);
  594. for ($r = -1; $r <= 1; $r++) {
  595. if ($row + $r < 0 || $moduleCount <= $row + $r) {
  596. continue;
  597. }
  598. for ($c = -1; $c <= 1; $c++) {
  599. if (($col + $c < 0 || $moduleCount <= $col + $c) || ($r == 0 && $c == 0)) {
  600. continue;
  601. }
  602. if ($dark == $qrCode->isDark($row + $r, $col + $c)) {
  603. $sameCount++;
  604. }
  605. }
  606. }
  607. if ($sameCount > 5) {
  608. $lostPoint += (3 + $sameCount - 5);
  609. }
  610. }
  611. }
  612. // LEVEL2
  613. for ($row = 0; $row < $moduleCount - 1; $row++) {
  614. for ($col = 0; $col < $moduleCount - 1; $col++) {
  615. $count = 0;
  616. if ($qrCode->isDark($row, $col)) $count++;
  617. if ($qrCode->isDark($row + 1, $col)) $count++;
  618. if ($qrCode->isDark($row, $col + 1)) $count++;
  619. if ($qrCode->isDark($row + 1, $col + 1)) $count++;
  620. if ($count == 0 || $count == 4) {
  621. $lostPoint += 3;
  622. }
  623. }
  624. }
  625. // LEVEL3
  626. for ($row = 0; $row < $moduleCount; $row++) {
  627. for ($col = 0; $col < $moduleCount - 6; $col++) {
  628. if ($qrCode->isDark($row, $col)
  629. && !$qrCode->isDark($row, $col + 1)
  630. && $qrCode->isDark($row, $col + 2)
  631. && $qrCode->isDark($row, $col + 3)
  632. && $qrCode->isDark($row, $col + 4)
  633. && !$qrCode->isDark($row, $col + 5)
  634. && $qrCode->isDark($row, $col + 6)) {
  635. $lostPoint += 40;
  636. }
  637. }
  638. }
  639. for ($col = 0; $col < $moduleCount; $col++) {
  640. for ($row = 0; $row < $moduleCount - 6; $row++) {
  641. if ($qrCode->isDark($row, $col)
  642. && !$qrCode->isDark($row + 1, $col)
  643. && $qrCode->isDark($row + 2, $col)
  644. && $qrCode->isDark($row + 3, $col)
  645. && $qrCode->isDark($row + 4, $col)
  646. && !$qrCode->isDark($row + 5, $col)
  647. && $qrCode->isDark($row + 6, $col)) {
  648. $lostPoint += 40;
  649. }
  650. }
  651. }
  652. // LEVEL4
  653. $darkCount = 0;
  654. for ($col = 0; $col < $moduleCount; $col++) {
  655. for ($row = 0; $row < $moduleCount; $row++) {
  656. if ($qrCode->isDark($row, $col)) {
  657. $darkCount++;
  658. }
  659. }
  660. }
  661. $ratio = abs(100 * $darkCount / $moduleCount / $moduleCount - 50) / 5;
  662. $lostPoint += $ratio * 10;
  663. return $lostPoint;
  664. }
  665. static function getMode($s)
  666. {
  667. if (QRUtil::isAlphaNum($s)) {
  668. if (QRUtil::isNumber($s)) {
  669. return QR_MODE_NUMBER;
  670. }
  671. return QR_MODE_ALPHA_NUM;
  672. } else if (QRUtil::isKanji($s)) {
  673. return QR_MODE_KANJI;
  674. } else {
  675. return QR_MODE_8BIT_BYTE;
  676. }
  677. }
  678. static function isNumber($s)
  679. {
  680. for ($i = 0; $i < strlen($s); $i++) {
  681. $c = ord($s[$i]);
  682. if (!(QRUtil::toCharCode('0') <= $c && $c <= QRUtil::toCharCode('9'))) {
  683. return false;
  684. }
  685. }
  686. return true;
  687. }
  688. static function isAlphaNum($s)
  689. {
  690. for ($i = 0; $i < strlen($s); $i++) {
  691. $c = ord($s[$i]);
  692. if (!(QRUtil::toCharCode('0') <= $c && $c <= QRUtil::toCharCode('9'))
  693. && !(QRUtil::toCharCode('A') <= $c && $c <= QRUtil::toCharCode('Z'))
  694. && strpos(" $%*+-./:", $s[$i]) === false) {
  695. return false;
  696. }
  697. }
  698. return true;
  699. }
  700. static function isKanji($s)
  701. {
  702. $data = $s;
  703. $i = 0;
  704. while ($i + 1 < strlen($data)) {
  705. $c = ((0xff & ord($data[$i])) << 8) | (0xff & ord($data[$i + 1]));
  706. if (!(0x8140 <= $c && $c <= 0x9FFC) && !(0xE040 <= $c && $c <= 0xEBBF)) {
  707. return false;
  708. }
  709. $i += 2;
  710. }
  711. if ($i < strlen($data)) {
  712. return false;
  713. }
  714. return true;
  715. }
  716. static function toCharCode($s)
  717. {
  718. return ord($s[0]);
  719. }
  720. static function getBCHTypeInfo($data)
  721. {
  722. $d = $data << 10;
  723. while (QRUtil::getBCHDigit($d) - QRUtil::getBCHDigit(QR_G15) >= 0) {
  724. $d ^= (QR_G15 << (QRUtil::getBCHDigit($d) - QRUtil::getBCHDigit(QR_G15)));
  725. }
  726. return (($data << 10) | $d) ^ QR_G15_MASK;
  727. }
  728. static function getBCHTypeNumber($data)
  729. {
  730. $d = $data << 12;
  731. while (QRUtil::getBCHDigit($d) - QRUtil::getBCHDigit(QR_G18) >= 0) {
  732. $d ^= (QR_G18 << (QRUtil::getBCHDigit($d) - QRUtil::getBCHDigit(QR_G18)));
  733. }
  734. return ($data << 12) | $d;
  735. }
  736. static function getBCHDigit($data)
  737. {
  738. $digit = 0;
  739. while ($data != 0) {
  740. $digit++;
  741. $data >>= 1;
  742. }
  743. return $digit;
  744. }
  745. }
  746. //---------------------------------------------------------------
  747. // QRRSBlock
  748. //---------------------------------------------------------------
  749. class QRRSBlock
  750. {
  751. var $totalCount;
  752. var $dataCount;
  753. static $QR_RS_BLOCK_TABLE = array(
  754. // L
  755. // M
  756. // Q
  757. // H
  758. // 1
  759. array(1, 26, 19),
  760. array(1, 26, 16),
  761. array(1, 26, 13),
  762. array(1, 26, 9),
  763. // 2
  764. array(1, 44, 34),
  765. array(1, 44, 28),
  766. array(1, 44, 22),
  767. array(1, 44, 16),
  768. // 3
  769. array(1, 70, 55),
  770. array(1, 70, 44),
  771. array(2, 35, 17),
  772. array(2, 35, 13),
  773. // 4
  774. array(1, 100, 80),
  775. array(2, 50, 32),
  776. array(2, 50, 24),
  777. array(4, 25, 9),
  778. // 5
  779. array(1, 134, 108),
  780. array(2, 67, 43),
  781. array(2, 33, 15, 2, 34, 16),
  782. array(2, 33, 11, 2, 34, 12),
  783. // 6
  784. array(2, 86, 68),
  785. array(4, 43, 27),
  786. array(4, 43, 19),
  787. array(4, 43, 15),
  788. // 7
  789. array(2, 98, 78),
  790. array(4, 49, 31),
  791. array(2, 32, 14, 4, 33, 15),
  792. array(4, 39, 13, 1, 40, 14),
  793. // 8
  794. array(2, 121, 97),
  795. array(2, 60, 38, 2, 61, 39),
  796. array(4, 40, 18, 2, 41, 19),
  797. array(4, 40, 14, 2, 41, 15),
  798. // 9
  799. array(2, 146, 116),
  800. array(3, 58, 36, 2, 59, 37),
  801. array(4, 36, 16, 4, 37, 17),
  802. array(4, 36, 12, 4, 37, 13),
  803. // 10
  804. array(2, 86, 68, 2, 87, 69),
  805. array(4, 69, 43, 1, 70, 44),
  806. array(6, 43, 19, 2, 44, 20),
  807. array(6, 43, 15, 2, 44, 16),
  808. // 11
  809. array(4, 101, 81),
  810. array(1, 80, 50, 4, 81, 51),
  811. array(4, 50, 22, 4, 51, 23),
  812. array(3, 36, 12, 8, 37, 13),
  813. // 12
  814. array(2, 116, 92, 2, 117, 93),
  815. array(6, 58, 36, 2, 59, 37),
  816. array(4, 46, 20, 6, 47, 21),
  817. array(7, 42, 14, 4, 43, 15),
  818. // 13
  819. array(4, 133, 107),
  820. array(8, 59, 37, 1, 60, 38),
  821. array(8, 44, 20, 4, 45, 21),
  822. array(12, 33, 11, 4, 34, 12),
  823. // 14
  824. array(3, 145, 115, 1, 146, 116),
  825. array(4, 64, 40, 5, 65, 41),
  826. array(11, 36, 16, 5, 37, 17),
  827. array(11, 36, 12, 5, 37, 13),
  828. // 15
  829. array(5, 109, 87, 1, 110, 88),
  830. array(5, 65, 41, 5, 66, 42),
  831. array(5, 54, 24, 7, 55, 25),
  832. array(11, 36, 12, 7, 37, 13),
  833. // 16
  834. array(5, 122, 98, 1, 123, 99),
  835. array(7, 73, 45, 3, 74, 46),
  836. array(15, 43, 19, 2, 44, 20),
  837. array(3, 45, 15, 13, 46, 16),
  838. // 17
  839. array(1, 135, 107, 5, 136, 108),
  840. array(10, 74, 46, 1, 75, 47),
  841. array(1, 50, 22, 15, 51, 23),
  842. array(2, 42, 14, 17, 43, 15),
  843. // 18
  844. array(5, 150, 120, 1, 151, 121),
  845. array(9, 69, 43, 4, 70, 44),
  846. array(17, 50, 22, 1, 51, 23),
  847. array(2, 42, 14, 19, 43, 15),
  848. // 19
  849. array(3, 141, 113, 4, 142, 114),
  850. array(3, 70, 44, 11, 71, 45),
  851. array(17, 47, 21, 4, 48, 22),
  852. array(9, 39, 13, 16, 40, 14),
  853. // 20
  854. array(3, 135, 107, 5, 136, 108),
  855. array(3, 67, 41, 13, 68, 42),
  856. array(15, 54, 24, 5, 55, 25),
  857. array(15, 43, 15, 10, 44, 16),
  858. // 21
  859. array(4, 144, 116, 4, 145, 117),
  860. array(17, 68, 42),
  861. array(17, 50, 22, 6, 51, 23),
  862. array(19, 46, 16, 6, 47, 17),
  863. // 22
  864. array(2, 139, 111, 7, 140, 112),
  865. array(17, 74, 46),
  866. array(7, 54, 24, 16, 55, 25),
  867. array(34, 37, 13),
  868. // 23
  869. array(4, 151, 121, 5, 152, 122),
  870. array(4, 75, 47, 14, 76, 48),
  871. array(11, 54, 24, 14, 55, 25),
  872. array(16, 45, 15, 14, 46, 16),
  873. // 24
  874. array(6, 147, 117, 4, 148, 118),
  875. array(6, 73, 45, 14, 74, 46),
  876. array(11, 54, 24, 16, 55, 25),
  877. array(30, 46, 16, 2, 47, 17),
  878. // 25
  879. array(8, 132, 106, 4, 133, 107),
  880. array(8, 75, 47, 13, 76, 48),
  881. array(7, 54, 24, 22, 55, 25),
  882. array(22, 45, 15, 13, 46, 16),
  883. // 26
  884. array(10, 142, 114, 2, 143, 115),
  885. array(19, 74, 46, 4, 75, 47),
  886. array(28, 50, 22, 6, 51, 23),
  887. array(33, 46, 16, 4, 47, 17),
  888. // 27
  889. array(8, 152, 122, 4, 153, 123),
  890. array(22, 73, 45, 3, 74, 46),
  891. array(8, 53, 23, 26, 54, 24),
  892. array(12, 45, 15, 28, 46, 16),
  893. // 28
  894. array(3, 147, 117, 10, 148, 118),
  895. array(3, 73, 45, 23, 74, 46),
  896. array(4, 54, 24, 31, 55, 25),
  897. array(11, 45, 15, 31, 46, 16),
  898. // 29
  899. array(7, 146, 116, 7, 147, 117),
  900. array(21, 73, 45, 7, 74, 46),
  901. array(1, 53, 23, 37, 54, 24),
  902. array(19, 45, 15, 26, 46, 16),
  903. // 30
  904. array(5, 145, 115, 10, 146, 116),
  905. array(19, 75, 47, 10, 76, 48),
  906. array(15, 54, 24, 25, 55, 25),
  907. array(23, 45, 15, 25, 46, 16),
  908. // 31
  909. array(13, 145, 115, 3, 146, 116),
  910. array(2, 74, 46, 29, 75, 47),
  911. array(42, 54, 24, 1, 55, 25),
  912. array(23, 45, 15, 28, 46, 16),
  913. // 32
  914. array(17, 145, 115),
  915. array(10, 74, 46, 23, 75, 47),
  916. array(10, 54, 24, 35, 55, 25),
  917. array(19, 45, 15, 35, 46, 16),
  918. // 33
  919. array(17, 145, 115, 1, 146, 116),
  920. array(14, 74, 46, 21, 75, 47),
  921. array(29, 54, 24, 19, 55, 25),
  922. array(11, 45, 15, 46, 46, 16),
  923. // 34
  924. array(13, 145, 115, 6, 146, 116),
  925. array(14, 74, 46, 23, 75, 47),
  926. array(44, 54, 24, 7, 55, 25),
  927. array(59, 46, 16, 1, 47, 17),
  928. // 35
  929. array(12, 151, 121, 7, 152, 122),
  930. array(12, 75, 47, 26, 76, 48),
  931. array(39, 54, 24, 14, 55, 25),
  932. array(22, 45, 15, 41, 46, 16),
  933. // 36
  934. array(6, 151, 121, 14, 152, 122),
  935. array(6, 75, 47, 34, 76, 48),
  936. array(46, 54, 24, 10, 55, 25),
  937. array(2, 45, 15, 64, 46, 16),
  938. // 37
  939. array(17, 152, 122, 4, 153, 123),
  940. array(29, 74, 46, 14, 75, 47),
  941. array(49, 54, 24, 10, 55, 25),
  942. array(24, 45, 15, 46, 46, 16),
  943. // 38
  944. array(4, 152, 122, 18, 153, 123),
  945. array(13, 74, 46, 32, 75, 47),
  946. array(48, 54, 24, 14, 55, 25),
  947. array(42, 45, 15, 32, 46, 16),
  948. // 39
  949. array(20, 147, 117, 4, 148, 118),
  950. array(40, 75, 47, 7, 76, 48),
  951. array(43, 54, 24, 22, 55, 25),
  952. array(10, 45, 15, 67, 46, 16),
  953. // 40
  954. array(19, 148, 118, 6, 149, 119),
  955. array(18, 75, 47, 31, 76, 48),
  956. array(34, 54, 24, 34, 55, 25),
  957. array(20, 45, 15, 61, 46, 16)
  958. );
  959. function __construct($totalCount, $dataCount)
  960. {
  961. $this->totalCount = $totalCount;
  962. $this->dataCount = $dataCount;
  963. }
  964. function getDataCount()
  965. {
  966. return $this->dataCount;
  967. }
  968. function getTotalCount()
  969. {
  970. return $this->totalCount;
  971. }
  972. static function getRSBlocks($typeNumber, $errorCorrectLevel)
  973. {
  974. $rsBlock = QRRSBlock::getRsBlockTable($typeNumber, $errorCorrectLevel);
  975. $length = count($rsBlock) / 3;
  976. $list = array();
  977. for ($i = 0; $i < $length; $i++) {
  978. $count = $rsBlock[$i * 3 + 0];
  979. $totalCount = $rsBlock[$i * 3 + 1];
  980. $dataCount = $rsBlock[$i * 3 + 2];
  981. for ($j = 0; $j < $count; $j++) {
  982. $list[] = new QRRSBlock($totalCount, $dataCount);
  983. }
  984. }
  985. return $list;
  986. }
  987. static function getRsBlockTable($typeNumber, $errorCorrectLevel)
  988. {
  989. switch ($errorCorrectLevel) {
  990. case QR_ERROR_CORRECT_LEVEL_L :
  991. return self::$QR_RS_BLOCK_TABLE[($typeNumber - 1) * 4 + 0];
  992. case QR_ERROR_CORRECT_LEVEL_M :
  993. return self::$QR_RS_BLOCK_TABLE[($typeNumber - 1) * 4 + 1];
  994. case QR_ERROR_CORRECT_LEVEL_Q :
  995. return self::$QR_RS_BLOCK_TABLE[($typeNumber - 1) * 4 + 2];
  996. case QR_ERROR_CORRECT_LEVEL_H :
  997. return self::$QR_RS_BLOCK_TABLE[($typeNumber - 1) * 4 + 3];
  998. default :
  999. trigger_error("tn:$typeNumber/ecl:$errorCorrectLevel", E_USER_ERROR);
  1000. }
  1001. }
  1002. }
  1003. //---------------------------------------------------------------
  1004. // QRNumber
  1005. //---------------------------------------------------------------
  1006. class QRNumber extends QRData
  1007. {
  1008. function __construct($data)
  1009. {
  1010. parent::__construct(QR_MODE_NUMBER, $data);
  1011. }
  1012. function write(&$buffer)
  1013. {
  1014. $data = $this->getData();
  1015. $i = 0;
  1016. while ($i + 2 < strlen($data)) {
  1017. $num = QRNumber::parseInt(substr($data, $i, 3));
  1018. $buffer->put($num, 10);
  1019. $i += 3;
  1020. }
  1021. if ($i < strlen($data)) {
  1022. if (strlen($data) - $i == 1) {
  1023. $num = QRNumber::parseInt(substr($data, $i, $i + 1));
  1024. $buffer->put($num, 4);
  1025. } else if (strlen($data) - $i == 2) {
  1026. $num = QRNumber::parseInt(substr($data, $i, $i + 2));
  1027. $buffer->put($num, 7);
  1028. }
  1029. }
  1030. }
  1031. static function parseInt($s)
  1032. {
  1033. $num = 0;
  1034. for ($i = 0; $i < strlen($s); $i++) {
  1035. $num = $num * 10 + QRNumber::parseIntAt(ord($s[$i]));
  1036. }
  1037. return $num;
  1038. }
  1039. static function parseIntAt($c)
  1040. {
  1041. if (QRUtil::toCharCode('0') <= $c && $c <= QRUtil::toCharCode('9')) {
  1042. return $c - QRUtil::toCharCode('0');
  1043. }
  1044. trigger_error("illegal char : $c", E_USER_ERROR);
  1045. }
  1046. }
  1047. //---------------------------------------------------------------
  1048. // QRKanji
  1049. //---------------------------------------------------------------
  1050. class QRKanji extends QRData
  1051. {
  1052. function __construct($data)
  1053. {
  1054. parent::__construct(QR_MODE_KANJI, $data);
  1055. }
  1056. function write(&$buffer)
  1057. {
  1058. $data = $this->getData();
  1059. $i = 0;
  1060. while ($i + 1 < strlen($data)) {
  1061. $c = ((0xff & ord($data[$i])) << 8) | (0xff & ord($data[$i + 1]));
  1062. if (0x8140 <= $c && $c <= 0x9FFC) {
  1063. $c -= 0x8140;
  1064. } else if (0xE040 <= $c && $c <= 0xEBBF) {
  1065. $c -= 0xC140;
  1066. } else {
  1067. trigger_error("illegal char at " . ($i + 1) . "/$c", E_USER_ERROR);
  1068. }
  1069. $c = (($c >> 8) & 0xff) * 0xC0 + ($c & 0xff);
  1070. $buffer->put($c, 13);
  1071. $i += 2;
  1072. }
  1073. if ($i < strlen($data)) {
  1074. trigger_error("illegal char at " . ($i + 1), E_USER_ERROR);
  1075. }
  1076. }
  1077. function getLength()
  1078. {
  1079. return floor(strlen($this->getData()) / 2);
  1080. }
  1081. }
  1082. //---------------------------------------------------------------
  1083. // QRAlphaNum
  1084. //---------------------------------------------------------------
  1085. class QRAlphaNum extends QRData
  1086. {
  1087. function __construct($data)
  1088. {
  1089. parent::__construct(QR_MODE_ALPHA_NUM, $data);
  1090. }
  1091. function write(&$buffer)
  1092. {
  1093. $i = 0;
  1094. $c = $this->getData();
  1095. while ($i + 1 < strlen($c)) {
  1096. $buffer->put(QRAlphaNum::getCode(ord($c[$i])) * 45
  1097. + QRAlphaNum::getCode(ord($c[$i + 1])), 11);
  1098. $i += 2;
  1099. }
  1100. if ($i < strlen($c)) {
  1101. $buffer->put(QRAlphaNum::getCode(ord($c[$i])), 6);
  1102. }
  1103. }
  1104. static function getCode($c)
  1105. {
  1106. if (QRUtil::toCharCode('0') <= $c
  1107. && $c <= QRUtil::toCharCode('9')) {
  1108. return $c - QRUtil::toCharCode('0');
  1109. } else if (QRUtil::toCharCode('A') <= $c
  1110. && $c <= QRUtil::toCharCode('Z')) {
  1111. return $c - QRUtil::toCharCode('A') + 10;
  1112. } else {
  1113. switch ($c) {
  1114. case QRUtil::toCharCode(' ') :
  1115. return 36;
  1116. case QRUtil::toCharCode('$') :
  1117. return 37;
  1118. case QRUtil::toCharCode('%') :
  1119. return 38;
  1120. case QRUtil::toCharCode('*') :
  1121. return 39;
  1122. case QRUtil::toCharCode('+') :
  1123. return 40;
  1124. case QRUtil::toCharCode('-') :
  1125. return 41;
  1126. case QRUtil::toCharCode('.') :
  1127. return 42;
  1128. case QRUtil::toCharCode('/') :
  1129. return 43;
  1130. case QRUtil::toCharCode(':') :
  1131. return 44;
  1132. default :
  1133. trigger_error("illegal char : $c", E_USER_ERROR);
  1134. }
  1135. }
  1136. }
  1137. }
  1138. //---------------------------------------------------------------
  1139. // QR8BitByte
  1140. //---------------------------------------------------------------
  1141. class QR8BitByte extends QRData
  1142. {
  1143. function __construct($data)
  1144. {
  1145. parent::__construct(QR_MODE_8BIT_BYTE, $data);
  1146. }
  1147. function write(&$buffer)
  1148. {
  1149. $data = $this->getData();
  1150. for ($i = 0; $i < strlen($data); $i++) {
  1151. $buffer->put(ord($data[$i]), 8);
  1152. }
  1153. }
  1154. }
  1155. //---------------------------------------------------------------
  1156. // QRData
  1157. //---------------------------------------------------------------
  1158. abstract class QRData
  1159. {
  1160. var $mode;
  1161. var $data;
  1162. function __construct($mode, $data)
  1163. {
  1164. $this->mode = $mode;
  1165. $this->data = $data;
  1166. }
  1167. function getMode()
  1168. {
  1169. return $this->mode;
  1170. }
  1171. function getData()
  1172. {
  1173. return $this->data;
  1174. }
  1175. /**
  1176. * @return int
  1177. */
  1178. function getLength()
  1179. {
  1180. return strlen($this->getData());
  1181. }
  1182. /**
  1183. * @param \QRBitBuffer $buffer
  1184. */
  1185. abstract function write(&$buffer);
  1186. function getLengthInBits($type)
  1187. {
  1188. if (1 <= $type && $type < 10) {
  1189. // 1 - 9
  1190. switch ($this->mode) {
  1191. case QR_MODE_NUMBER :
  1192. return 10;
  1193. case QR_MODE_ALPHA_NUM :
  1194. return 9;
  1195. case QR_MODE_8BIT_BYTE :
  1196. return 8;
  1197. case QR_MODE_KANJI :
  1198. return 8;
  1199. default :
  1200. trigger_error("mode:$this->mode", E_USER_ERROR);
  1201. }
  1202. } else if ($type < 27) {
  1203. // 10 - 26
  1204. switch ($this->mode) {
  1205. case QR_MODE_NUMBER :
  1206. return 12;
  1207. case QR_MODE_ALPHA_NUM :
  1208. return 11;
  1209. case QR_MODE_8BIT_BYTE :
  1210. return 16;
  1211. case QR_MODE_KANJI :
  1212. return 10;
  1213. default :
  1214. trigger_error("mode:$this->mode", E_USER_ERROR);
  1215. }
  1216. } else if ($type < 41) {
  1217. // 27 - 40
  1218. switch ($this->mode) {
  1219. case QR_MODE_NUMBER :
  1220. return 14;
  1221. case QR_MODE_ALPHA_NUM :
  1222. return 13;
  1223. case QR_MODE_8BIT_BYTE :
  1224. return 16;
  1225. case QR_MODE_KANJI :
  1226. return 12;
  1227. default :
  1228. trigger_error("mode:$this->mode", E_USER_ERROR);
  1229. }
  1230. } else {
  1231. trigger_error("mode:$this->mode", E_USER_ERROR);
  1232. }
  1233. }
  1234. }
  1235. //---------------------------------------------------------------
  1236. // QRMath
  1237. //---------------------------------------------------------------
  1238. class QRMath
  1239. {
  1240. static $QR_MATH_EXP_TABLE = null;
  1241. static $QR_MATH_LOG_TABLE = null;
  1242. static function init()
  1243. {
  1244. self::$QR_MATH_EXP_TABLE = QRMath::createNumArray(256);
  1245. for ($i = 0; $i < 8; $i++) {
  1246. self::$QR_MATH_EXP_TABLE[$i] = 1 << $i;
  1247. }
  1248. for ($i = 8; $i < 256; $i++) {
  1249. self::$QR_MATH_EXP_TABLE[$i] = self::$QR_MATH_EXP_TABLE[$i - 4]
  1250. ^ self::$QR_MATH_EXP_TABLE[$i - 5]
  1251. ^ self::$QR_MATH_EXP_TABLE[$i - 6]
  1252. ^ self::$QR_MATH_EXP_TABLE[$i - 8];
  1253. }
  1254. self::$QR_MATH_LOG_TABLE = QRMath::createNumArray(256);
  1255. for ($i = 0; $i < 255; $i++) {
  1256. self::$QR_MATH_LOG_TABLE[self::$QR_MATH_EXP_TABLE[$i]] = $i;
  1257. }
  1258. }
  1259. static function createNumArray($length)
  1260. {
  1261. $num_array = array();
  1262. for ($i = 0; $i < $length; $i++) {
  1263. $num_array[] = 0;
  1264. }
  1265. return $num_array;
  1266. }
  1267. static function glog($n)
  1268. {
  1269. if ($n < 1) {
  1270. trigger_error("log($n)", E_USER_ERROR);
  1271. }
  1272. return self::$QR_MATH_LOG_TABLE[$n];
  1273. }
  1274. static function gexp($n)
  1275. {
  1276. while ($n < 0) {
  1277. $n += 255;
  1278. }
  1279. while ($n >= 256) {
  1280. $n -= 255;
  1281. }
  1282. return self::$QR_MATH_EXP_TABLE[$n];
  1283. }
  1284. }
  1285. // init static table
  1286. QRMath::init();
  1287. //---------------------------------------------------------------
  1288. // QRPolynomial
  1289. //---------------------------------------------------------------
  1290. class QRPolynomial
  1291. {
  1292. var $num;
  1293. function __construct($num, $shift = 0)
  1294. {
  1295. $offset = 0;
  1296. while ($offset < count($num) && $num[$offset] == 0) {
  1297. $offset++;
  1298. }
  1299. $this->num = QRMath::createNumArray(count($num) - $offset + $shift);
  1300. for ($i = 0; $i < count($num) - $offset; $i++) {
  1301. $this->num[$i] = $num[$i + $offset];
  1302. }
  1303. }
  1304. function get($index)
  1305. {
  1306. return $this->num[$index];
  1307. }
  1308. function getLength()
  1309. {
  1310. return count($this->num);
  1311. }
  1312. // PHP5
  1313. function __toString()
  1314. {
  1315. return $this->toString();
  1316. }
  1317. function toString()
  1318. {
  1319. $buffer = "";
  1320. for ($i = 0; $i < $this->getLength(); $i++) {
  1321. if ($i > 0) {
  1322. $buffer .= ",";
  1323. }
  1324. $buffer .= $this->get($i);
  1325. }
  1326. return $buffer;
  1327. }
  1328. function toLogString()
  1329. {
  1330. $buffer = "";
  1331. for ($i = 0; $i < $this->getLength(); $i++) {
  1332. if ($i > 0) {
  1333. $buffer .= ",";
  1334. }
  1335. $buffer .= QRMath::glog($this->get($i));
  1336. }
  1337. return $buffer;
  1338. }
  1339. /**
  1340. * @param \QRPolynomial $e
  1341. *
  1342. * @return \QRPolynomial
  1343. */
  1344. function multiply($e)
  1345. {
  1346. $num = QRMath::createNumArray($this->getLength() + $e->getLength() - 1);
  1347. for ($i = 0; $i < $this->getLength(); $i++) {
  1348. $vi = QRMath::glog($this->get($i));
  1349. for ($j = 0; $j < $e->getLength(); $j++) {
  1350. $num[$i + $j] ^= QRMath::gexp($vi + QRMath::glog($e->get($j)));
  1351. }
  1352. }
  1353. return new QRPolynomial($num);
  1354. }
  1355. /**
  1356. * @param \QRPolynomial $e
  1357. *
  1358. * @return $this|\QRPolynomial
  1359. */
  1360. function mod($e)
  1361. {
  1362. if ($this->getLength() - $e->getLength() < 0) {
  1363. return $this;
  1364. }
  1365. $ratio = QRMath::glog($this->get(0)) - QRMath::glog($e->get(0));
  1366. $num = QRMath::createNumArray($this->getLength());
  1367. for ($i = 0; $i < $this->getLength(); $i++) {
  1368. $num[$i] = $this->get($i);
  1369. }
  1370. for ($i = 0; $i < $e->getLength(); $i++) {
  1371. $num[$i] ^= QRMath::gexp(QRMath::glog($e->get($i)) + $ratio);
  1372. }
  1373. $newPolynomial = new QRPolynomial($num);
  1374. return $newPolynomial->mod($e);
  1375. }
  1376. }
  1377. //---------------------------------------------------------------
  1378. // Mode
  1379. //---------------------------------------------------------------
  1380. define("QR_MODE_NUMBER", 1 << 0);
  1381. define("QR_MODE_ALPHA_NUM", 1 << 1);
  1382. define("QR_MODE_8BIT_BYTE", 1 << 2);
  1383. define("QR_MODE_KANJI", 1 << 3);
  1384. //---------------------------------------------------------------
  1385. // MaskPattern
  1386. //---------------------------------------------------------------
  1387. define("QR_MASK_PATTERN000", 0);
  1388. define("QR_MASK_PATTERN001", 1);
  1389. define("QR_MASK_PATTERN010", 2);
  1390. define("QR_MASK_PATTERN011", 3);
  1391. define("QR_MASK_PATTERN100", 4);
  1392. define("QR_MASK_PATTERN101", 5);
  1393. define("QR_MASK_PATTERN110", 6);
  1394. define("QR_MASK_PATTERN111", 7);
  1395. //---------------------------------------------------------------
  1396. // ErrorCorrectLevel
  1397. // 7%.
  1398. define("QR_ERROR_CORRECT_LEVEL_L", 1);
  1399. // 15%.
  1400. define("QR_ERROR_CORRECT_LEVEL_M", 0);
  1401. // 25%.
  1402. define("QR_ERROR_CORRECT_LEVEL_Q", 3);
  1403. // 30%.
  1404. define("QR_ERROR_CORRECT_LEVEL_H", 2);
  1405. //---------------------------------------------------------------
  1406. // QRBitBuffer
  1407. //---------------------------------------------------------------
  1408. class QRBitBuffer
  1409. {
  1410. var $buffer;
  1411. var $length;
  1412. function __construct()
  1413. {
  1414. $this->buffer = array();
  1415. $this->length = 0;
  1416. }
  1417. function getBuffer()
  1418. {
  1419. return $this->buffer;
  1420. }
  1421. function getLengthInBits()
  1422. {
  1423. return $this->length;
  1424. }
  1425. function __toString()
  1426. {
  1427. $buffer = "";
  1428. for ($i = 0; $i < $this->getLengthInBits(); $i++) {
  1429. $buffer .= $this->get($i) ? '1' : '0';
  1430. }
  1431. return $buffer;
  1432. }
  1433. function get($index)
  1434. {
  1435. $bufIndex = (int)floor($index / 8);
  1436. return (($this->buffer[$bufIndex] >> (7 - $index % 8)) & 1) == 1;
  1437. }
  1438. function put($num, $length)
  1439. {
  1440. for ($i = 0; $i < $length; $i++) {
  1441. $this->putBit((($num >> ($length - $i - 1)) & 1) == 1);
  1442. }
  1443. }
  1444. function putBit($bit)
  1445. {
  1446. $bufIndex = (int)floor($this->length / 8);
  1447. if (count($this->buffer) <= $bufIndex) {
  1448. $this->buffer[] = 0;
  1449. }
  1450. if ($bit) {
  1451. $this->buffer[$bufIndex] |= (0x80 >> ($this->length % 8));
  1452. }
  1453. $this->length++;
  1454. }
  1455. }