QRcode.php 115 KB

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