phpqrcode.php 66 KB

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