phpqrcode.php 104 KB

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