phpqrcode.php 128 KB

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