tcpdf_barcodes_1d.php 70 KB

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