qrcode.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. <?php
  2. //============================================================+
  3. // File name : qrcode.php
  4. // Version : 1.0.010
  5. // Begin : 2010-03-22
  6. // Last Update : 2012-07-25
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2010-2012 Nicola Asuni - Tecnick.com LTD
  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.010
  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.010
  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. * @return (int)
  1314. */
  1315. protected function splitString() {
  1316. while (strlen($this->dataStr) > 0) {
  1317. $mode = $this->identifyMode(0);
  1318. switch ($mode) {
  1319. case QR_MODE_NM: {
  1320. $length = $this->eatNum();
  1321. break;
  1322. }
  1323. case QR_MODE_AN: {
  1324. $length = $this->eatAn();
  1325. break;
  1326. }
  1327. case QR_MODE_KJ: {
  1328. if ($hint == QR_MODE_KJ) {
  1329. $length = $this->eatKanji();
  1330. } else {
  1331. $length = $this->eat8();
  1332. }
  1333. break;
  1334. }
  1335. default: {
  1336. $length = $this->eat8();
  1337. break;
  1338. }
  1339. }
  1340. if ($length == 0) {
  1341. return 0;
  1342. }
  1343. if ($length < 0) {
  1344. return -1;
  1345. }
  1346. $this->dataStr = substr($this->dataStr, $length);
  1347. }
  1348. return 0;
  1349. }
  1350. /**
  1351. * toUpper
  1352. */
  1353. protected function toUpper() {
  1354. $stringLen = strlen($this->dataStr);
  1355. $p = 0;
  1356. while ($p < $stringLen) {
  1357. $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
  1358. if ($mode == QR_MODE_KJ) {
  1359. $p += 2;
  1360. } else {
  1361. if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
  1362. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1363. }
  1364. $p++;
  1365. }
  1366. }
  1367. return $this->dataStr;
  1368. }
  1369. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1370. // QRinputItem
  1371. /**
  1372. * newInputItem
  1373. * @param $mode (int)
  1374. * @param $size (int)
  1375. * @param $data (array)
  1376. * @param $bstream (array)
  1377. * @return array input item
  1378. */
  1379. protected function newInputItem($mode, $size, $data, $bstream=null) {
  1380. $setData = array_slice($data, 0, $size);
  1381. if (count($setData) < $size) {
  1382. $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
  1383. }
  1384. if (!$this->check($mode, $size, $setData)) {
  1385. return NULL;
  1386. }
  1387. $inputitem = array();
  1388. $inputitem['mode'] = $mode;
  1389. $inputitem['size'] = $size;
  1390. $inputitem['data'] = $setData;
  1391. $inputitem['bstream'] = $bstream;
  1392. return $inputitem;
  1393. }
  1394. /**
  1395. * encodeModeNum
  1396. * @param $inputitem (array)
  1397. * @param $version (int)
  1398. * @return array input item
  1399. */
  1400. protected function encodeModeNum($inputitem, $version) {
  1401. $words = (int)($inputitem['size'] / 3);
  1402. $inputitem['bstream'] = array();
  1403. $val = 0x1;
  1404. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1405. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
  1406. for ($i=0; $i < $words; ++$i) {
  1407. $val = (ord($inputitem['data'][$i*3 ]) - ord('0')) * 100;
  1408. $val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
  1409. $val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
  1410. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
  1411. }
  1412. if ($inputitem['size'] - $words * 3 == 1) {
  1413. $val = ord($inputitem['data'][$words*3]) - ord('0');
  1414. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
  1415. } elseif (($inputitem['size'] - ($words * 3)) == 2) {
  1416. $val = (ord($inputitem['data'][$words*3 ]) - ord('0')) * 10;
  1417. $val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
  1418. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
  1419. }
  1420. return $inputitem;
  1421. }
  1422. /**
  1423. * encodeModeAn
  1424. * @param $inputitem (array)
  1425. * @param $version (int)
  1426. * @return array input item
  1427. */
  1428. protected function encodeModeAn($inputitem, $version) {
  1429. $words = (int)($inputitem['size'] / 2);
  1430. $inputitem['bstream'] = array();
  1431. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
  1432. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
  1433. for ($i=0; $i < $words; ++$i) {
  1434. $val = (int)($this->lookAnTable(ord($inputitem['data'][$i*2])) * 45);
  1435. $val += (int)($this->lookAnTable(ord($inputitem['data'][($i*2)+1])));
  1436. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
  1437. }
  1438. if ($inputitem['size'] & 1) {
  1439. $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
  1440. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
  1441. }
  1442. return $inputitem;
  1443. }
  1444. /**
  1445. * encodeMode8
  1446. * @param $inputitem (array)
  1447. * @param $version (int)
  1448. * @return array input item
  1449. */
  1450. protected function encodeMode8($inputitem, $version) {
  1451. $inputitem['bstream'] = array();
  1452. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
  1453. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
  1454. for ($i=0; $i < $inputitem['size']; ++$i) {
  1455. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
  1456. }
  1457. return $inputitem;
  1458. }
  1459. /**
  1460. * encodeModeKanji
  1461. * @param $inputitem (array)
  1462. * @param $version (int)
  1463. * @return array input item
  1464. */
  1465. protected function encodeModeKanji($inputitem, $version) {
  1466. $inputitem['bstream'] = array();
  1467. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
  1468. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
  1469. for ($i=0; $i<$inputitem['size']; $i+=2) {
  1470. $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
  1471. if ($val <= 0x9ffc) {
  1472. $val -= 0x8140;
  1473. } else {
  1474. $val -= 0xc140;
  1475. }
  1476. $h = ($val >> 8) * 0xc0;
  1477. $val = ($val & 0xff) + $h;
  1478. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
  1479. }
  1480. return $inputitem;
  1481. }
  1482. /**
  1483. * encodeModeStructure
  1484. * @param $inputitem (array)
  1485. * @return array input item
  1486. */
  1487. protected function encodeModeStructure($inputitem) {
  1488. $inputitem['bstream'] = array();
  1489. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
  1490. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
  1491. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
  1492. $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
  1493. return $inputitem;
  1494. }
  1495. /**
  1496. * encodeBitStream
  1497. * @param $inputitem (array)
  1498. * @param $version (int)
  1499. * @return array input item
  1500. */
  1501. protected function encodeBitStream($inputitem, $version) {
  1502. $inputitem['bstream'] = array();
  1503. $words = $this->maximumWords($inputitem['mode'], $version);
  1504. if ($inputitem['size'] > $words) {
  1505. $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
  1506. $st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words));
  1507. $st1 = $this->encodeBitStream($st1, $version);
  1508. $st2 = $this->encodeBitStream($st2, $version);
  1509. $inputitem['bstream'] = array();
  1510. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
  1511. $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
  1512. } else {
  1513. switch($inputitem['mode']) {
  1514. case QR_MODE_NM: {
  1515. $inputitem = $this->encodeModeNum($inputitem, $version);
  1516. break;
  1517. }
  1518. case QR_MODE_AN: {
  1519. $inputitem = $this->encodeModeAn($inputitem, $version);
  1520. break;
  1521. }
  1522. case QR_MODE_8B: {
  1523. $inputitem = $this->encodeMode8($inputitem, $version);
  1524. break;
  1525. }
  1526. case QR_MODE_KJ: {
  1527. $inputitem = $this->encodeModeKanji($inputitem, $version);
  1528. break;
  1529. }
  1530. case QR_MODE_ST: {
  1531. $inputitem = $this->encodeModeStructure($inputitem);
  1532. break;
  1533. }
  1534. default: {
  1535. break;
  1536. }
  1537. }
  1538. }
  1539. return $inputitem;
  1540. }
  1541. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1542. // QRinput
  1543. /**
  1544. * Append data to an input object.
  1545. * The data is copied and appended to the input object.
  1546. * @param $items (arrray) input items
  1547. * @param $mode (int) encoding mode.
  1548. * @param $size (int) size of data (byte).
  1549. * @param $data (array) array of input data.
  1550. * @return items
  1551. *
  1552. */
  1553. protected function appendNewInputItem($items, $mode, $size, $data) {
  1554. $newitem = $this->newInputItem($mode, $size, $data);
  1555. if (!empty($newitem)) {
  1556. $items[] = $newitem;
  1557. }
  1558. return $items;
  1559. }
  1560. /**
  1561. * insertStructuredAppendHeader
  1562. * @param $items (array)
  1563. * @param $size (int)
  1564. * @param $index (int)
  1565. * @param $parity (int)
  1566. * @return array items
  1567. */
  1568. protected function insertStructuredAppendHeader($items, $size, $index, $parity) {
  1569. if ($size > MAX_STRUCTURED_SYMBOLS) {
  1570. return -1;
  1571. }
  1572. if (($index <= 0) OR ($index > MAX_STRUCTURED_SYMBOLS)) {
  1573. return -1;
  1574. }
  1575. $buf = array($size, $index, $parity);
  1576. $entry = $this->newInputItem(QR_MODE_ST, 3, buf);
  1577. array_unshift($items, $entry);
  1578. return $items;
  1579. }
  1580. /**
  1581. * calcParity
  1582. * @param $items (array)
  1583. * @return int parity
  1584. */
  1585. protected function calcParity($items) {
  1586. $parity = 0;
  1587. foreach ($items as $item) {
  1588. if ($item['mode'] != QR_MODE_ST) {
  1589. for ($i=$item['size']-1; $i>=0; --$i) {
  1590. $parity ^= $item['data'][$i];
  1591. }
  1592. }
  1593. }
  1594. return $parity;
  1595. }
  1596. /**
  1597. * checkModeNum
  1598. * @param $size (int)
  1599. * @param $data (array)
  1600. * @return boolean true or false
  1601. */
  1602. protected function checkModeNum($size, $data) {
  1603. for ($i=0; $i<$size; ++$i) {
  1604. if ((ord($data[$i]) < ord('0')) OR (ord($data[$i]) > ord('9'))){
  1605. return false;
  1606. }
  1607. }
  1608. return true;
  1609. }
  1610. /**
  1611. * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
  1612. * @param $c (int) character value
  1613. * @return value
  1614. */
  1615. protected function lookAnTable($c) {
  1616. return (($c > 127)?-1:$this->anTable[$c]);
  1617. }
  1618. /**
  1619. * checkModeAn
  1620. * @param $size (int)
  1621. * @param $data (array)
  1622. * @return boolean true or false
  1623. */
  1624. protected function checkModeAn($size, $data) {
  1625. for ($i=0; $i<$size; ++$i) {
  1626. if ($this->lookAnTable(ord($data[$i])) == -1) {
  1627. return false;
  1628. }
  1629. }
  1630. return true;
  1631. }
  1632. /**
  1633. * estimateBitsModeNum
  1634. * @param $size (int)
  1635. * @return int number of bits
  1636. */
  1637. protected function estimateBitsModeNum($size) {
  1638. $w = (int)($size / 3);
  1639. $bits = ($w * 10);
  1640. switch($size - ($w * 3)) {
  1641. case 1: {
  1642. $bits += 4;
  1643. break;
  1644. }
  1645. case 2: {
  1646. $bits += 7;
  1647. break;
  1648. }
  1649. }
  1650. return $bits;
  1651. }
  1652. /**
  1653. * estimateBitsModeAn
  1654. * @param $size (int)
  1655. * @return int number of bits
  1656. */
  1657. protected function estimateBitsModeAn($size) {
  1658. $bits = (int)($size * 5.5); // (size / 2 ) * 11
  1659. if ($size & 1) {
  1660. $bits += 6;
  1661. }
  1662. return $bits;
  1663. }
  1664. /**
  1665. * estimateBitsMode8
  1666. * @param $size (int)
  1667. * @return int number of bits
  1668. */
  1669. protected function estimateBitsMode8($size) {
  1670. return (int)($size * 8);
  1671. }
  1672. /**
  1673. * estimateBitsModeKanji
  1674. * @param $size (int)
  1675. * @return int number of bits
  1676. */
  1677. protected function estimateBitsModeKanji($size) {
  1678. return (int)($size * 6.5); // (size / 2 ) * 13
  1679. }
  1680. /**
  1681. * checkModeKanji
  1682. * @param $size (int)
  1683. * @param $data (array)
  1684. * @return boolean true or false
  1685. */
  1686. protected function checkModeKanji($size, $data) {
  1687. if ($size & 1) {
  1688. return false;
  1689. }
  1690. for ($i=0; $i<$size; $i+=2) {
  1691. $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
  1692. if (($val < 0x8140) OR (($val > 0x9ffc) AND ($val < 0xe040)) OR ($val > 0xebbf)) {
  1693. return false;
  1694. }
  1695. }
  1696. return true;
  1697. }
  1698. /**
  1699. * Validate the input data.
  1700. * @param $mode (int) encoding mode.
  1701. * @param $size (int) size of data (byte).
  1702. * @param $data (array) data to validate
  1703. * @return boolean true in case of valid data, false otherwise
  1704. */
  1705. protected function check($mode, $size, $data) {
  1706. if ($size <= 0) {
  1707. return false;
  1708. }
  1709. switch($mode) {
  1710. case QR_MODE_NM: {
  1711. return $this->checkModeNum($size, $data);
  1712. }
  1713. case QR_MODE_AN: {
  1714. return $this->checkModeAn($size, $data);
  1715. }
  1716. case QR_MODE_KJ: {
  1717. return $this->checkModeKanji($size, $data);
  1718. }
  1719. case QR_MODE_8B: {
  1720. return true;
  1721. }
  1722. case QR_MODE_ST: {
  1723. return true;
  1724. }
  1725. default: {
  1726. break;
  1727. }
  1728. }
  1729. return false;
  1730. }
  1731. /**
  1732. * estimateBitStreamSize
  1733. * @param $items (array)
  1734. * @param $version (int)
  1735. * @return int bits
  1736. */
  1737. protected function estimateBitStreamSize($items, $version) {
  1738. $bits = 0;
  1739. if ($version == 0) {
  1740. $version = 1;
  1741. }
  1742. foreach ($items as $item) {
  1743. switch($item['mode']) {
  1744. case QR_MODE_NM: {
  1745. $bits = $this->estimateBitsModeNum($item['size']);
  1746. break;
  1747. }
  1748. case QR_MODE_AN: {
  1749. $bits = $this->estimateBitsModeAn($item['size']);
  1750. break;
  1751. }
  1752. case QR_MODE_8B: {
  1753. $bits = $this->estimateBitsMode8($item['size']);
  1754. break;
  1755. }
  1756. case QR_MODE_KJ: {
  1757. $bits = $this->estimateBitsModeKanji($item['size']);
  1758. break;
  1759. }
  1760. case QR_MODE_ST: {
  1761. return STRUCTURE_HEADER_BITS;
  1762. }
  1763. default: {
  1764. return 0;
  1765. }
  1766. }
  1767. $l = $this->lengthIndicator($item['mode'], $version);
  1768. $m = 1 << $l;
  1769. $num = (int)(($item['size'] + $m - 1) / $m);
  1770. $bits += $num * (4 + $l);
  1771. }
  1772. return $bits;
  1773. }
  1774. /**
  1775. * estimateVersion
  1776. * @param $items (array)
  1777. * @return int version
  1778. */
  1779. protected function estimateVersion($items) {
  1780. $version = 0;
  1781. $prev = 0;
  1782. do {
  1783. $prev = $version;
  1784. $bits = $this->estimateBitStreamSize($items, $prev);
  1785. $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1786. if ($version < 0) {
  1787. return -1;
  1788. }
  1789. } while ($version > $prev);
  1790. return $version;
  1791. }
  1792. /**
  1793. * lengthOfCode
  1794. * @param $mode (int)
  1795. * @param $version (int)
  1796. * @param $bits (int)
  1797. * @return int size
  1798. */
  1799. protected function lengthOfCode($mode, $version, $bits) {
  1800. $payload = $bits - 4 - $this->lengthIndicator($mode, $version);
  1801. switch($mode) {
  1802. case QR_MODE_NM: {
  1803. $chunks = (int)($payload / 10);
  1804. $remain = $payload - $chunks * 10;
  1805. $size = $chunks * 3;
  1806. if ($remain >= 7) {
  1807. $size += 2;
  1808. } elseif ($remain >= 4) {
  1809. $size += 1;
  1810. }
  1811. break;
  1812. }
  1813. case QR_MODE_AN: {
  1814. $chunks = (int)($payload / 11);
  1815. $remain = $payload - $chunks * 11;
  1816. $size = $chunks * 2;
  1817. if ($remain >= 6) {
  1818. ++$size;
  1819. }
  1820. break;
  1821. }
  1822. case QR_MODE_8B: {
  1823. $size = (int)($payload / 8);
  1824. break;
  1825. }
  1826. case QR_MODE_KJ: {
  1827. $size = (int)(($payload / 13) * 2);
  1828. break;
  1829. }
  1830. case QR_MODE_ST: {
  1831. $size = (int)($payload / 8);
  1832. break;
  1833. }
  1834. default: {
  1835. $size = 0;
  1836. break;
  1837. }
  1838. }
  1839. $maxsize = $this->maximumWords($mode, $version);
  1840. if ($size < 0) {
  1841. $size = 0;
  1842. }
  1843. if ($size > $maxsize) {
  1844. $size = $maxsize;
  1845. }
  1846. return $size;
  1847. }
  1848. /**
  1849. * createBitStream
  1850. * @param $items (array)
  1851. * @return array of items and total bits
  1852. */
  1853. protected function createBitStream($items) {
  1854. $total = 0;
  1855. foreach ($items as $key => $item) {
  1856. $items[$key] = $this->encodeBitStream($item, $this->version);
  1857. $bits = count($items[$key]['bstream']);
  1858. $total += $bits;
  1859. }
  1860. return array($items, $total);
  1861. }
  1862. /**
  1863. * convertData
  1864. * @param $items (array)
  1865. * @return array items
  1866. */
  1867. protected function convertData($items) {
  1868. $ver = $this->estimateVersion($items);
  1869. if ($ver > $this->version) {
  1870. $this->version = $ver;
  1871. }
  1872. while (true) {
  1873. $cbs = $this->createBitStream($items);
  1874. $items = $cbs[0];
  1875. $bits = $cbs[1];
  1876. if ($bits < 0) {
  1877. return -1;
  1878. }
  1879. $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  1880. if ($ver < 0) {
  1881. return -1;
  1882. } elseif ($ver > $this->version) {
  1883. $this->version = $ver;
  1884. } else {
  1885. break;
  1886. }
  1887. }
  1888. return $items;
  1889. }
  1890. /**
  1891. * Append Padding Bit to bitstream
  1892. * @param $bstream (array)
  1893. * @return array bitstream
  1894. */
  1895. protected function appendPaddingBit($bstream) {
  1896. if (is_null($bstream)) {
  1897. return null;
  1898. }
  1899. $bits = count($bstream);
  1900. $maxwords = $this->getDataLength($this->version, $this->level);
  1901. $maxbits = $maxwords * 8;
  1902. if ($maxbits == $bits) {
  1903. return $bstream;
  1904. }
  1905. if ($maxbits - $bits < 5) {
  1906. return $this->appendNum($bstream, $maxbits - $bits, 0);
  1907. }
  1908. $bits += 4;
  1909. $words = (int)(($bits + 7) / 8);
  1910. $padding = array();
  1911. $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
  1912. $padlen = $maxwords - $words;
  1913. if ($padlen > 0) {
  1914. $padbuf = array();
  1915. for ($i=0; $i<$padlen; ++$i) {
  1916. $padbuf[$i] = ($i&1)?0x11:0xec;
  1917. }
  1918. $padding = $this->appendBytes($padding, $padlen, $padbuf);
  1919. }
  1920. return $this->appendBitstream($bstream, $padding);
  1921. }
  1922. /**
  1923. * mergeBitStream
  1924. * @param $items (array) items
  1925. * @return array bitstream
  1926. */
  1927. protected function mergeBitStream($items) {
  1928. $items = $this->convertData($items);
  1929. if (!is_array($items)) {
  1930. return null;
  1931. }
  1932. $bstream = array();
  1933. foreach ($items as $item) {
  1934. $bstream = $this->appendBitstream($bstream, $item['bstream']);
  1935. }
  1936. return $bstream;
  1937. }
  1938. /**
  1939. * Returns a stream of bits.
  1940. * @param $items (int)
  1941. * @return array padded merged byte stream
  1942. */
  1943. protected function getBitStream($items) {
  1944. $bstream = $this->mergeBitStream($items);
  1945. return $this->appendPaddingBit($bstream);
  1946. }
  1947. /**
  1948. * Pack all bit streams padding bits into a byte array.
  1949. * @param $items (int)
  1950. * @return array padded merged byte stream
  1951. */
  1952. protected function getByteStream($items) {
  1953. $bstream = $this->getBitStream($items);
  1954. return $this->bitstreamToByte($bstream);
  1955. }
  1956. // - - - - - - - - - - - - - - - - - - - - - - - - -
  1957. // QRbitstream
  1958. /**
  1959. * Return an array with zeros
  1960. * @param $setLength (int) array size
  1961. * @return array
  1962. */
  1963. protected function allocate($setLength) {
  1964. return array_fill(0, $setLength, 0);
  1965. }
  1966. /**
  1967. * Return new bitstream from number
  1968. * @param $bits (int) number of bits
  1969. * @param $num (int) number
  1970. * @return array bitstream
  1971. */
  1972. protected function newFromNum($bits, $num) {
  1973. $bstream = $this->allocate($bits);
  1974. $mask = 1 << ($bits - 1);
  1975. for ($i=0; $i<$bits; ++$i) {
  1976. if ($num & $mask) {
  1977. $bstream[$i] = 1;
  1978. } else {
  1979. $bstream[$i] = 0;
  1980. }
  1981. $mask = $mask >> 1;
  1982. }
  1983. return $bstream;
  1984. }
  1985. /**
  1986. * Return new bitstream from bytes
  1987. * @param $size (int) size
  1988. * @param $data (array) bytes
  1989. * @return array bitstream
  1990. */
  1991. protected function newFromBytes($size, $data) {
  1992. $bstream = $this->allocate($size * 8);
  1993. $p=0;
  1994. for ($i=0; $i<$size; ++$i) {
  1995. $mask = 0x80;
  1996. for ($j=0; $j<8; ++$j) {
  1997. if ($data[$i] & $mask) {
  1998. $bstream[$p] = 1;
  1999. } else {
  2000. $bstream[$p] = 0;
  2001. }
  2002. $p++;
  2003. $mask = $mask >> 1;
  2004. }
  2005. }
  2006. return $bstream;
  2007. }
  2008. /**
  2009. * Append one bitstream to another
  2010. * @param $bitstream (array) original bitstream
  2011. * @param $append (array) bitstream to append
  2012. * @return array bitstream
  2013. */
  2014. protected function appendBitstream($bitstream, $append) {
  2015. if ((!is_array($append)) OR (count($append) == 0)) {
  2016. return $bitstream;
  2017. }
  2018. if (count($bitstream) == 0) {
  2019. return $append;
  2020. }
  2021. return array_values(array_merge($bitstream, $append));
  2022. }
  2023. /**
  2024. * Append one bitstream created from number to another
  2025. * @param $bitstream (array) original bitstream
  2026. * @param $bits (int) number of bits
  2027. * @param $num (int) number
  2028. * @return array bitstream
  2029. */
  2030. protected function appendNum($bitstream, $bits, $num) {
  2031. if ($bits == 0) {
  2032. return 0;
  2033. }
  2034. $b = $this->newFromNum($bits, $num);
  2035. return $this->appendBitstream($bitstream, $b);
  2036. }
  2037. /**
  2038. * Append one bitstream created from bytes to another
  2039. * @param $bitstream (array) original bitstream
  2040. * @param $size (int) size
  2041. * @param $data (array) bytes
  2042. * @return array bitstream
  2043. */
  2044. protected function appendBytes($bitstream, $size, $data) {
  2045. if ($size == 0) {
  2046. return 0;
  2047. }
  2048. $b = $this->newFromBytes($size, $data);
  2049. return $this->appendBitstream($bitstream, $b);
  2050. }
  2051. /**
  2052. * Convert bitstream to bytes
  2053. * @param $bstream (array) original bitstream
  2054. * @return array of bytes
  2055. */
  2056. protected function bitstreamToByte($bstream) {
  2057. if (is_null($bstream)) {
  2058. return null;
  2059. }
  2060. $size = count($bstream);
  2061. if ($size == 0) {
  2062. return array();
  2063. }
  2064. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  2065. $bytes = (int)($size / 8);
  2066. $p = 0;
  2067. for ($i=0; $i<$bytes; $i++) {
  2068. $v = 0;
  2069. for ($j=0; $j<8; $j++) {
  2070. $v = $v << 1;
  2071. $v |= $bstream[$p];
  2072. $p++;
  2073. }
  2074. $data[$i] = $v;
  2075. }
  2076. if ($size & 7) {
  2077. $v = 0;
  2078. for ($j=0; $j<($size & 7); $j++) {
  2079. $v = $v << 1;
  2080. $v |= $bstream[$p];
  2081. $p++;
  2082. }
  2083. $data[$bytes] = $v;
  2084. }
  2085. return $data;
  2086. }
  2087. // - - - - - - - - - - - - - - - - - - - - - - - - -
  2088. // QRspec
  2089. /**
  2090. * Replace a value on the array at the specified position
  2091. * @param $srctab (array)
  2092. * @param $x (int) X position
  2093. * @param $y (int) Y position
  2094. * @param $repl (string) value to replace
  2095. * @param $replLen (int) length of the repl string
  2096. * @return array srctab
  2097. */
  2098. protected function qrstrset($srctab, $x, $y, $repl, $replLen=false) {
  2099. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  2100. return $srctab;
  2101. }
  2102. /**
  2103. * Return maximum data code length (bytes) for the version.
  2104. * @param $version (int) version
  2105. * @param $level (int) error correction level
  2106. * @return int maximum size (bytes)
  2107. */
  2108. protected function getDataLength($version, $level) {
  2109. return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level];
  2110. }
  2111. /**
  2112. * Return maximum error correction code length (bytes) for the version.
  2113. * @param $version (int) version
  2114. * @param $level (int) error correction level
  2115. * @return int ECC size (bytes)
  2116. */
  2117. protected function getECCLength($version, $level){
  2118. return $this->capacity[$version][QRCAP_EC][$level];
  2119. }
  2120. /**
  2121. * Return the width of the symbol for the version.
  2122. * @param $version (int) version
  2123. * @return int width
  2124. */
  2125. protected function getWidth($version) {
  2126. return $this->capacity[$version][QRCAP_WIDTH];
  2127. }
  2128. /**
  2129. * Return the numer of remainder bits.
  2130. * @param $version (int) version
  2131. * @return int number of remainder bits
  2132. */
  2133. protected function getRemainder($version) {
  2134. return $this->capacity[$version][QRCAP_REMINDER];
  2135. }
  2136. /**
  2137. * Return a version number that satisfies the input code length.
  2138. * @param $size (int) input code length (bytes)
  2139. * @param $level (int) error correction level
  2140. * @return int version number
  2141. */
  2142. protected function getMinimumVersion($size, $level) {
  2143. for ($i = 1; $i <= QRSPEC_VERSION_MAX; ++$i) {
  2144. $words = ($this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level]);
  2145. if ($words >= $size) {
  2146. return $i;
  2147. }
  2148. }
  2149. // the size of input data is greater than QR capacity, try to lover the error correction mode
  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. //============================================================+