qrcode.php 77 KB

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