tcpdf_barcodes_1d.php 72 KB

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