QRcode.php 116 KB

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