phpqrcode.php 108 KB

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