phpqrcode.php 128 KB

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