phpqrcode.php 119 KB

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