qrcode.php 78 KB

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