barcodes.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. <?php
  2. //============================================================+
  3. // File name : barcodes.php
  4. // Version : 1.0.021
  5. // Begin : 2008-06-09
  6. // Last Update : 2011-09-15
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2008-2011 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // Description : PHP class to creates array representations for
  31. // common 1D barcodes to be used with TCPDF.
  32. //
  33. //============================================================+
  34. /**
  35. * @file
  36. * PHP class to creates array representations for common 1D barcodes to be used with TCPDF.
  37. * @package com.tecnick.tcpdf
  38. * @author Nicola Asuni
  39. * @version 1.0.021
  40. */
  41. /**
  42. * @class TCPDFBarcode
  43. * PHP class to creates array representations for common 1D barcodes to be used with TCPDF (http://www.tcpdf.org).<br>
  44. * @package com.tecnick.tcpdf
  45. * @version 1.0.021
  46. * @author Nicola Asuni
  47. */
  48. class TCPDFBarcode {
  49. /**
  50. * Array representation of barcode.
  51. * @protected
  52. */
  53. protected $barcode_array;
  54. /**
  55. * This is the class constructor.
  56. * Return an array representations for common 1D barcodes:<ul>
  57. * <li>$arrcode['code'] code to be printed on text label</li>
  58. * <li>$arrcode['maxh'] max barcode height</li>
  59. * <li>$arrcode['maxw'] max barcode width</li>
  60. * <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
  61. * <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
  62. * <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
  63. * <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
  64. * <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
  65. * @param $code (string) code to print
  66. * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
  67. * @public
  68. */
  69. public function __construct($code, $type) {
  70. $this->setBarcode($code, $type);
  71. }
  72. /**
  73. * Return an array representations of barcode.
  74. * @return array
  75. * @public
  76. */
  77. public function getBarcodeArray() {
  78. return $this->barcode_array;
  79. }
  80. /**
  81. * Send barcode as SVG image object to the standard output.
  82. * @param $w (int) Minimum width of a single bar in user units.
  83. * @param $h (int) Height of barcode in user units.
  84. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  85. * @public
  86. */
  87. public function getBarcodeSVG($w=2, $h=30, $color='black') {
  88. // send headers
  89. $code = $this->getBarcodeSVGcode($w, $h, $color);
  90. header('Content-Type: application/svg+xml');
  91. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  92. header('Pragma: public');
  93. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  94. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  95. header('Content-Disposition: inline; filename="'.md5($code).'.svg";');
  96. //header('Content-Length: '.strlen($code));
  97. echo $code;
  98. }
  99. /**
  100. * Return a SVG string representation of barcode.
  101. * @param $w (int) Minimum width of a single bar in user units.
  102. * @param $h (int) Height of barcode in user units.
  103. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  104. * @return string SVG code.
  105. * @public
  106. */
  107. public function getBarcodeSVGcode($w=2, $h=30, $color='black') {
  108. // replace table for special characters
  109. $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  110. $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
  111. $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
  112. $svg .= '<svg width="'.round(($this->barcode_array['maxw'] * $w), 3).'" height="'.$h.'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n";
  113. $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
  114. $svg .= "\t".'<g id="bars" fill="'.$color.'" stroke="none">'."\n";
  115. // print bars
  116. $x = 0;
  117. foreach ($this->barcode_array['bcode'] as $k => $v) {
  118. $bw = round(($v['w'] * $w), 3);
  119. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  120. if ($v['t']) {
  121. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  122. // draw a vertical bar
  123. $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$bw.'" height="'.$bh.'" />'."\n";
  124. }
  125. $x += $bw;
  126. }
  127. $svg .= "\t".'</g>'."\n";
  128. $svg .= '</svg>'."\n";
  129. return $svg;
  130. }
  131. /**
  132. * Return an HTML representation of barcode.
  133. * @param $w (int) Width of a single bar element in pixels.
  134. * @param $h (int) Height of a single bar element in pixels.
  135. * @param $color (string) Foreground color for bar elements (background is transparent).
  136. * @return string HTML code.
  137. * @public
  138. */
  139. public function getBarcodeHTML($w=2, $h=30, $color='black') {
  140. // replace table for special characters
  141. $html = '<div style="font-size:0;position:relative;">'."\n";
  142. // print bars
  143. $x = 0;
  144. foreach ($this->barcode_array['bcode'] as $k => $v) {
  145. $bw = round(($v['w'] * $w), 3);
  146. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  147. if ($v['t']) {
  148. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  149. // draw a vertical bar
  150. $html .= '<div style="background-color:'.$color.';width:'.$bw.'px;height:'.$bh.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;">&nbsp;</div>'."\n";
  151. }
  152. $x += $bw;
  153. }
  154. $html .= '</div>'."\n";
  155. return $html;
  156. }
  157. /**
  158. * Return a PNG image representation of barcode (requires GD or Imagick library).
  159. * @param $w (int) Width of a single bar element in pixels.
  160. * @param $h (int) Height of a single bar element in pixels.
  161. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  162. * @return image or false in case of error.
  163. * @public
  164. */
  165. public function getBarcodePNG($w=2, $h=30, $color=array(0,0,0)) {
  166. // calculate image size
  167. $width = ($this->barcode_array['maxw'] * $w);
  168. $height = $h;
  169. if (function_exists('imagecreate')) {
  170. // GD library
  171. $imagick = false;
  172. $png = imagecreate($width, $height);
  173. $bgcol = imagecolorallocate($png, 255, 255, 255);
  174. imagecolortransparent($png, $bgcol);
  175. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  176. } elseif (extension_loaded('imagick')) {
  177. $imagick = true;
  178. $bgcol = new imagickpixel('rgb(255,255,255');
  179. $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
  180. $png = new Imagick();
  181. $png->newImage($width, $height, 'none', 'png');
  182. $bar = new imagickdraw();
  183. $bar->setfillcolor($fgcol);
  184. } else {
  185. return false;
  186. }
  187. // print bars
  188. $x = 0;
  189. foreach ($this->barcode_array['bcode'] as $k => $v) {
  190. $bw = round(($v['w'] * $w), 3);
  191. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  192. if ($v['t']) {
  193. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  194. // draw a vertical bar
  195. if ($imagick) {
  196. $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
  197. } else {
  198. imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
  199. }
  200. }
  201. $x += $bw;
  202. }
  203. // send headers
  204. header('Content-Type: image/png');
  205. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  206. header('Pragma: public');
  207. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  208. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  209. if ($imagick) {
  210. $png->drawimage($bar);
  211. echo $png;
  212. } else {
  213. imagepng($png);
  214. imagedestroy($png);
  215. }
  216. }
  217. /**
  218. * Set the barcode.
  219. * @param $code (string) code to print
  220. * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
  221. * @return array barcode array
  222. * @public
  223. */
  224. public function setBarcode($code, $type) {
  225. switch (strtoupper($type)) {
  226. case 'C39': { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  227. $arrcode = $this->barcode_code39($code, false, false);
  228. break;
  229. }
  230. case 'C39+': { // CODE 39 with checksum
  231. $arrcode = $this->barcode_code39($code, false, true);
  232. break;
  233. }
  234. case 'C39E': { // CODE 39 EXTENDED
  235. $arrcode = $this->barcode_code39($code, true, false);
  236. break;
  237. }
  238. case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
  239. $arrcode = $this->barcode_code39($code, true, true);
  240. break;
  241. }
  242. case 'C93': { // CODE 93 - USS-93
  243. $arrcode = $this->barcode_code93($code);
  244. break;
  245. }
  246. case 'S25': { // Standard 2 of 5
  247. $arrcode = $this->barcode_s25($code, false);
  248. break;
  249. }
  250. case 'S25+': { // Standard 2 of 5 + CHECKSUM
  251. $arrcode = $this->barcode_s25($code, true);
  252. break;
  253. }
  254. case 'I25': { // Interleaved 2 of 5
  255. $arrcode = $this->barcode_i25($code, false);
  256. break;
  257. }
  258. case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
  259. $arrcode = $this->barcode_i25($code, true);
  260. break;
  261. }
  262. case 'C128': { // CODE 128
  263. $arrcode = $this->barcode_c128($code, '');
  264. break;
  265. }
  266. case 'C128A': { // CODE 128 A
  267. $arrcode = $this->barcode_c128($code, 'A');
  268. break;
  269. }
  270. case 'C128B': { // CODE 128 B
  271. $arrcode = $this->barcode_c128($code, 'B');
  272. break;
  273. }
  274. case 'C128C': { // CODE 128 C
  275. $arrcode = $this->barcode_c128($code, 'C');
  276. break;
  277. }
  278. case 'EAN2': { // 2-Digits UPC-Based Extention
  279. $arrcode = $this->barcode_eanext($code, 2);
  280. break;
  281. }
  282. case 'EAN5': { // 5-Digits UPC-Based Extention
  283. $arrcode = $this->barcode_eanext($code, 5);
  284. break;
  285. }
  286. case 'EAN8': { // EAN 8
  287. $arrcode = $this->barcode_eanupc($code, 8);
  288. break;
  289. }
  290. case 'EAN13': { // EAN 13
  291. $arrcode = $this->barcode_eanupc($code, 13);
  292. break;
  293. }
  294. case 'UPCA': { // UPC-A
  295. $arrcode = $this->barcode_eanupc($code, 12);
  296. break;
  297. }
  298. case 'UPCE': { // UPC-E
  299. $arrcode = $this->barcode_eanupc($code, 6);
  300. break;
  301. }
  302. case 'MSI': { // MSI (Variation of Plessey code)
  303. $arrcode = $this->barcode_msi($code, false);
  304. break;
  305. }
  306. case 'MSI+': { // MSI + CHECKSUM (modulo 11)
  307. $arrcode = $this->barcode_msi($code, true);
  308. break;
  309. }
  310. case 'POSTNET': { // POSTNET
  311. $arrcode = $this->barcode_postnet($code, false);
  312. break;
  313. }
  314. case 'PLANET': { // PLANET
  315. $arrcode = $this->barcode_postnet($code, true);
  316. break;
  317. }
  318. case 'RMS4CC': { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
  319. $arrcode = $this->barcode_rms4cc($code, false);
  320. break;
  321. }
  322. case 'KIX': { // KIX (Klant index - Customer index)
  323. $arrcode = $this->barcode_rms4cc($code, true);
  324. break;
  325. }
  326. case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  327. $arrcode = $this->barcode_imb($code);
  328. break;
  329. }
  330. case 'CODABAR': { // CODABAR
  331. $arrcode = $this->barcode_codabar($code);
  332. break;
  333. }
  334. case 'CODE11': { // CODE 11
  335. $arrcode = $this->barcode_code11($code);
  336. break;
  337. }
  338. case 'PHARMA': { // PHARMACODE
  339. $arrcode = $this->barcode_pharmacode($code);
  340. break;
  341. }
  342. case 'PHARMA2T': { // PHARMACODE TWO-TRACKS
  343. $arrcode = $this->barcode_pharmacode2t($code);
  344. break;
  345. }
  346. default: {
  347. $this->barcode_array = false;
  348. $arrcode = false;
  349. break;
  350. }
  351. }
  352. $this->barcode_array = $arrcode;
  353. }
  354. /**
  355. * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  356. * General-purpose code in very wide use world-wide
  357. * @param $code (string) code to represent.
  358. * @param $extended (boolean) if true uses the extended mode.
  359. * @param $checksum (boolean) if true add a checksum to the code.
  360. * @return array barcode representation.
  361. * @protected
  362. */
  363. protected function barcode_code39($code, $extended=false, $checksum=false) {
  364. $chr['0'] = '111331311';
  365. $chr['1'] = '311311113';
  366. $chr['2'] = '113311113';
  367. $chr['3'] = '313311111';
  368. $chr['4'] = '111331113';
  369. $chr['5'] = '311331111';
  370. $chr['6'] = '113331111';
  371. $chr['7'] = '111311313';
  372. $chr['8'] = '311311311';
  373. $chr['9'] = '113311311';
  374. $chr['A'] = '311113113';
  375. $chr['B'] = '113113113';
  376. $chr['C'] = '313113111';
  377. $chr['D'] = '111133113';
  378. $chr['E'] = '311133111';
  379. $chr['F'] = '113133111';
  380. $chr['G'] = '111113313';
  381. $chr['H'] = '311113311';
  382. $chr['I'] = '113113311';
  383. $chr['J'] = '111133311';
  384. $chr['K'] = '311111133';
  385. $chr['L'] = '113111133';
  386. $chr['M'] = '313111131';
  387. $chr['N'] = '111131133';
  388. $chr['O'] = '311131131';
  389. $chr['P'] = '113131131';
  390. $chr['Q'] = '111111333';
  391. $chr['R'] = '311111331';
  392. $chr['S'] = '113111331';
  393. $chr['T'] = '111131331';
  394. $chr['U'] = '331111113';
  395. $chr['V'] = '133111113';
  396. $chr['W'] = '333111111';
  397. $chr['X'] = '131131113';
  398. $chr['Y'] = '331131111';
  399. $chr['Z'] = '133131111';
  400. $chr['-'] = '131111313';
  401. $chr['.'] = '331111311';
  402. $chr[' '] = '133111311';
  403. $chr['$'] = '131313111';
  404. $chr['/'] = '131311131';
  405. $chr['+'] = '131113131';
  406. $chr['%'] = '111313131';
  407. $chr['*'] = '131131311';
  408. $code = strtoupper($code);
  409. if ($extended) {
  410. // extended mode
  411. $code = $this->encode_code39_ext($code);
  412. }
  413. if ($code === false) {
  414. return false;
  415. }
  416. if ($checksum) {
  417. // checksum
  418. $code .= $this->checksum_code39($code);
  419. }
  420. // add start and stop codes
  421. $code = '*'.$code.'*';
  422. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  423. $k = 0;
  424. $clen = strlen($code);
  425. for ($i = 0; $i < $clen; ++$i) {
  426. $char = $code{$i};
  427. if(!isset($chr[$char])) {
  428. // invalid character
  429. return false;
  430. }
  431. for ($j = 0; $j < 9; ++$j) {
  432. if (($j % 2) == 0) {
  433. $t = true; // bar
  434. } else {
  435. $t = false; // space
  436. }
  437. $w = $chr[$char]{$j};
  438. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  439. $bararray['maxw'] += $w;
  440. ++$k;
  441. }
  442. // intercharacter gap
  443. $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
  444. $bararray['maxw'] += 1;
  445. ++$k;
  446. }
  447. return $bararray;
  448. }
  449. /**
  450. * Encode a string to be used for CODE 39 Extended mode.
  451. * @param $code (string) code to represent.
  452. * @return encoded string.
  453. * @protected
  454. */
  455. protected function encode_code39_ext($code) {
  456. $encode = array(
  457. chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
  458. chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
  459. chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
  460. chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
  461. chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
  462. chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
  463. chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
  464. chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
  465. chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
  466. chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
  467. chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
  468. chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
  469. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  470. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  471. chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
  472. chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
  473. chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  474. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  475. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  476. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  477. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  478. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  479. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
  480. chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
  481. chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
  482. chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
  483. chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
  484. chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
  485. chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
  486. chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
  487. chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
  488. chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
  489. $code_ext = '';
  490. $clen = strlen($code);
  491. for ($i = 0 ; $i < $clen; ++$i) {
  492. if (ord($code{$i}) > 127) {
  493. return false;
  494. }
  495. $code_ext .= $encode[$code{$i}];
  496. }
  497. return $code_ext;
  498. }
  499. /**
  500. * Calculate CODE 39 checksum (modulo 43).
  501. * @param $code (string) code to represent.
  502. * @return char checksum.
  503. * @protected
  504. */
  505. protected function checksum_code39($code) {
  506. $chars = array(
  507. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  508. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  509. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  510. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
  511. $sum = 0;
  512. $clen = strlen($code);
  513. for ($i = 0 ; $i < $clen; ++$i) {
  514. $k = array_keys($chars, $code{$i});
  515. $sum += $k[0];
  516. }
  517. $j = ($sum % 43);
  518. return $chars[$j];
  519. }
  520. /**
  521. * CODE 93 - USS-93
  522. * Compact code similar to Code 39
  523. * @param $code (string) code to represent.
  524. * @return array barcode representation.
  525. * @protected
  526. */
  527. protected function barcode_code93($code) {
  528. $chr[48] = '131112'; // 0
  529. $chr[49] = '111213'; // 1
  530. $chr[50] = '111312'; // 2
  531. $chr[51] = '111411'; // 3
  532. $chr[52] = '121113'; // 4
  533. $chr[53] = '121212'; // 5
  534. $chr[54] = '121311'; // 6
  535. $chr[55] = '111114'; // 7
  536. $chr[56] = '131211'; // 8
  537. $chr[57] = '141111'; // 9
  538. $chr[65] = '211113'; // A
  539. $chr[66] = '211212'; // B
  540. $chr[67] = '211311'; // C
  541. $chr[68] = '221112'; // D
  542. $chr[69] = '221211'; // E
  543. $chr[70] = '231111'; // F
  544. $chr[71] = '112113'; // G
  545. $chr[72] = '112212'; // H
  546. $chr[73] = '112311'; // I
  547. $chr[74] = '122112'; // J
  548. $chr[75] = '132111'; // K
  549. $chr[76] = '111123'; // L
  550. $chr[77] = '111222'; // M
  551. $chr[78] = '111321'; // N
  552. $chr[79] = '121122'; // O
  553. $chr[80] = '131121'; // P
  554. $chr[81] = '212112'; // Q
  555. $chr[82] = '212211'; // R
  556. $chr[83] = '211122'; // S
  557. $chr[84] = '211221'; // T
  558. $chr[85] = '221121'; // U
  559. $chr[86] = '222111'; // V
  560. $chr[87] = '112122'; // W
  561. $chr[88] = '112221'; // X
  562. $chr[89] = '122121'; // Y
  563. $chr[90] = '123111'; // Z
  564. $chr[45] = '121131'; // -
  565. $chr[46] = '311112'; // .
  566. $chr[32] = '311211'; //
  567. $chr[36] = '321111'; // $
  568. $chr[47] = '112131'; // /
  569. $chr[43] = '113121'; // +
  570. $chr[37] = '211131'; // %
  571. $chr[128] = '121221'; // ($)
  572. $chr[129] = '311121'; // (/)
  573. $chr[130] = '122211'; // (+)
  574. $chr[131] = '312111'; // (%)
  575. $chr[42] = '111141'; // start-stop
  576. $code = strtoupper($code);
  577. $encode = array(
  578. chr(0) => chr(131).'U', chr(1) => chr(128).'A', chr(2) => chr(128).'B', chr(3) => chr(128).'C',
  579. chr(4) => chr(128).'D', chr(5) => chr(128).'E', chr(6) => chr(128).'F', chr(7) => chr(128).'G',
  580. chr(8) => chr(128).'H', chr(9) => chr(128).'I', chr(10) => chr(128).'J', chr(11) => '£K',
  581. chr(12) => chr(128).'L', chr(13) => chr(128).'M', chr(14) => chr(128).'N', chr(15) => chr(128).'O',
  582. chr(16) => chr(128).'P', chr(17) => chr(128).'Q', chr(18) => chr(128).'R', chr(19) => chr(128).'S',
  583. chr(20) => chr(128).'T', chr(21) => chr(128).'U', chr(22) => chr(128).'V', chr(23) => chr(128).'W',
  584. chr(24) => chr(128).'X', chr(25) => chr(128).'Y', chr(26) => chr(128).'Z', chr(27) => chr(131).'A',
  585. chr(28) => chr(131).'B', chr(29) => chr(131).'C', chr(30) => chr(131).'D', chr(31) => chr(131).'E',
  586. chr(32) => ' ', chr(33) => chr(129).'A', chr(34) => chr(129).'B', chr(35) => chr(129).'C',
  587. chr(36) => chr(129).'D', chr(37) => chr(129).'E', chr(38) => chr(129).'F', chr(39) => chr(129).'G',
  588. chr(40) => chr(129).'H', chr(41) => chr(129).'I', chr(42) => chr(129).'J', chr(43) => chr(129).'K',
  589. chr(44) => chr(129).'L', chr(45) => '-', chr(46) => '.', chr(47) => chr(129).'O',
  590. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  591. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  592. chr(56) => '8', chr(57) => '9', chr(58) => chr(129).'Z', chr(59) => chr(131).'F',
  593. chr(60) => chr(131).'G', chr(61) => chr(131).'H', chr(62) => chr(131).'I', chr(63) => chr(131).'J',
  594. chr(64) => chr(131).'V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  595. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  596. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  597. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  598. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  599. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  600. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => chr(131).'K',
  601. chr(92) => chr(131).'L', chr(93) => chr(131).'M', chr(94) => chr(131).'N', chr(95) => chr(131).'O',
  602. chr(96) => chr(131).'W', chr(97) => chr(130).'A', chr(98) => chr(130).'B', chr(99) => chr(130).'C',
  603. chr(100) => chr(130).'D', chr(101) => chr(130).'E', chr(102) => chr(130).'F', chr(103) => chr(130).'G',
  604. chr(104) => chr(130).'H', chr(105) => chr(130).'I', chr(106) => chr(130).'J', chr(107) => chr(130).'K',
  605. chr(108) => chr(130).'L', chr(109) => chr(130).'M', chr(110) => chr(130).'N', chr(111) => chr(130).'O',
  606. chr(112) => chr(130).'P', chr(113) => chr(130).'Q', chr(114) => chr(130).'R', chr(115) => chr(130).'S',
  607. chr(116) => chr(130).'T', chr(117) => chr(130).'U', chr(118) => chr(130).'V', chr(119) => chr(130).'W',
  608. chr(120) => chr(130).'X', chr(121) => chr(130).'Y', chr(122) => chr(130).'Z', chr(123) => chr(131).'P',
  609. chr(124) => chr(131).'Q', chr(125) => chr(131).'R', chr(126) => chr(131).'S', chr(127) => chr(131).'T');
  610. $code_ext = '';
  611. $clen = strlen($code);
  612. for ($i = 0 ; $i < $clen; ++$i) {
  613. if (ord($code{$i}) > 127) {
  614. return false;
  615. }
  616. $code_ext .= $encode[$code{$i}];
  617. }
  618. // checksum
  619. $code_ext .= $this->checksum_code93($code_ext);
  620. // add start and stop codes
  621. $code = '*'.$code_ext.'*';
  622. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  623. $k = 0;
  624. $clen = strlen($code);
  625. for ($i = 0; $i < $clen; ++$i) {
  626. $char = ord($code{$i});
  627. if(!isset($chr[$char])) {
  628. // invalid character
  629. return false;
  630. }
  631. for ($j = 0; $j < 6; ++$j) {
  632. if (($j % 2) == 0) {
  633. $t = true; // bar
  634. } else {
  635. $t = false; // space
  636. }
  637. $w = $chr[$char]{$j};
  638. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  639. $bararray['maxw'] += $w;
  640. ++$k;
  641. }
  642. }
  643. $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0);
  644. $bararray['maxw'] += 1;
  645. ++$k;
  646. return $bararray;
  647. }
  648. /**
  649. * Calculate CODE 93 checksum (modulo 47).
  650. * @param $code (string) code to represent.
  651. * @return string checksum code.
  652. * @protected
  653. */
  654. protected function checksum_code93($code) {
  655. $chars = array(
  656. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  657. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  658. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  659. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%',
  660. '<', '=', '>', '?');
  661. // translate special characters
  662. $code = strtr($code, chr(128).chr(131).chr(129).chr(130), '<=>?');
  663. $len = strlen($code);
  664. // calculate check digit C
  665. $p = 1;
  666. $check = 0;
  667. for ($i = ($len - 1); $i >= 0; --$i) {
  668. $k = array_keys($chars, $code{$i});
  669. $check += ($k[0] * $p);
  670. ++$p;
  671. if ($p > 20) {
  672. $p = 1;
  673. }
  674. }
  675. $check %= 47;
  676. $c = $chars[$check];
  677. $code .= $c;
  678. // calculate check digit K
  679. $p = 1;
  680. $check = 0;
  681. for ($i = $len; $i >= 0; --$i) {
  682. $k = array_keys($chars, $code{$i});
  683. $check += ($k[0] * $p);
  684. ++$p;
  685. if ($p > 15) {
  686. $p = 1;
  687. }
  688. }
  689. $check %= 47;
  690. $k = $chars[$check];
  691. $checksum = $c.$k;
  692. // resto respecial characters
  693. $checksum = strtr($checksum, '<=>?', chr(128).chr(131).chr(129).chr(130));
  694. return $checksum;
  695. }
  696. /**
  697. * Checksum for standard 2 of 5 barcodes.
  698. * @param $code (string) code to process.
  699. * @return int checksum.
  700. * @protected
  701. */
  702. protected function checksum_s25($code) {
  703. $len = strlen($code);
  704. $sum = 0;
  705. for ($i = 0; $i < $len; $i+=2) {
  706. $sum += $code{$i};
  707. }
  708. $sum *= 3;
  709. for ($i = 1; $i < $len; $i+=2) {
  710. $sum += ($code{$i});
  711. }
  712. $r = $sum % 10;
  713. if($r > 0) {
  714. $r = (10 - $r);
  715. }
  716. return $r;
  717. }
  718. /**
  719. * MSI.
  720. * Variation of Plessey code, with similar applications
  721. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  722. * @param $code (string) code to represent.
  723. * @param $checksum (boolean) if true add a checksum to the code (modulo 11)
  724. * @return array barcode representation.
  725. * @protected
  726. */
  727. protected function barcode_msi($code, $checksum=false) {
  728. $chr['0'] = '100100100100';
  729. $chr['1'] = '100100100110';
  730. $chr['2'] = '100100110100';
  731. $chr['3'] = '100100110110';
  732. $chr['4'] = '100110100100';
  733. $chr['5'] = '100110100110';
  734. $chr['6'] = '100110110100';
  735. $chr['7'] = '100110110110';
  736. $chr['8'] = '110100100100';
  737. $chr['9'] = '110100100110';
  738. $chr['A'] = '110100110100';
  739. $chr['B'] = '110100110110';
  740. $chr['C'] = '110110100100';
  741. $chr['D'] = '110110100110';
  742. $chr['E'] = '110110110100';
  743. $chr['F'] = '110110110110';
  744. if ($checksum) {
  745. // add checksum
  746. $clen = strlen($code);
  747. $p = 2;
  748. $check = 0;
  749. for ($i = ($clen - 1); $i >= 0; --$i) {
  750. $check += (hexdec($code{$i}) * $p);
  751. ++$p;
  752. if ($p > 7) {
  753. $p = 2;
  754. }
  755. }
  756. $check %= 11;
  757. if ($check > 0) {
  758. $check = 11 - $check;
  759. }
  760. $code .= $check;
  761. }
  762. $seq = '110'; // left guard
  763. $clen = strlen($code);
  764. for ($i = 0; $i < $clen; ++$i) {
  765. $digit = $code{$i};
  766. if (!isset($chr[$digit])) {
  767. // invalid character
  768. return false;
  769. }
  770. $seq .= $chr[$digit];
  771. }
  772. $seq .= '1001'; // right guard
  773. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  774. return $this->binseq_to_array($seq, $bararray);
  775. }
  776. /**
  777. * Standard 2 of 5 barcodes.
  778. * Used in airline ticket marking, photofinishing
  779. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  780. * @param $code (string) code to represent.
  781. * @param $checksum (boolean) if true add a checksum to the code
  782. * @return array barcode representation.
  783. * @protected
  784. */
  785. protected function barcode_s25($code, $checksum=false) {
  786. $chr['0'] = '10101110111010';
  787. $chr['1'] = '11101010101110';
  788. $chr['2'] = '10111010101110';
  789. $chr['3'] = '11101110101010';
  790. $chr['4'] = '10101110101110';
  791. $chr['5'] = '11101011101010';
  792. $chr['6'] = '10111011101010';
  793. $chr['7'] = '10101011101110';
  794. $chr['8'] = '10101110111010';
  795. $chr['9'] = '10111010111010';
  796. if ($checksum) {
  797. // add checksum
  798. $code .= $this->checksum_s25($code);
  799. }
  800. if((strlen($code) % 2) != 0) {
  801. // add leading zero if code-length is odd
  802. $code = '0'.$code;
  803. }
  804. $seq = '11011010';
  805. $clen = strlen($code);
  806. for ($i = 0; $i < $clen; ++$i) {
  807. $digit = $code{$i};
  808. if (!isset($chr[$digit])) {
  809. // invalid character
  810. return false;
  811. }
  812. $seq .= $chr[$digit];
  813. }
  814. $seq .= '1101011';
  815. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  816. return $this->binseq_to_array($seq, $bararray);
  817. }
  818. /**
  819. * Convert binary barcode sequence to TCPDF barcode array.
  820. * @param $seq (string) barcode as binary sequence.
  821. * @param $bararray (array) barcode array.
  822. * òparam array $bararray TCPDF barcode array to fill up
  823. * @return array barcode representation.
  824. * @protected
  825. */
  826. protected function binseq_to_array($seq, $bararray) {
  827. $len = strlen($seq);
  828. $w = 0;
  829. $k = 0;
  830. for ($i = 0; $i < $len; ++$i) {
  831. $w += 1;
  832. if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq{$i} != $seq{($i+1)}))) {
  833. if ($seq{$i} == '1') {
  834. $t = true; // bar
  835. } else {
  836. $t = false; // space
  837. }
  838. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  839. $bararray['maxw'] += $w;
  840. ++$k;
  841. $w = 0;
  842. }
  843. }
  844. return $bararray;
  845. }
  846. /**
  847. * Interleaved 2 of 5 barcodes.
  848. * Compact numeric code, widely used in industry, air cargo
  849. * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
  850. * @param $code (string) code to represent.
  851. * @param $checksum (boolean) if true add a checksum to the code
  852. * @return array barcode representation.
  853. * @protected
  854. */
  855. protected function barcode_i25($code, $checksum=false) {
  856. $chr['0'] = '11221';
  857. $chr['1'] = '21112';
  858. $chr['2'] = '12112';
  859. $chr['3'] = '22111';
  860. $chr['4'] = '11212';
  861. $chr['5'] = '21211';
  862. $chr['6'] = '12211';
  863. $chr['7'] = '11122';
  864. $chr['8'] = '21121';
  865. $chr['9'] = '12121';
  866. $chr['A'] = '11';
  867. $chr['Z'] = '21';
  868. if ($checksum) {
  869. // add checksum
  870. $code .= $this->checksum_s25($code);
  871. }
  872. if((strlen($code) % 2) != 0) {
  873. // add leading zero if code-length is odd
  874. $code = '0'.$code;
  875. }
  876. // add start and stop codes
  877. $code = 'AA'.strtolower($code).'ZA';
  878. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  879. $k = 0;
  880. $clen = strlen($code);
  881. for ($i = 0; $i < $clen; $i = ($i + 2)) {
  882. $char_bar = $code{$i};
  883. $char_space = $code{$i+1};
  884. if((!isset($chr[$char_bar])) OR (!isset($chr[$char_space]))) {
  885. // invalid character
  886. return false;
  887. }
  888. // create a bar-space sequence
  889. $seq = '';
  890. $chrlen = strlen($chr[$char_bar]);
  891. for ($s = 0; $s < $chrlen; $s++){
  892. $seq .= $chr[$char_bar]{$s} . $chr[$char_space]{$s};
  893. }
  894. $seqlen = strlen($seq);
  895. for ($j = 0; $j < $seqlen; ++$j) {
  896. if (($j % 2) == 0) {
  897. $t = true; // bar
  898. } else {
  899. $t = false; // space
  900. }
  901. $w = $seq{$j};
  902. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  903. $bararray['maxw'] += $w;
  904. ++$k;
  905. }
  906. }
  907. return $bararray;
  908. }
  909. /**
  910. * C128 barcodes.
  911. * Very capable code, excellent density, high reliability; in very wide use world-wide
  912. * @param $code (string) code to represent.
  913. * @param $type (string) barcode type: A, B, C or empty for automatic switch (AUTO mode)
  914. * @return array barcode representation.
  915. * @protected
  916. */
  917. protected function barcode_c128($code, $type='') {
  918. $chr = array(
  919. '212222', /* 00 */
  920. '222122', /* 01 */
  921. '222221', /* 02 */
  922. '121223', /* 03 */
  923. '121322', /* 04 */
  924. '131222', /* 05 */
  925. '122213', /* 06 */
  926. '122312', /* 07 */
  927. '132212', /* 08 */
  928. '221213', /* 09 */
  929. '221312', /* 10 */
  930. '231212', /* 11 */
  931. '112232', /* 12 */
  932. '122132', /* 13 */
  933. '122231', /* 14 */
  934. '113222', /* 15 */
  935. '123122', /* 16 */
  936. '123221', /* 17 */
  937. '223211', /* 18 */
  938. '221132', /* 19 */
  939. '221231', /* 20 */
  940. '213212', /* 21 */
  941. '223112', /* 22 */
  942. '312131', /* 23 */
  943. '311222', /* 24 */
  944. '321122', /* 25 */
  945. '321221', /* 26 */
  946. '312212', /* 27 */
  947. '322112', /* 28 */
  948. '322211', /* 29 */
  949. '212123', /* 30 */
  950. '212321', /* 31 */
  951. '232121', /* 32 */
  952. '111323', /* 33 */
  953. '131123', /* 34 */
  954. '131321', /* 35 */
  955. '112313', /* 36 */
  956. '132113', /* 37 */
  957. '132311', /* 38 */
  958. '211313', /* 39 */
  959. '231113', /* 40 */
  960. '231311', /* 41 */
  961. '112133', /* 42 */
  962. '112331', /* 43 */
  963. '132131', /* 44 */
  964. '113123', /* 45 */
  965. '113321', /* 46 */
  966. '133121', /* 47 */
  967. '313121', /* 48 */
  968. '211331', /* 49 */
  969. '231131', /* 50 */
  970. '213113', /* 51 */
  971. '213311', /* 52 */
  972. '213131', /* 53 */
  973. '311123', /* 54 */
  974. '311321', /* 55 */
  975. '331121', /* 56 */
  976. '312113', /* 57 */
  977. '312311', /* 58 */
  978. '332111', /* 59 */
  979. '314111', /* 60 */
  980. '221411', /* 61 */
  981. '431111', /* 62 */
  982. '111224', /* 63 */
  983. '111422', /* 64 */
  984. '121124', /* 65 */
  985. '121421', /* 66 */
  986. '141122', /* 67 */
  987. '141221', /* 68 */
  988. '112214', /* 69 */
  989. '112412', /* 70 */
  990. '122114', /* 71 */
  991. '122411', /* 72 */
  992. '142112', /* 73 */
  993. '142211', /* 74 */
  994. '241211', /* 75 */
  995. '221114', /* 76 */
  996. '413111', /* 77 */
  997. '241112', /* 78 */
  998. '134111', /* 79 */
  999. '111242', /* 80 */
  1000. '121142', /* 81 */
  1001. '121241', /* 82 */
  1002. '114212', /* 83 */
  1003. '124112', /* 84 */
  1004. '124211', /* 85 */
  1005. '411212', /* 86 */
  1006. '421112', /* 87 */
  1007. '421211', /* 88 */
  1008. '212141', /* 89 */
  1009. '214121', /* 90 */
  1010. '412121', /* 91 */
  1011. '111143', /* 92 */
  1012. '111341', /* 93 */
  1013. '131141', /* 94 */
  1014. '114113', /* 95 */
  1015. '114311', /* 96 */
  1016. '411113', /* 97 */
  1017. '411311', /* 98 */
  1018. '113141', /* 99 */
  1019. '114131', /* 100 */
  1020. '311141', /* 101 */
  1021. '411131', /* 102 */
  1022. '211412', /* 103 START A */
  1023. '211214', /* 104 START B */
  1024. '211232', /* 105 START C */
  1025. '233111', /* STOP */
  1026. '200000' /* END */
  1027. );
  1028. // ASCII characters for code A (ASCII 00 - 95)
  1029. $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
  1030. $keys_a .= chr(0).chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8).chr(9);
  1031. $keys_a .= chr(10).chr(11).chr(12).chr(13).chr(14).chr(15).chr(16).chr(17).chr(18).chr(19);
  1032. $keys_a .= chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29);
  1033. $keys_a .= chr(30).chr(31);
  1034. // ASCII characters for code B (ASCII 32 - 127)
  1035. $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.chr(127);
  1036. // special codes
  1037. $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);
  1038. $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100);
  1039. // array of symbols
  1040. $code_data = array();
  1041. // lenght of the code
  1042. $len = strlen($code);
  1043. switch(strtoupper($type)) {
  1044. case 'A': { // MODE A
  1045. $startid = 103;
  1046. for ($i = 0; $i < $len; ++$i) {
  1047. $char = $code{$i};
  1048. $char_id = ord($char);
  1049. if (($char_id >= 241) AND ($char_id <= 244)) {
  1050. $code_data[] = $fnc_a[$char_id];
  1051. } elseif (($char_id >= 0) AND ($char_id <= 95)) {
  1052. $code_data[] = strpos($keys_a, $char);
  1053. } else {
  1054. return false;
  1055. }
  1056. }
  1057. break;
  1058. }
  1059. case 'B': { // MODE B
  1060. $startid = 104;
  1061. for ($i = 0; $i < $len; ++$i) {
  1062. $char = $code{$i};
  1063. $char_id = ord($char);
  1064. if (($char_id >= 241) AND ($char_id <= 244)) {
  1065. $code_data[] = $fnc_b[$char_id];
  1066. } elseif (($char_id >= 32) AND ($char_id <= 127)) {
  1067. $code_data[] = strpos($keys_b, $char);
  1068. } else {
  1069. return false;
  1070. }
  1071. }
  1072. break;
  1073. }
  1074. case 'C': { // MODE C
  1075. $startid = 105;
  1076. if (ord($code{0}) == 241) {
  1077. $code_data[] = 102;
  1078. $code = substr($code, 1);
  1079. --$len;
  1080. }
  1081. if (($len % 2) != 0) {
  1082. // the length must be even
  1083. return false;
  1084. }
  1085. for ($i = 0; $i < $len; $i+=2) {
  1086. $chrnum = $code{$i}.$code{$i+1};
  1087. if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
  1088. $code_data[] = intval($chrnum);
  1089. } else {
  1090. return false;
  1091. }
  1092. }
  1093. break;
  1094. }
  1095. default: { // MODE AUTO
  1096. // split code into sequences
  1097. $sequence = array();
  1098. // get numeric sequences (if any)
  1099. $numseq = array();
  1100. preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1101. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1102. $end_offset = 0;
  1103. foreach ($numseq[1] as $val) {
  1104. $offset = $val[1];
  1105. if ($offset > $end_offset) {
  1106. // non numeric sequence
  1107. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset))));
  1108. }
  1109. // numeric sequence
  1110. $slen = strlen($val[0]);
  1111. if (($slen % 2) != 0) {
  1112. // the length must be even
  1113. --$slen;
  1114. }
  1115. $sequence[] = array('C', substr($code, $offset, $slen), $slen);
  1116. $end_offset = $offset + $slen;
  1117. }
  1118. if ($end_offset < $len) {
  1119. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset)));
  1120. }
  1121. } else {
  1122. // text code (non C mode)
  1123. $sequence = array_merge($sequence, $this->get128ABsequence($code));
  1124. }
  1125. // process the sequence
  1126. foreach ($sequence as $key => $seq) {
  1127. switch($seq[0]) {
  1128. case 'A': {
  1129. if ($key == 0) {
  1130. $startid = 103;
  1131. } elseif ($sequence[($key - 1)][0] != 'A') {
  1132. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND (!isset($sequence[($key - 1)][3]))) {
  1133. // single character shift
  1134. $code_data[] = 98;
  1135. // mark shift
  1136. $sequence[$key][3] = true;
  1137. } elseif (!isset($sequence[($key - 1)][3])) {
  1138. $code_data[] = 101;
  1139. }
  1140. }
  1141. for ($i = 0; $i < $seq[2]; ++$i) {
  1142. $char = $seq[1]{$i};
  1143. $char_id = ord($char);
  1144. if (($char_id >= 241) AND ($char_id <= 244)) {
  1145. $code_data[] = $fnc_a[$char_id];
  1146. } else {
  1147. $code_data[] = strpos($keys_a, $char);
  1148. }
  1149. }
  1150. break;
  1151. }
  1152. case 'B': {
  1153. if ($key == 0) {
  1154. $tmpchr = ord($seq[1]{0});
  1155. if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) {
  1156. switch ($sequence[($key + 1)][0]) {
  1157. case 'A': {
  1158. $startid = 103;
  1159. $sequence[$key][0] = 'A';
  1160. $code_data[] = $fnc_a[$tmpchr];
  1161. break;
  1162. }
  1163. case 'C': {
  1164. $startid = 105;
  1165. $sequence[$key][0] = 'C';
  1166. $code_data[] = $fnc_a[$tmpchr];
  1167. break;
  1168. }
  1169. }
  1170. break;
  1171. } else {
  1172. $startid = 104;
  1173. }
  1174. } elseif ($sequence[($key - 1)][0] != 'B') {
  1175. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND (!isset($sequence[($key - 1)][3]))) {
  1176. // single character shift
  1177. $code_data[] = 98;
  1178. // mark shift
  1179. $sequence[$key][3] = true;
  1180. } elseif (!isset($sequence[($key - 1)][3])) {
  1181. $code_data[] = 100;
  1182. }
  1183. }
  1184. for ($i = 0; $i < $seq[2]; ++$i) {
  1185. $char = $seq[1]{$i};
  1186. $char_id = ord($char);
  1187. if (($char_id >= 241) AND ($char_id <= 244)) {
  1188. $code_data[] = $fnc_b[$char_id];
  1189. } else {
  1190. $code_data[] = strpos($keys_b, $char);
  1191. }
  1192. }
  1193. break;
  1194. }
  1195. case 'C': {
  1196. if ($key == 0) {
  1197. $startid = 105;
  1198. } elseif ($sequence[($key - 1)][0] != 'C') {
  1199. $code_data[] = 99;
  1200. }
  1201. for ($i = 0; $i < $seq[2]; $i+=2) {
  1202. $chrnum = $seq[1]{$i}.$seq[1]{$i+1};
  1203. $code_data[] = intval($chrnum);
  1204. }
  1205. break;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. // calculate check character
  1212. $sum = $startid;
  1213. foreach ($code_data as $key => $val) {
  1214. $sum += ($val * ($key + 1));
  1215. }
  1216. // add check character
  1217. $code_data[] = ($sum % 103);
  1218. // add stop sequence
  1219. $code_data[] = 106;
  1220. $code_data[] = 107;
  1221. // add start code at the beginning
  1222. array_unshift($code_data, $startid);
  1223. // build barcode array
  1224. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1225. foreach ($code_data as $val) {
  1226. $seq = $chr[$val];
  1227. for ($j = 0; $j < 6; ++$j) {
  1228. if (($j % 2) == 0) {
  1229. $t = true; // bar
  1230. } else {
  1231. $t = false; // space
  1232. }
  1233. $w = $seq{$j};
  1234. $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1235. $bararray['maxw'] += $w;
  1236. }
  1237. }
  1238. return $bararray;
  1239. }
  1240. /**
  1241. * Split text code in A/B sequence for 128 code
  1242. * @param $code (string) code to split.
  1243. * @return array sequence
  1244. * @protected
  1245. */
  1246. protected function get128ABsequence($code) {
  1247. $len = strlen($code);
  1248. $sequence = array();
  1249. // get A sequences (if any)
  1250. $numseq = array();
  1251. preg_match_all('/([\0-\31])/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1252. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1253. $end_offset = 0;
  1254. foreach ($numseq[1] as $val) {
  1255. $offset = $val[1];
  1256. if ($offset > $end_offset) {
  1257. // B sequence
  1258. $sequence[] = array('B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset));
  1259. }
  1260. // A sequence
  1261. $slen = strlen($val[0]);
  1262. $sequence[] = array('A', substr($code, $offset, $slen), $slen);
  1263. $end_offset = $offset + $slen;
  1264. }
  1265. if ($end_offset < $len) {
  1266. $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
  1267. }
  1268. } else {
  1269. // only B sequence
  1270. $sequence[] = array('B', $code, $len);
  1271. }
  1272. return $sequence;
  1273. }
  1274. /**
  1275. * EAN13 and UPC-A barcodes.
  1276. * EAN13: European Article Numbering international retail product code
  1277. * UPC-A: Universal product code seen on almost all retail products in the USA and Canada
  1278. * UPC-E: Short version of UPC symbol
  1279. * @param $code (string) code to represent.
  1280. * @param $len (string) barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A
  1281. * @return array barcode representation.
  1282. * @protected
  1283. */
  1284. protected function barcode_eanupc($code, $len=13) {
  1285. $upce = false;
  1286. if ($len == 6) {
  1287. $len = 12; // UPC-A
  1288. $upce = true; // UPC-E mode
  1289. }
  1290. $data_len = $len - 1;
  1291. //Padding
  1292. $code = str_pad($code, $data_len, '0', STR_PAD_LEFT);
  1293. $code_len = strlen($code);
  1294. // calculate check digit
  1295. $sum_a = 0;
  1296. for ($i = 1; $i < $data_len; $i+=2) {
  1297. $sum_a += $code{$i};
  1298. }
  1299. if ($len > 12) {
  1300. $sum_a *= 3;
  1301. }
  1302. $sum_b = 0;
  1303. for ($i = 0; $i < $data_len; $i+=2) {
  1304. $sum_b += ($code{$i});
  1305. }
  1306. if ($len < 13) {
  1307. $sum_b *= 3;
  1308. }
  1309. $r = ($sum_a + $sum_b) % 10;
  1310. if($r > 0) {
  1311. $r = (10 - $r);
  1312. }
  1313. if ($code_len == $data_len) {
  1314. // add check digit
  1315. $code .= $r;
  1316. } elseif ($r !== intval($code{$data_len})) {
  1317. // wrong checkdigit
  1318. return false;
  1319. }
  1320. if ($len == 12) {
  1321. // UPC-A
  1322. $code = '0'.$code;
  1323. ++$len;
  1324. }
  1325. if ($upce) {
  1326. // convert UPC-A to UPC-E
  1327. $tmp = substr($code, 4, 3);
  1328. if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) {
  1329. // manufacturer code ends in 000, 100, or 200
  1330. $upce_code = substr($code, 2, 2).substr($code, 9, 3).substr($code, 4, 1);
  1331. } else {
  1332. $tmp = substr($code, 5, 2);
  1333. if ($tmp == '00') {
  1334. // manufacturer code ends in 00
  1335. $upce_code = substr($code, 2, 3).substr($code, 10, 2).'3';
  1336. } else {
  1337. $tmp = substr($code, 6, 1);
  1338. if ($tmp == '0') {
  1339. // manufacturer code ends in 0
  1340. $upce_code = substr($code, 2, 4).substr($code, 11, 1).'4';
  1341. } else {
  1342. // manufacturer code does not end in zero
  1343. $upce_code = substr($code, 2, 5).substr($code, 11, 1);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. //Convert digits to bars
  1349. $codes = array(
  1350. 'A'=>array( // left odd parity
  1351. '0'=>'0001101',
  1352. '1'=>'0011001',
  1353. '2'=>'0010011',
  1354. '3'=>'0111101',
  1355. '4'=>'0100011',
  1356. '5'=>'0110001',
  1357. '6'=>'0101111',
  1358. '7'=>'0111011',
  1359. '8'=>'0110111',
  1360. '9'=>'0001011'),
  1361. 'B'=>array( // left even parity
  1362. '0'=>'0100111',
  1363. '1'=>'0110011',
  1364. '2'=>'0011011',
  1365. '3'=>'0100001',
  1366. '4'=>'0011101',
  1367. '5'=>'0111001',
  1368. '6'=>'0000101',
  1369. '7'=>'0010001',
  1370. '8'=>'0001001',
  1371. '9'=>'0010111'),
  1372. 'C'=>array( // right
  1373. '0'=>'1110010',
  1374. '1'=>'1100110',
  1375. '2'=>'1101100',
  1376. '3'=>'1000010',
  1377. '4'=>'1011100',
  1378. '5'=>'1001110',
  1379. '6'=>'1010000',
  1380. '7'=>'1000100',
  1381. '8'=>'1001000',
  1382. '9'=>'1110100')
  1383. );
  1384. $parities = array(
  1385. '0'=>array('A','A','A','A','A','A'),
  1386. '1'=>array('A','A','B','A','B','B'),
  1387. '2'=>array('A','A','B','B','A','B'),
  1388. '3'=>array('A','A','B','B','B','A'),
  1389. '4'=>array('A','B','A','A','B','B'),
  1390. '5'=>array('A','B','B','A','A','B'),
  1391. '6'=>array('A','B','B','B','A','A'),
  1392. '7'=>array('A','B','A','B','A','B'),
  1393. '8'=>array('A','B','A','B','B','A'),
  1394. '9'=>array('A','B','B','A','B','A')
  1395. );
  1396. $upce_parities = array();
  1397. $upce_parities[0] = array(
  1398. '0'=>array('B','B','B','A','A','A'),
  1399. '1'=>array('B','B','A','B','A','A'),
  1400. '2'=>array('B','B','A','A','B','A'),
  1401. '3'=>array('B','B','A','A','A','B'),
  1402. '4'=>array('B','A','B','B','A','A'),
  1403. '5'=>array('B','A','A','B','B','A'),
  1404. '6'=>array('B','A','A','A','B','B'),
  1405. '7'=>array('B','A','B','A','B','A'),
  1406. '8'=>array('B','A','B','A','A','B'),
  1407. '9'=>array('B','A','A','B','A','B')
  1408. );
  1409. $upce_parities[1] = array(
  1410. '0'=>array('A','A','A','B','B','B'),
  1411. '1'=>array('A','A','B','A','B','B'),
  1412. '2'=>array('A','A','B','B','A','B'),
  1413. '3'=>array('A','A','B','B','B','A'),
  1414. '4'=>array('A','B','A','A','B','B'),
  1415. '5'=>array('A','B','B','A','A','B'),
  1416. '6'=>array('A','B','B','B','A','A'),
  1417. '7'=>array('A','B','A','B','A','B'),
  1418. '8'=>array('A','B','A','B','B','A'),
  1419. '9'=>array('A','B','B','A','B','A')
  1420. );
  1421. $k = 0;
  1422. $seq = '101'; // left guard bar
  1423. if ($upce) {
  1424. $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1425. $p = $upce_parities[$code{1}][$r];
  1426. for ($i = 0; $i < 6; ++$i) {
  1427. $seq .= $codes[$p[$i]][$upce_code{$i}];
  1428. }
  1429. $seq .= '010101'; // right guard bar
  1430. } else {
  1431. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1432. $half_len = ceil($len / 2);
  1433. if ($len == 8) {
  1434. for ($i = 0; $i < $half_len; ++$i) {
  1435. $seq .= $codes['A'][$code{$i}];
  1436. }
  1437. } else {
  1438. $p = $parities[$code{0}];
  1439. for ($i = 1; $i < $half_len; ++$i) {
  1440. $seq .= $codes[$p[$i-1]][$code{$i}];
  1441. }
  1442. }
  1443. $seq .= '01010'; // center guard bar
  1444. for ($i = $half_len; $i < $len; ++$i) {
  1445. $seq .= $codes['C'][$code{$i}];
  1446. }
  1447. $seq .= '101'; // right guard bar
  1448. }
  1449. $clen = strlen($seq);
  1450. $w = 0;
  1451. for ($i = 0; $i < $clen; ++$i) {
  1452. $w += 1;
  1453. if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq{$i} != $seq{($i+1)}))) {
  1454. if ($seq{$i} == '1') {
  1455. $t = true; // bar
  1456. } else {
  1457. $t = false; // space
  1458. }
  1459. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1460. $bararray['maxw'] += $w;
  1461. ++$k;
  1462. $w = 0;
  1463. }
  1464. }
  1465. return $bararray;
  1466. }
  1467. /**
  1468. * UPC-Based Extentions
  1469. * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
  1470. * 5-Digit Ext.: Used to mark suggested retail price of books
  1471. * @param $code (string) code to represent.
  1472. * @param $len (string) barcode type: 2 = 2-Digit, 5 = 5-Digit
  1473. * @return array barcode representation.
  1474. * @protected
  1475. */
  1476. protected function barcode_eanext($code, $len=5) {
  1477. //Padding
  1478. $code = str_pad($code, $len, '0', STR_PAD_LEFT);
  1479. // calculate check digit
  1480. if ($len == 2) {
  1481. $r = $code % 4;
  1482. } elseif ($len == 5) {
  1483. $r = (3 * ($code{0} + $code{2} + $code{4})) + (9 * ($code{1} + $code{3}));
  1484. $r %= 10;
  1485. } else {
  1486. return false;
  1487. }
  1488. //Convert digits to bars
  1489. $codes = array(
  1490. 'A'=>array( // left odd parity
  1491. '0'=>'0001101',
  1492. '1'=>'0011001',
  1493. '2'=>'0010011',
  1494. '3'=>'0111101',
  1495. '4'=>'0100011',
  1496. '5'=>'0110001',
  1497. '6'=>'0101111',
  1498. '7'=>'0111011',
  1499. '8'=>'0110111',
  1500. '9'=>'0001011'),
  1501. 'B'=>array( // left even parity
  1502. '0'=>'0100111',
  1503. '1'=>'0110011',
  1504. '2'=>'0011011',
  1505. '3'=>'0100001',
  1506. '4'=>'0011101',
  1507. '5'=>'0111001',
  1508. '6'=>'0000101',
  1509. '7'=>'0010001',
  1510. '8'=>'0001001',
  1511. '9'=>'0010111')
  1512. );
  1513. $parities = array();
  1514. $parities[2] = array(
  1515. '0'=>array('A','A'),
  1516. '1'=>array('A','B'),
  1517. '2'=>array('B','A'),
  1518. '3'=>array('B','B')
  1519. );
  1520. $parities[5] = array(
  1521. '0'=>array('B','B','A','A','A'),
  1522. '1'=>array('B','A','B','A','A'),
  1523. '2'=>array('B','A','A','B','A'),
  1524. '3'=>array('B','A','A','A','B'),
  1525. '4'=>array('A','B','B','A','A'),
  1526. '5'=>array('A','A','B','B','A'),
  1527. '6'=>array('A','A','A','B','B'),
  1528. '7'=>array('A','B','A','B','A'),
  1529. '8'=>array('A','B','A','A','B'),
  1530. '9'=>array('A','A','B','A','B')
  1531. );
  1532. $p = $parities[$len][$r];
  1533. $seq = '1011'; // left guard bar
  1534. $seq .= $codes[$p[0]][$code{0}];
  1535. for ($i = 1; $i < $len; ++$i) {
  1536. $seq .= '01'; // separator
  1537. $seq .= $codes[$p[$i]][$code{$i}];
  1538. }
  1539. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1540. return $this->binseq_to_array($seq, $bararray);
  1541. }
  1542. /**
  1543. * POSTNET and PLANET barcodes.
  1544. * Used by U.S. Postal Service for automated mail sorting
  1545. * @param $code (string) zip code to represent. Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD.
  1546. * @param $planet (boolean) if true print the PLANET barcode, otherwise print POSTNET
  1547. * @return array barcode representation.
  1548. * @protected
  1549. */
  1550. protected function barcode_postnet($code, $planet=false) {
  1551. // bar lenght
  1552. if ($planet) {
  1553. $barlen = Array(
  1554. 0 => Array(1,1,2,2,2),
  1555. 1 => Array(2,2,2,1,1),
  1556. 2 => Array(2,2,1,2,1),
  1557. 3 => Array(2,2,1,1,2),
  1558. 4 => Array(2,1,2,2,1),
  1559. 5 => Array(2,1,2,1,2),
  1560. 6 => Array(2,1,1,2,2),
  1561. 7 => Array(1,2,2,2,1),
  1562. 8 => Array(1,2,2,1,2),
  1563. 9 => Array(1,2,1,2,2)
  1564. );
  1565. } else {
  1566. $barlen = Array(
  1567. 0 => Array(2,2,1,1,1),
  1568. 1 => Array(1,1,1,2,2),
  1569. 2 => Array(1,1,2,1,2),
  1570. 3 => Array(1,1,2,2,1),
  1571. 4 => Array(1,2,1,1,2),
  1572. 5 => Array(1,2,1,2,1),
  1573. 6 => Array(1,2,2,1,1),
  1574. 7 => Array(2,1,1,1,2),
  1575. 8 => Array(2,1,1,2,1),
  1576. 9 => Array(2,1,2,1,1)
  1577. );
  1578. }
  1579. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
  1580. $k = 0;
  1581. $code = str_replace('-', '', $code);
  1582. $code = str_replace(' ', '', $code);
  1583. $len = strlen($code);
  1584. // calculate checksum
  1585. $sum = 0;
  1586. for ($i = 0; $i < $len; ++$i) {
  1587. $sum += intval($code{$i});
  1588. }
  1589. $chkd = ($sum % 10);
  1590. if($chkd > 0) {
  1591. $chkd = (10 - $chkd);
  1592. }
  1593. $code .= $chkd;
  1594. $len = strlen($code);
  1595. // start bar
  1596. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1597. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1598. $bararray['maxw'] += 2;
  1599. for ($i = 0; $i < $len; ++$i) {
  1600. for ($j = 0; $j < 5; ++$j) {
  1601. $h = $barlen[$code{$i}][$j];
  1602. $p = floor(1 / $h);
  1603. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1604. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1605. $bararray['maxw'] += 2;
  1606. }
  1607. }
  1608. // end bar
  1609. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1610. $bararray['maxw'] += 1;
  1611. return $bararray;
  1612. }
  1613. /**
  1614. * RMS4CC - CBC - KIX
  1615. * RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index)
  1616. * RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service.
  1617. * @param $code (string) code to print
  1618. * @param $kix (boolean) if true prints the KIX variation (doesn't use the start and end symbols, and the checksum) - in this case the house number must be sufficed with an X and placed at the end of the code.
  1619. * @return array barcode representation.
  1620. * @protected
  1621. */
  1622. protected function barcode_rms4cc($code, $kix=false) {
  1623. $notkix = !$kix;
  1624. // bar mode
  1625. // 1 = pos 1, length 2
  1626. // 2 = pos 1, length 3
  1627. // 3 = pos 2, length 1
  1628. // 4 = pos 2, length 2
  1629. $barmode = array(
  1630. '0' => array(3,3,2,2),
  1631. '1' => array(3,4,1,2),
  1632. '2' => array(3,4,2,1),
  1633. '3' => array(4,3,1,2),
  1634. '4' => array(4,3,2,1),
  1635. '5' => array(4,4,1,1),
  1636. '6' => array(3,1,4,2),
  1637. '7' => array(3,2,3,2),
  1638. '8' => array(3,2,4,1),
  1639. '9' => array(4,1,3,2),
  1640. 'A' => array(4,1,4,1),
  1641. 'B' => array(4,2,3,1),
  1642. 'C' => array(3,1,2,4),
  1643. 'D' => array(3,2,1,4),
  1644. 'E' => array(3,2,2,3),
  1645. 'F' => array(4,1,1,4),
  1646. 'G' => array(4,1,2,3),
  1647. 'H' => array(4,2,1,3),
  1648. 'I' => array(1,3,4,2),
  1649. 'J' => array(1,4,3,2),
  1650. 'K' => array(1,4,4,1),
  1651. 'L' => array(2,3,3,2),
  1652. 'M' => array(2,3,4,1),
  1653. 'N' => array(2,4,3,1),
  1654. 'O' => array(1,3,2,4),
  1655. 'P' => array(1,4,1,4),
  1656. 'Q' => array(1,4,2,3),
  1657. 'R' => array(2,3,1,4),
  1658. 'S' => array(2,3,2,3),
  1659. 'T' => array(2,4,1,3),
  1660. 'U' => array(1,1,4,4),
  1661. 'V' => array(1,2,3,4),
  1662. 'W' => array(1,2,4,3),
  1663. 'X' => array(2,1,3,4),
  1664. 'Y' => array(2,1,4,3),
  1665. 'Z' => array(2,2,3,3)
  1666. );
  1667. $code = strtoupper($code);
  1668. $len = strlen($code);
  1669. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
  1670. if ($notkix) {
  1671. // table for checksum calculation (row,col)
  1672. $checktable = array(
  1673. '0' => array(1,1),
  1674. '1' => array(1,2),
  1675. '2' => array(1,3),
  1676. '3' => array(1,4),
  1677. '4' => array(1,5),
  1678. '5' => array(1,0),
  1679. '6' => array(2,1),
  1680. '7' => array(2,2),
  1681. '8' => array(2,3),
  1682. '9' => array(2,4),
  1683. 'A' => array(2,5),
  1684. 'B' => array(2,0),
  1685. 'C' => array(3,1),
  1686. 'D' => array(3,2),
  1687. 'E' => array(3,3),
  1688. 'F' => array(3,4),
  1689. 'G' => array(3,5),
  1690. 'H' => array(3,0),
  1691. 'I' => array(4,1),
  1692. 'J' => array(4,2),
  1693. 'K' => array(4,3),
  1694. 'L' => array(4,4),
  1695. 'M' => array(4,5),
  1696. 'N' => array(4,0),
  1697. 'O' => array(5,1),
  1698. 'P' => array(5,2),
  1699. 'Q' => array(5,3),
  1700. 'R' => array(5,4),
  1701. 'S' => array(5,5),
  1702. 'T' => array(5,0),
  1703. 'U' => array(0,1),
  1704. 'V' => array(0,2),
  1705. 'W' => array(0,3),
  1706. 'X' => array(0,4),
  1707. 'Y' => array(0,5),
  1708. 'Z' => array(0,0)
  1709. );
  1710. $row = 0;
  1711. $col = 0;
  1712. for ($i = 0; $i < $len; ++$i) {
  1713. $row += $checktable[$code{$i}][0];
  1714. $col += $checktable[$code{$i}][1];
  1715. }
  1716. $row %= 6;
  1717. $col %= 6;
  1718. $chk = array_keys($checktable, array($row,$col));
  1719. $code .= $chk[0];
  1720. ++$len;
  1721. }
  1722. $k = 0;
  1723. if ($notkix) {
  1724. // start bar
  1725. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1726. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1727. $bararray['maxw'] += 2;
  1728. }
  1729. for ($i = 0; $i < $len; ++$i) {
  1730. for ($j = 0; $j < 4; ++$j) {
  1731. switch ($barmode[$code{$i}][$j]) {
  1732. case 1: {
  1733. $p = 0;
  1734. $h = 2;
  1735. break;
  1736. }
  1737. case 2: {
  1738. $p = 0;
  1739. $h = 3;
  1740. break;
  1741. }
  1742. case 3: {
  1743. $p = 1;
  1744. $h = 1;
  1745. break;
  1746. }
  1747. case 4: {
  1748. $p = 1;
  1749. $h = 2;
  1750. break;
  1751. }
  1752. }
  1753. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1754. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1755. $bararray['maxw'] += 2;
  1756. }
  1757. }
  1758. if ($notkix) {
  1759. // stop bar
  1760. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0);
  1761. $bararray['maxw'] += 1;
  1762. }
  1763. return $bararray;
  1764. }
  1765. /**
  1766. * CODABAR barcodes.
  1767. * Older code often used in library systems, sometimes in blood banks
  1768. * @param $code (string) code to represent.
  1769. * @return array barcode representation.
  1770. * @protected
  1771. */
  1772. protected function barcode_codabar($code) {
  1773. $chr = array(
  1774. '0' => '11111221',
  1775. '1' => '11112211',
  1776. '2' => '11121121',
  1777. '3' => '22111111',
  1778. '4' => '11211211',
  1779. '5' => '21111211',
  1780. '6' => '12111121',
  1781. '7' => '12112111',
  1782. '8' => '12211111',
  1783. '9' => '21121111',
  1784. '-' => '11122111',
  1785. '$' => '11221111',
  1786. ':' => '21112121',
  1787. '/' => '21211121',
  1788. '.' => '21212111',
  1789. '+' => '11222221',
  1790. 'A' => '11221211',
  1791. 'B' => '12121121',
  1792. 'C' => '11121221',
  1793. 'D' => '11122211'
  1794. );
  1795. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1796. $k = 0;
  1797. $w = 0;
  1798. $seq = '';
  1799. $code = 'A'.strtoupper($code).'A';
  1800. $len = strlen($code);
  1801. for ($i = 0; $i < $len; ++$i) {
  1802. if (!isset($chr[$code{$i}])) {
  1803. return false;
  1804. }
  1805. $seq = $chr[$code{$i}];
  1806. for ($j = 0; $j < 8; ++$j) {
  1807. if (($j % 2) == 0) {
  1808. $t = true; // bar
  1809. } else {
  1810. $t = false; // space
  1811. }
  1812. $w = $seq{$j};
  1813. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1814. $bararray['maxw'] += $w;
  1815. ++$k;
  1816. }
  1817. }
  1818. return $bararray;
  1819. }
  1820. /**
  1821. * CODE11 barcodes.
  1822. * Used primarily for labeling telecommunications equipment
  1823. * @param $code (string) code to represent.
  1824. * @return array barcode representation.
  1825. * @protected
  1826. */
  1827. protected function barcode_code11($code) {
  1828. $chr = array(
  1829. '0' => '111121',
  1830. '1' => '211121',
  1831. '2' => '121121',
  1832. '3' => '221111',
  1833. '4' => '112121',
  1834. '5' => '212111',
  1835. '6' => '122111',
  1836. '7' => '111221',
  1837. '8' => '211211',
  1838. '9' => '211111',
  1839. '-' => '112111',
  1840. 'S' => '112211'
  1841. );
  1842. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1843. $k = 0;
  1844. $w = 0;
  1845. $seq = '';
  1846. $len = strlen($code);
  1847. // calculate check digit C
  1848. $p = 1;
  1849. $check = 0;
  1850. for ($i = ($len - 1); $i >= 0; --$i) {
  1851. $digit = $code{$i};
  1852. if ($digit == '-') {
  1853. $dval = 10;
  1854. } else {
  1855. $dval = intval($digit);
  1856. }
  1857. $check += ($dval * $p);
  1858. ++$p;
  1859. if ($p > 10) {
  1860. $p = 1;
  1861. }
  1862. }
  1863. $check %= 11;
  1864. if ($check == 10) {
  1865. $check = '-';
  1866. }
  1867. $code .= $check;
  1868. if ($len > 10) {
  1869. // calculate check digit K
  1870. $p = 1;
  1871. $check = 0;
  1872. for ($i = $len; $i >= 0; --$i) {
  1873. $digit = $code{$i};
  1874. if ($digit == '-') {
  1875. $dval = 10;
  1876. } else {
  1877. $dval = intval($digit);
  1878. }
  1879. $check += ($dval * $p);
  1880. ++$p;
  1881. if ($p > 9) {
  1882. $p = 1;
  1883. }
  1884. }
  1885. $check %= 11;
  1886. $code .= $check;
  1887. ++$len;
  1888. }
  1889. $code = 'S'.$code.'S';
  1890. $len += 3;
  1891. for ($i = 0; $i < $len; ++$i) {
  1892. if (!isset($chr[$code{$i}])) {
  1893. return false;
  1894. }
  1895. $seq = $chr[$code{$i}];
  1896. for ($j = 0; $j < 6; ++$j) {
  1897. if (($j % 2) == 0) {
  1898. $t = true; // bar
  1899. } else {
  1900. $t = false; // space
  1901. }
  1902. $w = $seq{$j};
  1903. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1904. $bararray['maxw'] += $w;
  1905. ++$k;
  1906. }
  1907. }
  1908. return $bararray;
  1909. }
  1910. /**
  1911. * Pharmacode
  1912. * Contains digits (0 to 9)
  1913. * @param $code (string) code to represent.
  1914. * @return array barcode representation.
  1915. * @protected
  1916. */
  1917. protected function barcode_pharmacode($code) {
  1918. $seq = '';
  1919. $code = intval($code);
  1920. while ($code > 0) {
  1921. if (($code % 2) == 0) {
  1922. $seq .= '11100';
  1923. $code -= 2;
  1924. } else {
  1925. $seq .= '100';
  1926. $code -= 1;
  1927. }
  1928. $code /= 2;
  1929. }
  1930. $seq = substr($seq, 0, -2);
  1931. $seq = strrev($seq);
  1932. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1933. return $this->binseq_to_array($seq, $bararray);
  1934. }
  1935. /**
  1936. * Pharmacode two-track
  1937. * Contains digits (0 to 9)
  1938. * @param $code (string) code to represent.
  1939. * @return array barcode representation.
  1940. * @protected
  1941. */
  1942. protected function barcode_pharmacode2t($code) {
  1943. $seq = '';
  1944. $code = intval($code);
  1945. do {
  1946. switch ($code % 3) {
  1947. case 0: {
  1948. $seq .= '3';
  1949. $code = ($code - 3) / 3;
  1950. break;
  1951. }
  1952. case 1: {
  1953. $seq .= '1';
  1954. $code = ($code - 1) / 3;
  1955. break;
  1956. }
  1957. case 2: {
  1958. $seq .= '2';
  1959. $code = ($code - 2) / 3;
  1960. break;
  1961. }
  1962. }
  1963. } while($code != 0);
  1964. $seq = strrev($seq);
  1965. $k = 0;
  1966. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
  1967. $len = strlen($seq);
  1968. for ($i = 0; $i < $len; ++$i) {
  1969. switch ($seq{$i}) {
  1970. case '1': {
  1971. $p = 1;
  1972. $h = 1;
  1973. break;
  1974. }
  1975. case '2': {
  1976. $p = 0;
  1977. $h = 1;
  1978. break;
  1979. }
  1980. case '3': {
  1981. $p = 0;
  1982. $h = 2;
  1983. break;
  1984. }
  1985. }
  1986. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1987. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1988. $bararray['maxw'] += 2;
  1989. }
  1990. unset($bararray['bcode'][($k - 1)]);
  1991. --$bararray['maxw'];
  1992. return $bararray;
  1993. }
  1994. /**
  1995. * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  1996. * (requires PHP bcmath extension)
  1997. * Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
  1998. * The fields are described as follows:<ul><li>The Barcode Identifier shall be assigned by USPS to encode the presort identification that is currently printed in human readable form on the optional endorsement line (OEL) as well as for future USPS use. This shall be two digits, with the second digit in the range of 0–4. The allowable encoding ranges shall be 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and 90–94.</li><li>The Service Type Identifier shall be assigned by USPS for any combination of services requested on the mailpiece. The allowable encoding range shall be 000http://it2.php.net/manual/en/function.dechex.php–999. Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier values.</li><li>The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.</li><li>The Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999 when used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999, 000000000–999999999, and 00000000000–99999999999.</li></ul>
  1999. * @param $code (string) code to print, separate the ZIP (routing code) from the rest using a minus char '-' (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode)
  2000. * @return array barcode representation.
  2001. * @protected
  2002. */
  2003. protected function barcode_imb($code) {
  2004. $asc_chr = array(4,0,2,6,3,5,1,9,8,7,1,2,0,6,4,8,2,9,5,3,0,1,3,7,4,6,8,9,2,0,5,1,9,4,3,8,6,7,1,2,4,3,9,5,7,8,3,0,2,1,4,0,9,1,7,0,2,4,6,3,7,1,9,5,8);
  2005. $dsc_chr = array(7,1,9,5,8,0,2,4,6,3,5,8,9,7,3,0,6,1,7,4,6,8,9,2,5,1,7,5,4,3,8,7,6,0,2,5,4,9,3,0,1,6,8,2,0,4,5,9,6,7,5,2,6,3,8,5,1,9,8,7,4,0,2,6,3);
  2006. $asc_pos = array(3,0,8,11,1,12,8,11,10,6,4,12,2,7,9,6,7,9,2,8,4,0,12,7,10,9,0,7,10,5,7,9,6,8,2,12,1,4,2,0,1,5,4,6,12,1,0,9,4,7,5,10,2,6,9,11,2,12,6,7,5,11,0,3,2);
  2007. $dsc_pos = array(2,10,12,5,9,1,5,4,3,9,11,5,10,1,6,3,4,1,10,0,2,11,8,6,1,12,3,8,6,4,4,11,0,6,1,9,11,5,3,7,3,10,7,11,8,2,10,3,5,8,0,3,12,11,8,4,5,1,3,0,7,12,9,8,10);
  2008. $code_arr = explode('-', $code);
  2009. $tracking_number = $code_arr[0];
  2010. if (isset($code_arr[1])) {
  2011. $routing_code = $code_arr[1];
  2012. } else {
  2013. $routing_code = '';
  2014. }
  2015. // Conversion of Routing Code
  2016. switch (strlen($routing_code)) {
  2017. case 0: {
  2018. $binary_code = 0;
  2019. break;
  2020. }
  2021. case 5: {
  2022. $binary_code = bcadd($routing_code, '1');
  2023. break;
  2024. }
  2025. case 9: {
  2026. $binary_code = bcadd($routing_code, '100001');
  2027. break;
  2028. }
  2029. case 11: {
  2030. $binary_code = bcadd($routing_code, '1000100001');
  2031. break;
  2032. }
  2033. default: {
  2034. return false;
  2035. break;
  2036. }
  2037. }
  2038. $binary_code = bcmul($binary_code, 10);
  2039. $binary_code = bcadd($binary_code, $tracking_number{0});
  2040. $binary_code = bcmul($binary_code, 5);
  2041. $binary_code = bcadd($binary_code, $tracking_number{1});
  2042. $binary_code .= substr($tracking_number, 2, 18);
  2043. // convert to hexadecimal
  2044. $binary_code = $this->dec_to_hex($binary_code);
  2045. // pad to get 13 bytes
  2046. $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT);
  2047. // convert string to array of bytes
  2048. $binary_code_arr = chunk_split($binary_code, 2, "\r");
  2049. $binary_code_arr = substr($binary_code_arr, 0, -1);
  2050. $binary_code_arr = explode("\r", $binary_code_arr);
  2051. // calculate frame check sequence
  2052. $fcs = $this->imb_crc11fcs($binary_code_arr);
  2053. // exclude first 2 bits from first byte
  2054. $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2));
  2055. $binary_code_102bit = $first_byte.substr($binary_code, 2);
  2056. // convert binary data to codewords
  2057. $codewords = array();
  2058. $data = $this->hex_to_dec($binary_code_102bit);
  2059. $codewords[0] = bcmod($data, 636) * 2;
  2060. $data = bcdiv($data, 636);
  2061. for ($i = 1; $i < 9; ++$i) {
  2062. $codewords[$i] = bcmod($data, 1365);
  2063. $data = bcdiv($data, 1365);
  2064. }
  2065. $codewords[9] = $data;
  2066. if (($fcs >> 10) == 1) {
  2067. $codewords[9] += 659;
  2068. }
  2069. // generate lookup tables
  2070. $table2of13 = $this->imb_tables(2, 78);
  2071. $table5of13 = $this->imb_tables(5, 1287);
  2072. // convert codewords to characters
  2073. $characters = array();
  2074. $bitmask = 512;
  2075. foreach($codewords as $k => $val) {
  2076. if ($val <= 1286) {
  2077. $chrcode = $table5of13[$val];
  2078. } else {
  2079. $chrcode = $table2of13[($val - 1287)];
  2080. }
  2081. if (($fcs & $bitmask) > 0) {
  2082. // bitwise invert
  2083. $chrcode = ((~$chrcode) & 8191);
  2084. }
  2085. $characters[] = $chrcode;
  2086. $bitmask /= 2;
  2087. }
  2088. $characters = array_reverse($characters);
  2089. // build bars
  2090. $k = 0;
  2091. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
  2092. for ($i = 0; $i < 65; ++$i) {
  2093. $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0);
  2094. $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0);
  2095. if ($asc AND $dsc) {
  2096. // full bar (F)
  2097. $p = 0;
  2098. $h = 3;
  2099. } elseif ($asc) {
  2100. // ascender (A)
  2101. $p = 0;
  2102. $h = 2;
  2103. } elseif ($dsc) {
  2104. // descender (D)
  2105. $p = 1;
  2106. $h = 2;
  2107. } else {
  2108. // tracker (T)
  2109. $p = 1;
  2110. $h = 1;
  2111. }
  2112. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  2113. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  2114. $bararray['maxw'] += 2;
  2115. }
  2116. unset($bararray['bcode'][($k - 1)]);
  2117. --$bararray['maxw'];
  2118. return $bararray;
  2119. }
  2120. /**
  2121. * Convert large integer number to hexadecimal representation.
  2122. * (requires PHP bcmath extension)
  2123. * @param $number (string) number to convert specified as a string
  2124. * @return string hexadecimal representation
  2125. */
  2126. public function dec_to_hex($number) {
  2127. $i = 0;
  2128. $hex = array();
  2129. if($number == 0) {
  2130. return '00';
  2131. }
  2132. while($number > 0) {
  2133. if($number == 0) {
  2134. array_push($hex, '0');
  2135. } else {
  2136. array_push($hex, strtoupper(dechex(bcmod($number, '16'))));
  2137. $number = bcdiv($number, '16', 0);
  2138. }
  2139. }
  2140. $hex = array_reverse($hex);
  2141. return implode($hex);
  2142. }
  2143. /**
  2144. * Convert large hexadecimal number to decimal representation (string).
  2145. * (requires PHP bcmath extension)
  2146. * @param $hex (string) hexadecimal number to convert specified as a string
  2147. * @return string hexadecimal representation
  2148. */
  2149. public function hex_to_dec($hex) {
  2150. $dec = 0;
  2151. $bitval = 1;
  2152. $len = strlen($hex);
  2153. for($pos = ($len - 1); $pos >= 0; --$pos) {
  2154. $dec = bcadd($dec, bcmul(hexdec($hex{$pos}), $bitval));
  2155. $bitval = bcmul($bitval, 16);
  2156. }
  2157. return $dec;
  2158. }
  2159. /**
  2160. * Intelligent Mail Barcode calculation of Frame Check Sequence
  2161. * @param $code_arr (string) array of hexadecimal values (13 bytes holding 102 bits right justified).
  2162. * @return int 11 bit Frame Check Sequence as integer (decimal base)
  2163. * @protected
  2164. */
  2165. protected function imb_crc11fcs($code_arr) {
  2166. $genpoly = 0x0F35; // generator polynomial
  2167. $fcs = 0x07FF; // Frame Check Sequence
  2168. // do most significant byte skipping the 2 most significant bits
  2169. $data = hexdec($code_arr[0]) << 5;
  2170. for ($bit = 2; $bit < 8; ++$bit) {
  2171. if (($fcs ^ $data) & 0x400) {
  2172. $fcs = ($fcs << 1) ^ $genpoly;
  2173. } else {
  2174. $fcs = ($fcs << 1);
  2175. }
  2176. $fcs &= 0x7FF;
  2177. $data <<= 1;
  2178. }
  2179. // do rest of bytes
  2180. for ($byte = 1; $byte < 13; ++$byte) {
  2181. $data = hexdec($code_arr[$byte]) << 3;
  2182. for ($bit = 0; $bit < 8; ++$bit) {
  2183. if (($fcs ^ $data) & 0x400) {
  2184. $fcs = ($fcs << 1) ^ $genpoly;
  2185. } else {
  2186. $fcs = ($fcs << 1);
  2187. }
  2188. $fcs &= 0x7FF;
  2189. $data <<= 1;
  2190. }
  2191. }
  2192. return $fcs;
  2193. }
  2194. /**
  2195. * Reverse unsigned short value
  2196. * @param $num (int) value to reversr
  2197. * @return int reversed value
  2198. * @protected
  2199. */
  2200. protected function imb_reverse_us($num) {
  2201. $rev = 0;
  2202. for ($i = 0; $i < 16; ++$i) {
  2203. $rev <<= 1;
  2204. $rev |= ($num & 1);
  2205. $num >>= 1;
  2206. }
  2207. return $rev;
  2208. }
  2209. /**
  2210. * generate Nof13 tables used for Intelligent Mail Barcode
  2211. * @param $n (int) is the type of table: 2 for 2of13 table, 5 for 5of13table
  2212. * @param $size (int) size of table (78 for n=2 and 1287 for n=5)
  2213. * @return array requested table
  2214. * @protected
  2215. */
  2216. protected function imb_tables($n, $size) {
  2217. $table = array();
  2218. $lli = 0; // LUT lower index
  2219. $lui = $size - 1; // LUT upper index
  2220. for ($count = 0; $count < 8192; ++$count) {
  2221. $bit_count = 0;
  2222. for ($bit_index = 0; $bit_index < 13; ++$bit_index) {
  2223. $bit_count += intval(($count & (1 << $bit_index)) != 0);
  2224. }
  2225. // if we don't have the right number of bits on, go on to the next value
  2226. if ($bit_count == $n) {
  2227. $reverse = ($this->imb_reverse_us($count) >> 3);
  2228. // if the reverse is less than count, we have already visited this pair before
  2229. if ($reverse >= $count) {
  2230. // If count is symmetric, place it at the first free slot from the end of the list.
  2231. // Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list
  2232. if ($reverse == $count) {
  2233. $table[$lui] = $count;
  2234. --$lui;
  2235. } else {
  2236. $table[$lli] = $count;
  2237. ++$lli;
  2238. $table[$lli] = $reverse;
  2239. ++$lli;
  2240. }
  2241. }
  2242. }
  2243. }
  2244. return $table;
  2245. }
  2246. } // end of class
  2247. //============================================================+
  2248. // END OF FILE
  2249. //============================================================+