QRcode.php 118 KB

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