0
0

QRcode.php 116 KB

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