You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2357 lines
72 KiB

  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;
  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 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 $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. * Send 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. * @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 $w (int) Width of a single bar element in pixels.
  177. * @param $h (int) Height of a single bar element in pixels.
  178. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  179. * @return 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 $code (string) code to print
  234. * @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 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 array barcode array
  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 = false;
  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 $code (string) code to represent.
  376. * @param $extended (boolean) if true uses the extended mode.
  377. * @param $checksum (boolean) 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 $code (string) code to represent.
  470. * @return 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 $code (string) code to represent.
  520. * @return 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 $code (string) 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 $code (string) 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 $code (string) 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 $code (string) code to represent.
  741. * @param $checksum (boolean) 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 $code (string) code to represent.
  799. * @param $checksum (boolean) 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 TCPDF barcode array.
  838. * @param $seq (string) barcode as binary sequence.
  839. * @param $bararray (array) barcode array.
  840. * òparam array $bararray TCPDF barcode array to fill up
  841. * @return array barcode representation.
  842. * @protected
  843. */
  844. protected function binseq_to_array($seq, $bararray) {
  845. $len = strlen($seq);
  846. $w = 0;
  847. $k = 0;
  848. for ($i = 0; $i < $len; ++$i) {
  849. $w += 1;
  850. if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq[$i] != $seq[($i+1)]))) {
  851. if ($seq[$i] == '1') {
  852. $t = true; // bar
  853. } else {
  854. $t = false; // space
  855. }
  856. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  857. $bararray['maxw'] += $w;
  858. ++$k;
  859. $w = 0;
  860. }
  861. }
  862. return $bararray;
  863. }
  864. /**
  865. * Interleaved 2 of 5 barcodes.
  866. * Compact numeric code, widely used in industry, air cargo
  867. * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
  868. * @param $code (string) code to represent.
  869. * @param $checksum (boolean) if true add a checksum to the code
  870. * @return array barcode representation.
  871. * @protected
  872. */
  873. protected function barcode_i25($code, $checksum=false) {
  874. $chr['0'] = '11221';
  875. $chr['1'] = '21112';
  876. $chr['2'] = '12112';
  877. $chr['3'] = '22111';
  878. $chr['4'] = '11212';
  879. $chr['5'] = '21211';
  880. $chr['6'] = '12211';
  881. $chr['7'] = '11122';
  882. $chr['8'] = '21121';
  883. $chr['9'] = '12121';
  884. $chr['A'] = '11';
  885. $chr['Z'] = '21';
  886. if ($checksum) {
  887. // add checksum
  888. $code .= $this->checksum_s25($code);
  889. }
  890. if((strlen($code) % 2) != 0) {
  891. // add leading zero if code-length is odd
  892. $code = '0'.$code;
  893. }
  894. // add start and stop codes
  895. $code = 'AA'.strtolower($code).'ZA';
  896. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  897. $k = 0;
  898. $clen = strlen($code);
  899. for ($i = 0; $i < $clen; $i = ($i + 2)) {
  900. $char_bar = $code[$i];
  901. $char_space = $code[$i+1];
  902. if((!isset($chr[$char_bar])) OR (!isset($chr[$char_space]))) {
  903. // invalid character
  904. return false;
  905. }
  906. // create a bar-space sequence
  907. $seq = '';
  908. $chrlen = strlen($chr[$char_bar]);
  909. for ($s = 0; $s < $chrlen; $s++){
  910. $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s];
  911. }
  912. $seqlen = strlen($seq);
  913. for ($j = 0; $j < $seqlen; ++$j) {
  914. if (($j % 2) == 0) {
  915. $t = true; // bar
  916. } else {
  917. $t = false; // space
  918. }
  919. $w = $seq[$j];
  920. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  921. $bararray['maxw'] += $w;
  922. ++$k;
  923. }
  924. }
  925. return $bararray;
  926. }
  927. /**
  928. * C128 barcodes.
  929. * Very capable code, excellent density, high reliability; in very wide use world-wide
  930. * @param $code (string) code to represent.
  931. * @param $type (string) barcode type: A, B, C or empty for automatic switch (AUTO mode)
  932. * @return array barcode representation.
  933. * @protected
  934. */
  935. protected function barcode_c128($code, $type='') {
  936. $chr = array(
  937. '212222', /* 00 */
  938. '222122', /* 01 */
  939. '222221', /* 02 */
  940. '121223', /* 03 */
  941. '121322', /* 04 */
  942. '131222', /* 05 */
  943. '122213', /* 06 */
  944. '122312', /* 07 */
  945. '132212', /* 08 */
  946. '221213', /* 09 */
  947. '221312', /* 10 */
  948. '231212', /* 11 */
  949. '112232', /* 12 */
  950. '122132', /* 13 */
  951. '122231', /* 14 */
  952. '113222', /* 15 */
  953. '123122', /* 16 */
  954. '123221', /* 17 */
  955. '223211', /* 18 */
  956. '221132', /* 19 */
  957. '221231', /* 20 */
  958. '213212', /* 21 */
  959. '223112', /* 22 */
  960. '312131', /* 23 */
  961. '311222', /* 24 */
  962. '321122', /* 25 */
  963. '321221', /* 26 */
  964. '312212', /* 27 */
  965. '322112', /* 28 */
  966. '322211', /* 29 */
  967. '212123', /* 30 */
  968. '212321', /* 31 */
  969. '232121', /* 32 */
  970. '111323', /* 33 */
  971. '131123', /* 34 */
  972. '131321', /* 35 */
  973. '112313', /* 36 */
  974. '132113', /* 37 */
  975. '132311', /* 38 */
  976. '211313', /* 39 */
  977. '231113', /* 40 */
  978. '231311', /* 41 */
  979. '112133', /* 42 */
  980. '112331', /* 43 */
  981. '132131', /* 44 */
  982. '113123', /* 45 */
  983. '113321', /* 46 */
  984. '133121', /* 47 */
  985. '313121', /* 48 */
  986. '211331', /* 49 */
  987. '231131', /* 50 */
  988. '213113', /* 51 */
  989. '213311', /* 52 */
  990. '213131', /* 53 */
  991. '311123', /* 54 */
  992. '311321', /* 55 */
  993. '331121', /* 56 */
  994. '312113', /* 57 */
  995. '312311', /* 58 */
  996. '332111', /* 59 */
  997. '314111', /* 60 */
  998. '221411', /* 61 */
  999. '431111', /* 62 */
  1000. '111224', /* 63 */
  1001. '111422', /* 64 */
  1002. '121124', /* 65 */
  1003. '121421', /* 66 */
  1004. '141122', /* 67 */
  1005. '141221', /* 68 */
  1006. '112214', /* 69 */
  1007. '112412', /* 70 */
  1008. '122114', /* 71 */
  1009. '122411', /* 72 */
  1010. '142112', /* 73 */
  1011. '142211', /* 74 */
  1012. '241211', /* 75 */
  1013. '221114', /* 76 */
  1014. '413111', /* 77 */
  1015. '241112', /* 78 */
  1016. '134111', /* 79 */
  1017. '111242', /* 80 */
  1018. '121142', /* 81 */
  1019. '121241', /* 82 */
  1020. '114212', /* 83 */
  1021. '124112', /* 84 */
  1022. '124211', /* 85 */
  1023. '411212', /* 86 */
  1024. '421112', /* 87 */
  1025. '421211', /* 88 */
  1026. '212141', /* 89 */
  1027. '214121', /* 90 */
  1028. '412121', /* 91 */
  1029. '111143', /* 92 */
  1030. '111341', /* 93 */
  1031. '131141', /* 94 */
  1032. '114113', /* 95 */
  1033. '114311', /* 96 */
  1034. '411113', /* 97 */
  1035. '411311', /* 98 */
  1036. '113141', /* 99 */
  1037. '114131', /* 100 */
  1038. '311141', /* 101 */
  1039. '411131', /* 102 */
  1040. '211412', /* 103 START A */
  1041. '211214', /* 104 START B */
  1042. '211232', /* 105 START C */
  1043. '233111', /* STOP */
  1044. '200000' /* END */
  1045. );
  1046. // ASCII characters for code A (ASCII 00 - 95)
  1047. $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
  1048. $keys_a .= chr(0).chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8).chr(9);
  1049. $keys_a .= chr(10).chr(11).chr(12).chr(13).chr(14).chr(15).chr(16).chr(17).chr(18).chr(19);
  1050. $keys_a .= chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29);
  1051. $keys_a .= chr(30).chr(31);
  1052. // ASCII characters for code B (ASCII 32 - 127)
  1053. $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.chr(127);
  1054. // special codes
  1055. $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);
  1056. $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100);
  1057. // array of symbols
  1058. $code_data = array();
  1059. // length of the code
  1060. $len = strlen($code);
  1061. switch(strtoupper($type)) {
  1062. case 'A': { // MODE A
  1063. $startid = 103;
  1064. for ($i = 0; $i < $len; ++$i) {
  1065. $char = $code[$i];
  1066. $char_id = ord($char);
  1067. if (($char_id >= 241) AND ($char_id <= 244)) {
  1068. $code_data[] = $fnc_a[$char_id];
  1069. } elseif (($char_id >= 0) AND ($char_id <= 95)) {
  1070. $code_data[] = strpos($keys_a, $char);
  1071. } else {
  1072. return false;
  1073. }
  1074. }
  1075. break;
  1076. }
  1077. case 'B': { // MODE B
  1078. $startid = 104;
  1079. for ($i = 0; $i < $len; ++$i) {
  1080. $char = $code[$i];
  1081. $char_id = ord($char);
  1082. if (($char_id >= 241) AND ($char_id <= 244)) {
  1083. $code_data[] = $fnc_b[$char_id];
  1084. } elseif (($char_id >= 32) AND ($char_id <= 127)) {
  1085. $code_data[] = strpos($keys_b, $char);
  1086. } else {
  1087. return false;
  1088. }
  1089. }
  1090. break;
  1091. }
  1092. case 'C': { // MODE C
  1093. $startid = 105;
  1094. if (ord($code[0]) == 241) {
  1095. $code_data[] = 102;
  1096. $code = substr($code, 1);
  1097. --$len;
  1098. }
  1099. if (($len % 2) != 0) {
  1100. // the length must be even
  1101. return false;
  1102. }
  1103. for ($i = 0; $i < $len; $i+=2) {
  1104. $chrnum = $code[$i].$code[$i+1];
  1105. if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
  1106. $code_data[] = intval($chrnum);
  1107. } else {
  1108. return false;
  1109. }
  1110. }
  1111. break;
  1112. }
  1113. default: { // MODE AUTO
  1114. // split code into sequences
  1115. $sequence = array();
  1116. // get numeric sequences (if any)
  1117. $numseq = array();
  1118. preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1119. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1120. $end_offset = 0;
  1121. foreach ($numseq[1] as $val) {
  1122. $offset = $val[1];
  1123. if ($offset > $end_offset) {
  1124. // non numeric sequence
  1125. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset))));
  1126. }
  1127. // numeric sequence
  1128. $slen = strlen($val[0]);
  1129. if (($slen % 2) != 0) {
  1130. // the length must be even
  1131. --$slen;
  1132. }
  1133. $sequence[] = array('C', substr($code, $offset, $slen), $slen);
  1134. $end_offset = $offset + $slen;
  1135. }
  1136. if ($end_offset < $len) {
  1137. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset)));
  1138. }
  1139. } else {
  1140. // text code (non C mode)
  1141. $sequence = array_merge($sequence, $this->get128ABsequence($code));
  1142. }
  1143. // process the sequence
  1144. foreach ($sequence as $key => $seq) {
  1145. switch($seq[0]) {
  1146. case 'A': {
  1147. if ($key == 0) {
  1148. $startid = 103;
  1149. } elseif ($sequence[($key - 1)][0] != 'A') {
  1150. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND (!isset($sequence[($key - 1)][3]))) {
  1151. // single character shift
  1152. $code_data[] = 98;
  1153. // mark shift
  1154. $sequence[$key][3] = true;
  1155. } elseif (!isset($sequence[($key - 1)][3])) {
  1156. $code_data[] = 101;
  1157. }
  1158. }
  1159. for ($i = 0; $i < $seq[2]; ++$i) {
  1160. $char = $seq[1][$i];
  1161. $char_id = ord($char);
  1162. if (($char_id >= 241) AND ($char_id <= 244)) {
  1163. $code_data[] = $fnc_a[$char_id];
  1164. } else {
  1165. $code_data[] = strpos($keys_a, $char);
  1166. }
  1167. }
  1168. break;
  1169. }
  1170. case 'B': {
  1171. if ($key == 0) {
  1172. $tmpchr = ord($seq[1][0]);
  1173. if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) {
  1174. switch ($sequence[($key + 1)][0]) {
  1175. case 'A': {
  1176. $startid = 103;
  1177. $sequence[$key][0] = 'A';
  1178. $code_data[] = $fnc_a[$tmpchr];
  1179. break;
  1180. }
  1181. case 'C': {
  1182. $startid = 105;
  1183. $sequence[$key][0] = 'C';
  1184. $code_data[] = $fnc_a[$tmpchr];
  1185. break;
  1186. }
  1187. }
  1188. break;
  1189. } else {
  1190. $startid = 104;
  1191. }
  1192. } elseif ($sequence[($key - 1)][0] != 'B') {
  1193. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND (!isset($sequence[($key - 1)][3]))) {
  1194. // single character shift
  1195. $code_data[] = 98;
  1196. // mark shift
  1197. $sequence[$key][3] = true;
  1198. } elseif (!isset($sequence[($key - 1)][3])) {
  1199. $code_data[] = 100;
  1200. }
  1201. }
  1202. for ($i = 0; $i < $seq[2]; ++$i) {
  1203. $char = $seq[1][$i];
  1204. $char_id = ord($char);
  1205. if (($char_id >= 241) AND ($char_id <= 244)) {
  1206. $code_data[] = $fnc_b[$char_id];
  1207. } else {
  1208. $code_data[] = strpos($keys_b, $char);
  1209. }
  1210. }
  1211. break;
  1212. }
  1213. case 'C': {
  1214. if ($key == 0) {
  1215. $startid = 105;
  1216. } elseif ($sequence[($key - 1)][0] != 'C') {
  1217. $code_data[] = 99;
  1218. }
  1219. for ($i = 0; $i < $seq[2]; $i+=2) {
  1220. $chrnum = $seq[1][$i].$seq[1][$i+1];
  1221. $code_data[] = intval($chrnum);
  1222. }
  1223. break;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. // calculate check character
  1230. $sum = $startid;
  1231. foreach ($code_data as $key => $val) {
  1232. $sum += ($val * ($key + 1));
  1233. }
  1234. // add check character
  1235. $code_data[] = ($sum % 103);
  1236. // add stop sequence
  1237. $code_data[] = 106;
  1238. $code_data[] = 107;
  1239. // add start code at the beginning
  1240. array_unshift($code_data, $startid);
  1241. // build barcode array
  1242. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1243. foreach ($code_data as $val) {
  1244. $seq = $chr[$val];
  1245. for ($j = 0; $j < 6; ++$j) {
  1246. if (($j % 2) == 0) {
  1247. $t = true; // bar
  1248. } else {
  1249. $t = false; // space
  1250. }
  1251. $w = $seq[$j];
  1252. $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1253. $bararray['maxw'] += $w;
  1254. }
  1255. }
  1256. return $bararray;
  1257. }
  1258. /**
  1259. * Split text code in A/B sequence for 128 code
  1260. * @param $code (string) code to split.
  1261. * @return array sequence
  1262. * @protected
  1263. */
  1264. protected function get128ABsequence($code) {
  1265. $len = strlen($code);
  1266. $sequence = array();
  1267. // get A sequences (if any)
  1268. $numseq = array();
  1269. preg_match_all('/([\0-\31])/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1270. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1271. $end_offset = 0;
  1272. foreach ($numseq[1] as $val) {
  1273. $offset = $val[1];
  1274. if ($offset > $end_offset) {
  1275. // B sequence
  1276. $sequence[] = array('B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset));
  1277. }
  1278. // A sequence
  1279. $slen = strlen($val[0]);
  1280. $sequence[] = array('A', substr($code, $offset, $slen), $slen);
  1281. $end_offset = $offset + $slen;
  1282. }
  1283. if ($end_offset < $len) {
  1284. $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
  1285. }
  1286. } else {
  1287. // only B sequence
  1288. $sequence[] = array('B', $code, $len);
  1289. }
  1290. return $sequence;
  1291. }
  1292. /**
  1293. * EAN13 and UPC-A barcodes.
  1294. * EAN13: European Article Numbering international retail product code
  1295. * UPC-A: Universal product code seen on almost all retail products in the USA and Canada
  1296. * UPC-E: Short version of UPC symbol
  1297. * @param $code (string) code to represent.
  1298. * @param $len (string) barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A
  1299. * @return array barcode representation.
  1300. * @protected
  1301. */
  1302. protected function barcode_eanupc($code, $len=13) {
  1303. $upce = false;
  1304. if ($len == 6) {
  1305. $len = 12; // UPC-A
  1306. $upce = true; // UPC-E mode
  1307. }
  1308. $data_len = $len - 1;
  1309. //Padding
  1310. $code = str_pad($code, $data_len, '0', STR_PAD_LEFT);
  1311. $code_len = strlen($code);
  1312. // calculate check digit
  1313. $sum_a = 0;
  1314. for ($i = 1; $i < $data_len; $i+=2) {
  1315. $sum_a += $code[$i];
  1316. }
  1317. if ($len > 12) {
  1318. $sum_a *= 3;
  1319. }
  1320. $sum_b = 0;
  1321. for ($i = 0; $i < $data_len; $i+=2) {
  1322. $sum_b += ($code[$i]);
  1323. }
  1324. if ($len < 13) {
  1325. $sum_b *= 3;
  1326. }
  1327. $r = ($sum_a + $sum_b) % 10;
  1328. if($r > 0) {
  1329. $r = (10 - $r);
  1330. }
  1331. if ($code_len == $data_len) {
  1332. // add check digit
  1333. $code .= $r;
  1334. } elseif ($r !== intval($code[$data_len])) {
  1335. // wrong checkdigit
  1336. return false;
  1337. }
  1338. if ($len == 12) {
  1339. // UPC-A
  1340. $code = '0'.$code;
  1341. ++$len;
  1342. }
  1343. if ($upce) {
  1344. // convert UPC-A to UPC-E
  1345. $tmp = substr($code, 4, 3);
  1346. if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) {
  1347. // manufacturer code ends in 000, 100, or 200
  1348. $upce_code = substr($code, 2, 2).substr($code, 9, 3).substr($code, 4, 1);
  1349. } else {
  1350. $tmp = substr($code, 5, 2);
  1351. if ($tmp == '00') {
  1352. // manufacturer code ends in 00
  1353. $upce_code = substr($code, 2, 3).substr($code, 10, 2).'3';
  1354. } else {
  1355. $tmp = substr($code, 6, 1);
  1356. if ($tmp == '0') {
  1357. // manufacturer code ends in 0
  1358. $upce_code = substr($code, 2, 4).substr($code, 11, 1).'4';
  1359. } else {
  1360. // manufacturer code does not end in zero
  1361. $upce_code = substr($code, 2, 5).substr($code, 11, 1);
  1362. }
  1363. }
  1364. }
  1365. }
  1366. //Convert digits to bars
  1367. $codes = array(
  1368. 'A'=>array( // left odd parity
  1369. '0'=>'0001101',
  1370. '1'=>'0011001',
  1371. '2'=>'0010011',
  1372. '3'=>'0111101',
  1373. '4'=>'0100011',
  1374. '5'=>'0110001',
  1375. '6'=>'0101111',
  1376. '7'=>'0111011',
  1377. '8'=>'0110111',
  1378. '9'=>'0001011'),
  1379. 'B'=>array( // left even parity
  1380. '0'=>'0100111',
  1381. '1'=>'0110011',
  1382. '2'=>'0011011',
  1383. '3'=>'0100001',
  1384. '4'=>'0011101',
  1385. '5'=>'0111001',
  1386. '6'=>'0000101',
  1387. '7'=>'0010001',
  1388. '8'=>'0001001',
  1389. '9'=>'0010111'),
  1390. 'C'=>array( // right
  1391. '0'=>'1110010',
  1392. '1'=>'1100110',
  1393. '2'=>'1101100',
  1394. '3'=>'1000010',
  1395. '4'=>'1011100',
  1396. '5'=>'1001110',
  1397. '6'=>'1010000',
  1398. '7'=>'1000100',
  1399. '8'=>'1001000',
  1400. '9'=>'1110100')
  1401. );
  1402. $parities = array(
  1403. '0'=>array('A','A','A','A','A','A'),
  1404. '1'=>array('A','A','B','A','B','B'),
  1405. '2'=>array('A','A','B','B','A','B'),
  1406. '3'=>array('A','A','B','B','B','A'),
  1407. '4'=>array('A','B','A','A','B','B'),
  1408. '5'=>array('A','B','B','A','A','B'),
  1409. '6'=>array('A','B','B','B','A','A'),
  1410. '7'=>array('A','B','A','B','A','B'),
  1411. '8'=>array('A','B','A','B','B','A'),
  1412. '9'=>array('A','B','B','A','B','A')
  1413. );
  1414. $upce_parities = array();
  1415. $upce_parities[0] = array(
  1416. '0'=>array('B','B','B','A','A','A'),
  1417. '1'=>array('B','B','A','B','A','A'),
  1418. '2'=>array('B','B','A','A','B','A'),
  1419. '3'=>array('B','B','A','A','A','B'),
  1420. '4'=>array('B','A','B','B','A','A'),
  1421. '5'=>array('B','A','A','B','B','A'),
  1422. '6'=>array('B','A','A','A','B','B'),
  1423. '7'=>array('B','A','B','A','B','A'),
  1424. '8'=>array('B','A','B','A','A','B'),
  1425. '9'=>array('B','A','A','B','A','B')
  1426. );
  1427. $upce_parities[1] = array(
  1428. '0'=>array('A','A','A','B','B','B'),
  1429. '1'=>array('A','A','B','A','B','B'),
  1430. '2'=>array('A','A','B','B','A','B'),
  1431. '3'=>array('A','A','B','B','B','A'),
  1432. '4'=>array('A','B','A','A','B','B'),
  1433. '5'=>array('A','B','B','A','A','B'),
  1434. '6'=>array('A','B','B','B','A','A'),
  1435. '7'=>array('A','B','A','B','A','B'),
  1436. '8'=>array('A','B','A','B','B','A'),
  1437. '9'=>array('A','B','B','A','B','A')
  1438. );
  1439. $k = 0;
  1440. $seq = '101'; // left guard bar
  1441. if ($upce) {
  1442. $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1443. $p = $upce_parities[$code[1]][$r];
  1444. for ($i = 0; $i < 6; ++$i) {
  1445. $seq .= $codes[$p[$i]][$upce_code[$i]];
  1446. }
  1447. $seq .= '010101'; // right guard bar
  1448. } else {
  1449. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1450. $half_len = intval(ceil($len / 2));
  1451. if ($len == 8) {
  1452. for ($i = 0; $i < $half_len; ++$i) {
  1453. $seq .= $codes['A'][$code[$i]];
  1454. }
  1455. } else {
  1456. $p = $parities[$code[0]];
  1457. for ($i = 1; $i < $half_len; ++$i) {
  1458. $seq .= $codes[$p[$i-1]][$code[$i]];
  1459. }
  1460. }
  1461. $seq .= '01010'; // center guard bar
  1462. for ($i = $half_len; $i < $len; ++$i) {
  1463. $seq .= $codes['C'][$code[$i]];
  1464. }
  1465. $seq .= '101'; // right guard bar
  1466. }
  1467. $clen = strlen($seq);
  1468. $w = 0;
  1469. for ($i = 0; $i < $clen; ++$i) {
  1470. $w += 1;
  1471. if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq[$i] != $seq[$i+1]))) {
  1472. if ($seq[$i] == '1') {
  1473. $t = true; // bar
  1474. } else {
  1475. $t = false; // space
  1476. }
  1477. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1478. $bararray['maxw'] += $w;
  1479. ++$k;
  1480. $w = 0;
  1481. }
  1482. }
  1483. return $bararray;
  1484. }
  1485. /**
  1486. * UPC-Based Extensions
  1487. * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
  1488. * 5-Digit Ext.: Used to mark suggested retail price of books
  1489. * @param $code (string) code to represent.
  1490. * @param $len (string) barcode type: 2 = 2-Digit, 5 = 5-Digit
  1491. * @return array barcode representation.
  1492. * @protected
  1493. */
  1494. protected function barcode_eanext($code, $len=5) {
  1495. //Padding
  1496. $code = str_pad($code, $len, '0', STR_PAD_LEFT);
  1497. // calculate check digit
  1498. if ($len == 2) {
  1499. $r = $code % 4;
  1500. } elseif ($len == 5) {
  1501. $r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3]));
  1502. $r %= 10;
  1503. } else {
  1504. return false;
  1505. }
  1506. //Convert digits to bars
  1507. $codes = array(
  1508. 'A'=>array( // left odd parity
  1509. '0'=>'0001101',
  1510. '1'=>'0011001',
  1511. '2'=>'0010011',
  1512. '3'=>'0111101',
  1513. '4'=>'0100011',
  1514. '5'=>'0110001',
  1515. '6'=>'0101111',
  1516. '7'=>'0111011',
  1517. '8'=>'0110111',
  1518. '9'=>'0001011'),
  1519. 'B'=>array( // left even parity
  1520. '0'=>'0100111',
  1521. '1'=>'0110011',
  1522. '2'=>'0011011',
  1523. '3'=>'0100001',
  1524. '4'=>'0011101',
  1525. '5'=>'0111001',
  1526. '6'=>'0000101',
  1527. '7'=>'0010001',
  1528. '8'=>'0001001',
  1529. '9'=>'0010111')
  1530. );
  1531. $parities = array();
  1532. $parities[2] = array(
  1533. '0'=>array('A','A'),
  1534. '1'=>array('A','B'),
  1535. '2'=>array('B','A'),
  1536. '3'=>array('B','B')
  1537. );
  1538. $parities[5] = array(
  1539. '0'=>array('B','B','A','A','A'),
  1540. '1'=>array('B','A','B','A','A'),
  1541. '2'=>array('B','A','A','B','A'),
  1542. '3'=>array('B','A','A','A','B'),
  1543. '4'=>array('A','B','B','A','A'),
  1544. '5'=>array('A','A','B','B','A'),
  1545. '6'=>array('A','A','A','B','B'),
  1546. '7'=>array('A','B','A','B','A'),
  1547. '8'=>array('A','B','A','A','B'),
  1548. '9'=>array('A','A','B','A','B')
  1549. );
  1550. $p = $parities[$len][$r];
  1551. $seq = '1011'; // left guard bar
  1552. $seq .= $codes[$p[0]][$code[0]];
  1553. for ($i = 1; $i < $len; ++$i) {
  1554. $seq .= '01'; // separator
  1555. $seq .= $codes[$p[$i]][$code[$i]];
  1556. }
  1557. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1558. return $this->binseq_to_array($seq, $bararray);
  1559. }
  1560. /**
  1561. * POSTNET and PLANET barcodes.
  1562. * Used by U.S. Postal Service for automated mail sorting
  1563. * @param $code (string) zip code to represent. Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD.
  1564. * @param $planet (boolean) if true print the PLANET barcode, otherwise print POSTNET
  1565. * @return array barcode representation.
  1566. * @protected
  1567. */
  1568. protected function barcode_postnet($code, $planet=false) {
  1569. // bar length
  1570. if ($planet) {
  1571. $barlen = Array(
  1572. 0 => Array(1,1,2,2,2),
  1573. 1 => Array(2,2,2,1,1),
  1574. 2 => Array(2,2,1,2,1),
  1575. 3 => Array(2,2,1,1,2),
  1576. 4 => Array(2,1,2,2,1),
  1577. 5 => Array(2,1,2,1,2),
  1578. 6 => Array(2,1,1,2,2),
  1579. 7 => Array(1,2,2,2,1),
  1580. 8 => Array(1,2,2,1,2),
  1581. 9 => Array(1,2,1,2,2)
  1582. );
  1583. } else {
  1584. $barlen = Array(
  1585. 0 => Array(2,2,1,1,1),
  1586. 1 => Array(1,1,1,2,2),
  1587. 2 => Array(1,1,2,1,2),
  1588. 3 => Array(1,1,2,2,1),
  1589. 4 => Array(1,2,1,1,2),
  1590. 5 => Array(1,2,1,2,1),
  1591. 6 => Array(1,2,2,1,1),
  1592. 7 => Array(2,1,1,1,2),
  1593. 8 => Array(2,1,1,2,1),
  1594. 9 => Array(2,1,2,1,1)
  1595. );
  1596. }
  1597. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
  1598. $k = 0;
  1599. $code = str_replace('-', '', $code);
  1600. $code = str_replace(' ', '', $code);
  1601. $len = strlen($code);
  1602. // calculate checksum
  1603. $sum = 0;
  1604. for ($i = 0; $i < $len; ++$i) {
  1605. $sum += intval($code[$i]);
  1606. }
  1607. $chkd = ($sum % 10);
  1608. if($chkd > 0) {
  1609. $chkd = (10 - $chkd);
  1610. }
  1611. $code .= $chkd;
  1612. $len = strlen($code);
  1613. // start bar
  1614. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1615. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1616. $bararray['maxw'] += 2;
  1617. for ($i = 0; $i < $len; ++$i) {
  1618. for ($j = 0; $j < 5; ++$j) {
  1619. $h = $barlen[$code[$i]][$j];
  1620. $p = floor(1 / $h);
  1621. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1622. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1623. $bararray['maxw'] += 2;
  1624. }
  1625. }
  1626. // end bar
  1627. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1628. $bararray['maxw'] += 1;
  1629. return $bararray;
  1630. }
  1631. /**
  1632. * RMS4CC - CBC - KIX
  1633. * RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index)
  1634. * RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service.
  1635. * @param $code (string) code to print
  1636. * @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.
  1637. * @return array barcode representation.
  1638. * @protected
  1639. */
  1640. protected function barcode_rms4cc($code, $kix=false) {
  1641. $notkix = !$kix;
  1642. // bar mode
  1643. // 1 = pos 1, length 2
  1644. // 2 = pos 1, length 3
  1645. // 3 = pos 2, length 1
  1646. // 4 = pos 2, length 2
  1647. $barmode = array(
  1648. '0' => array(3,3,2,2),
  1649. '1' => array(3,4,1,2),
  1650. '2' => array(3,4,2,1),
  1651. '3' => array(4,3,1,2),
  1652. '4' => array(4,3,2,1),
  1653. '5' => array(4,4,1,1),
  1654. '6' => array(3,1,4,2),
  1655. '7' => array(3,2,3,2),
  1656. '8' => array(3,2,4,1),
  1657. '9' => array(4,1,3,2),
  1658. 'A' => array(4,1,4,1),
  1659. 'B' => array(4,2,3,1),
  1660. 'C' => array(3,1,2,4),
  1661. 'D' => array(3,2,1,4),
  1662. 'E' => array(3,2,2,3),
  1663. 'F' => array(4,1,1,4),
  1664. 'G' => array(4,1,2,3),
  1665. 'H' => array(4,2,1,3),
  1666. 'I' => array(1,3,4,2),
  1667. 'J' => array(1,4,3,2),
  1668. 'K' => array(1,4,4,1),
  1669. 'L' => array(2,3,3,2),
  1670. 'M' => array(2,3,4,1),
  1671. 'N' => array(2,4,3,1),
  1672. 'O' => array(1,3,2,4),
  1673. 'P' => array(1,4,1,4),
  1674. 'Q' => array(1,4,2,3),
  1675. 'R' => array(2,3,1,4),
  1676. 'S' => array(2,3,2,3),
  1677. 'T' => array(2,4,1,3),
  1678. 'U' => array(1,1,4,4),
  1679. 'V' => array(1,2,3,4),
  1680. 'W' => array(1,2,4,3),
  1681. 'X' => array(2,1,3,4),
  1682. 'Y' => array(2,1,4,3),
  1683. 'Z' => array(2,2,3,3)
  1684. );
  1685. $code = strtoupper($code);
  1686. $len = strlen($code);
  1687. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
  1688. if ($notkix) {
  1689. // table for checksum calculation (row,col)
  1690. $checktable = array(
  1691. '0' => array(1,1),
  1692. '1' => array(1,2),
  1693. '2' => array(1,3),
  1694. '3' => array(1,4),
  1695. '4' => array(1,5),
  1696. '5' => array(1,0),
  1697. '6' => array(2,1),
  1698. '7' => array(2,2),
  1699. '8' => array(2,3),
  1700. '9' => array(2,4),
  1701. 'A' => array(2,5),
  1702. 'B' => array(2,0),
  1703. 'C' => array(3,1),
  1704. 'D' => array(3,2),
  1705. 'E' => array(3,3),
  1706. 'F' => array(3,4),
  1707. 'G' => array(3,5),
  1708. 'H' => array(3,0),
  1709. 'I' => array(4,1),
  1710. 'J' => array(4,2),
  1711. 'K' => array(4,3),
  1712. 'L' => array(4,4),
  1713. 'M' => array(4,5),
  1714. 'N' => array(4,0),
  1715. 'O' => array(5,1),
  1716. 'P' => array(5,2),
  1717. 'Q' => array(5,3),
  1718. 'R' => array(5,4),
  1719. 'S' => array(5,5),
  1720. 'T' => array(5,0),
  1721. 'U' => array(0,1),
  1722. 'V' => array(0,2),
  1723. 'W' => array(0,3),
  1724. 'X' => array(0,4),
  1725. 'Y' => array(0,5),
  1726. 'Z' => array(0,0)
  1727. );
  1728. $row = 0;
  1729. $col = 0;
  1730. for ($i = 0; $i < $len; ++$i) {
  1731. $row += $checktable[$code[$i]][0];
  1732. $col += $checktable[$code[$i]][1];
  1733. }
  1734. $row %= 6;
  1735. $col %= 6;
  1736. $chk = array_keys($checktable, array($row,$col));
  1737. $code .= $chk[0];
  1738. ++$len;
  1739. }
  1740. $k = 0;
  1741. if ($notkix) {
  1742. // start bar
  1743. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
  1744. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1745. $bararray['maxw'] += 2;
  1746. }
  1747. for ($i = 0; $i < $len; ++$i) {
  1748. for ($j = 0; $j < 4; ++$j) {
  1749. switch ($barmode[$code[$i]][$j]) {
  1750. case 1: {
  1751. $p = 0;
  1752. $h = 2;
  1753. break;
  1754. }
  1755. case 2: {
  1756. $p = 0;
  1757. $h = 3;
  1758. break;
  1759. }
  1760. case 3: {
  1761. $p = 1;
  1762. $h = 1;
  1763. break;
  1764. }
  1765. case 4: {
  1766. $p = 1;
  1767. $h = 2;
  1768. break;
  1769. }
  1770. }
  1771. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1772. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  1773. $bararray['maxw'] += 2;
  1774. }
  1775. }
  1776. if ($notkix) {
  1777. // stop bar
  1778. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0);
  1779. $bararray['maxw'] += 1;
  1780. }
  1781. return $bararray;
  1782. }
  1783. /**
  1784. * CODABAR barcodes.
  1785. * Older code often used in library systems, sometimes in blood banks
  1786. * @param $code (string) code to represent.
  1787. * @return array barcode representation.
  1788. * @protected
  1789. */
  1790. protected function barcode_codabar($code) {
  1791. $chr = array(
  1792. '0' => '11111221',
  1793. '1' => '11112211',
  1794. '2' => '11121121',
  1795. '3' => '22111111',
  1796. '4' => '11211211',
  1797. '5' => '21111211',
  1798. '6' => '12111121',
  1799. '7' => '12112111',
  1800. '8' => '12211111',
  1801. '9' => '21121111',
  1802. '-' => '11122111',
  1803. '$' => '11221111',
  1804. ':' => '21112121',
  1805. '/' => '21211121',
  1806. '.' => '21212111',
  1807. '+' => '11222221',
  1808. 'A' => '11221211',
  1809. 'B' => '12121121',
  1810. 'C' => '11121221',
  1811. 'D' => '11122211'
  1812. );
  1813. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1814. $k = 0;
  1815. $w = 0;
  1816. $seq = '';
  1817. $code = 'A'.strtoupper($code).'A';
  1818. $len = strlen($code);
  1819. for ($i = 0; $i < $len; ++$i) {
  1820. if (!isset($chr[$code[$i]])) {
  1821. return false;
  1822. }
  1823. $seq = $chr[$code[$i]];
  1824. for ($j = 0; $j < 8; ++$j) {
  1825. if (($j % 2) == 0) {
  1826. $t = true; // bar
  1827. } else {
  1828. $t = false; // space
  1829. }
  1830. $w = $seq[$j];
  1831. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1832. $bararray['maxw'] += $w;
  1833. ++$k;
  1834. }
  1835. }
  1836. return $bararray;
  1837. }
  1838. /**
  1839. * CODE11 barcodes.
  1840. * Used primarily for labeling telecommunications equipment
  1841. * @param $code (string) code to represent.
  1842. * @return array barcode representation.
  1843. * @protected
  1844. */
  1845. protected function barcode_code11($code) {
  1846. $chr = array(
  1847. '0' => '111121',
  1848. '1' => '211121',
  1849. '2' => '121121',
  1850. '3' => '221111',
  1851. '4' => '112121',
  1852. '5' => '212111',
  1853. '6' => '122111',
  1854. '7' => '111221',
  1855. '8' => '211211',
  1856. '9' => '211111',
  1857. '-' => '112111',
  1858. 'S' => '112211'
  1859. );
  1860. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1861. $k = 0;
  1862. $w = 0;
  1863. $seq = '';
  1864. $len = strlen($code);
  1865. // calculate check digit C
  1866. $p = 1;
  1867. $check = 0;
  1868. for ($i = ($len - 1); $i >= 0; --$i) {
  1869. $digit = $code[$i];
  1870. if ($digit == '-') {
  1871. $dval = 10;
  1872. } else {
  1873. $dval = intval($digit);
  1874. }
  1875. $check += ($dval * $p);
  1876. ++$p;
  1877. if ($p > 10) {
  1878. $p = 1;
  1879. }
  1880. }
  1881. $check %= 11;
  1882. if ($check == 10) {
  1883. $check = '-';
  1884. }
  1885. $code .= $check;
  1886. if ($len > 10) {
  1887. // calculate check digit K
  1888. $p = 1;
  1889. $check = 0;
  1890. for ($i = $len; $i >= 0; --$i) {
  1891. $digit = $code[$i];
  1892. if ($digit == '-') {
  1893. $dval = 10;
  1894. } else {
  1895. $dval = intval($digit);
  1896. }
  1897. $check += ($dval * $p);
  1898. ++$p;
  1899. if ($p > 9) {
  1900. $p = 1;
  1901. }
  1902. }
  1903. $check %= 11;
  1904. $code .= $check;
  1905. ++$len;
  1906. }
  1907. $code = 'S'.$code.'S';
  1908. $len += 3;
  1909. for ($i = 0; $i < $len; ++$i) {
  1910. if (!isset($chr[$code[$i]])) {
  1911. return false;
  1912. }
  1913. $seq = $chr[$code[$i]];
  1914. for ($j = 0; $j < 6; ++$j) {
  1915. if (($j % 2) == 0) {
  1916. $t = true; // bar
  1917. } else {
  1918. $t = false; // space
  1919. }
  1920. $w = $seq[$j];
  1921. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1922. $bararray['maxw'] += $w;
  1923. ++$k;
  1924. }
  1925. }
  1926. return $bararray;
  1927. }
  1928. /**
  1929. * Pharmacode
  1930. * Contains digits (0 to 9)
  1931. * @param $code (string) code to represent.
  1932. * @return array barcode representation.
  1933. * @protected
  1934. */
  1935. protected function barcode_pharmacode($code) {
  1936. $seq = '';
  1937. $code = intval($code);
  1938. while ($code > 0) {
  1939. if (($code % 2) == 0) {
  1940. $seq .= '11100';
  1941. $code -= 2;
  1942. } else {
  1943. $seq .= '100';
  1944. $code -= 1;
  1945. }
  1946. $code /= 2;
  1947. }
  1948. $seq = substr($seq, 0, -2);
  1949. $seq = strrev($seq);
  1950. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1951. return $this->binseq_to_array($seq, $bararray);
  1952. }
  1953. /**
  1954. * Pharmacode two-track
  1955. * Contains digits (0 to 9)
  1956. * @param $code (string) code to represent.
  1957. * @return array barcode representation.
  1958. * @protected
  1959. */
  1960. protected function barcode_pharmacode2t($code) {
  1961. $seq = '';
  1962. $code = intval($code);
  1963. do {
  1964. switch ($code % 3) {
  1965. case 0: {
  1966. $seq .= '3';
  1967. $code = ($code - 3) / 3;
  1968. break;
  1969. }
  1970. case 1: {
  1971. $seq .= '1';
  1972. $code = ($code - 1) / 3;
  1973. break;
  1974. }
  1975. case 2: {
  1976. $seq .= '2';
  1977. $code = ($code - 2) / 3;
  1978. break;
  1979. }
  1980. }
  1981. } while($code != 0);
  1982. $seq = strrev($seq);
  1983. $k = 0;
  1984. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
  1985. $len = strlen($seq);
  1986. for ($i = 0; $i < $len; ++$i) {
  1987. switch ($seq[$i]) {
  1988. case '1': {
  1989. $p = 1;
  1990. $h = 1;
  1991. break;
  1992. }
  1993. case '2': {
  1994. $p = 0;
  1995. $h = 1;
  1996. break;
  1997. }
  1998. case '3': {
  1999. $p = 0;
  2000. $h = 2;
  2001. break;
  2002. }
  2003. }
  2004. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  2005. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  2006. $bararray['maxw'] += 2;
  2007. }
  2008. unset($bararray['bcode'][($k - 1)]);
  2009. --$bararray['maxw'];
  2010. return $bararray;
  2011. }
  2012. /**
  2013. * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  2014. * (requires PHP bcmath extension)
  2015. * Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
  2016. * 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>
  2017. * @param $code (string) code to print, separate the ZIP (routing code) from the rest using a minus char '-' (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode)
  2018. * @return array barcode representation.
  2019. * @protected
  2020. */
  2021. protected function barcode_imb($code) {
  2022. $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);
  2023. $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);
  2024. $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);
  2025. $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);
  2026. $code_arr = explode('-', $code);
  2027. $tracking_number = $code_arr[0];
  2028. if (isset($code_arr[1])) {
  2029. $routing_code = $code_arr[1];
  2030. } else {
  2031. $routing_code = '';
  2032. }
  2033. // Conversion of Routing Code
  2034. switch (strlen($routing_code)) {
  2035. case 0: {
  2036. $binary_code = 0;
  2037. break;
  2038. }
  2039. case 5: {
  2040. $binary_code = bcadd($routing_code, '1');
  2041. break;
  2042. }
  2043. case 9: {
  2044. $binary_code = bcadd($routing_code, '100001');
  2045. break;
  2046. }
  2047. case 11: {
  2048. $binary_code = bcadd($routing_code, '1000100001');
  2049. break;
  2050. }
  2051. default: {
  2052. return false;
  2053. break;
  2054. }
  2055. }
  2056. $binary_code = bcmul($binary_code, 10);
  2057. $binary_code = bcadd($binary_code, $tracking_number[0]);
  2058. $binary_code = bcmul($binary_code, 5);
  2059. $binary_code = bcadd($binary_code, $tracking_number[1]);
  2060. $binary_code .= substr($tracking_number, 2, 18);
  2061. // convert to hexadecimal
  2062. $binary_code = $this->dec_to_hex($binary_code);
  2063. // pad to get 13 bytes
  2064. $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT);
  2065. // convert string to array of bytes
  2066. $binary_code_arr = chunk_split($binary_code, 2, "\r");
  2067. $binary_code_arr = substr($binary_code_arr, 0, -1);
  2068. $binary_code_arr = explode("\r", $binary_code_arr);
  2069. // calculate frame check sequence
  2070. $fcs = $this->imb_crc11fcs($binary_code_arr);
  2071. // exclude first 2 bits from first byte
  2072. $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2));
  2073. $binary_code_102bit = $first_byte.substr($binary_code, 2);
  2074. // convert binary data to codewords
  2075. $codewords = array();
  2076. $data = $this->hex_to_dec($binary_code_102bit);
  2077. $codewords[0] = bcmod($data, 636) * 2;
  2078. $data = bcdiv($data, 636);
  2079. for ($i = 1; $i < 9; ++$i) {
  2080. $codewords[$i] = bcmod($data, 1365);
  2081. $data = bcdiv($data, 1365);
  2082. }
  2083. $codewords[9] = $data;
  2084. if (($fcs >> 10) == 1) {
  2085. $codewords[9] += 659;
  2086. }
  2087. // generate lookup tables
  2088. $table2of13 = $this->imb_tables(2, 78);
  2089. $table5of13 = $this->imb_tables(5, 1287);
  2090. // convert codewords to characters
  2091. $characters = array();
  2092. $bitmask = 512;
  2093. foreach($codewords as $k => $val) {
  2094. if ($val <= 1286) {
  2095. $chrcode = $table5of13[$val];
  2096. } else {
  2097. $chrcode = $table2of13[($val - 1287)];
  2098. }
  2099. if (($fcs & $bitmask) > 0) {
  2100. // bitwise invert
  2101. $chrcode = ((~$chrcode) & 8191);
  2102. }
  2103. $characters[] = $chrcode;
  2104. $bitmask /= 2;
  2105. }
  2106. $characters = array_reverse($characters);
  2107. // build bars
  2108. $k = 0;
  2109. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
  2110. for ($i = 0; $i < 65; ++$i) {
  2111. $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0);
  2112. $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0);
  2113. if ($asc AND $dsc) {
  2114. // full bar (F)
  2115. $p = 0;
  2116. $h = 3;
  2117. } elseif ($asc) {
  2118. // ascender (A)
  2119. $p = 0;
  2120. $h = 2;
  2121. } elseif ($dsc) {
  2122. // descender (D)
  2123. $p = 1;
  2124. $h = 2;
  2125. } else {
  2126. // tracker (T)
  2127. $p = 1;
  2128. $h = 1;
  2129. }
  2130. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  2131. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  2132. $bararray['maxw'] += 2;
  2133. }
  2134. unset($bararray['bcode'][($k - 1)]);
  2135. --$bararray['maxw'];
  2136. return $bararray;
  2137. }
  2138. /**
  2139. * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  2140. *
  2141. * @param $code (string) pre-formatted IMB barcode (65 chars "FADT")
  2142. * @return array barcode representation.
  2143. * @protected
  2144. */
  2145. protected function barcode_imb_pre($code) {
  2146. if (!preg_match('/^[fadtFADT]{65}$/', $code) == 1) {
  2147. return false;
  2148. }
  2149. $characters = str_split(strtolower($code), 1);
  2150. // build bars
  2151. $k = 0;
  2152. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
  2153. for ($i = 0; $i < 65; ++$i) {
  2154. switch($characters[$i]) {
  2155. case 'f': {
  2156. // full bar
  2157. $p = 0;
  2158. $h = 3;
  2159. break;
  2160. }
  2161. case 'a': {
  2162. // ascender
  2163. $p = 0;
  2164. $h = 2;
  2165. break;
  2166. }
  2167. case 'd': {
  2168. // descender
  2169. $p = 1;
  2170. $h = 2;
  2171. break;
  2172. }
  2173. case 't': {
  2174. // tracker (short)
  2175. $p = 1;
  2176. $h = 1;
  2177. break;
  2178. }
  2179. }
  2180. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  2181. $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
  2182. $bararray['maxw'] += 2;
  2183. }
  2184. unset($bararray['bcode'][($k - 1)]);
  2185. --$bararray['maxw'];
  2186. return $bararray;
  2187. }
  2188. /**
  2189. * Convert large integer number to hexadecimal representation.
  2190. * (requires PHP bcmath extension)
  2191. * @param $number (string) number to convert specified as a string
  2192. * @return string hexadecimal representation
  2193. */
  2194. public function dec_to_hex($number) {
  2195. $i = 0;
  2196. $hex = array();
  2197. if($number == 0) {
  2198. return '00';
  2199. }
  2200. while($number > 0) {
  2201. if($number == 0) {
  2202. array_push($hex, '0');
  2203. } else {
  2204. array_push($hex, strtoupper(dechex(bcmod($number, '16'))));
  2205. $number = bcdiv($number, '16', 0);
  2206. }
  2207. }
  2208. $hex = array_reverse($hex);
  2209. return implode($hex);
  2210. }
  2211. /**
  2212. * Convert large hexadecimal number to decimal representation (string).
  2213. * (requires PHP bcmath extension)
  2214. * @param $hex (string) hexadecimal number to convert specified as a string
  2215. * @return string hexadecimal representation
  2216. */
  2217. public function hex_to_dec($hex) {
  2218. $dec = 0;
  2219. $bitval = 1;
  2220. $len = strlen($hex);
  2221. for($pos = ($len - 1); $pos >= 0; --$pos) {
  2222. $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval));
  2223. $bitval = bcmul($bitval, 16);
  2224. }
  2225. return $dec;
  2226. }
  2227. /**
  2228. * Intelligent Mail Barcode calculation of Frame Check Sequence
  2229. * @param $code_arr (string) array of hexadecimal values (13 bytes holding 102 bits right justified).
  2230. * @return int 11 bit Frame Check Sequence as integer (decimal base)
  2231. * @protected
  2232. */
  2233. protected function imb_crc11fcs($code_arr) {
  2234. $genpoly = 0x0F35; // generator polynomial
  2235. $fcs = 0x07FF; // Frame Check Sequence
  2236. // do most significant byte skipping the 2 most significant bits
  2237. $data = hexdec($code_arr[0]) << 5;
  2238. for ($bit = 2; $bit < 8; ++$bit) {
  2239. if (($fcs ^ $data) & 0x400) {
  2240. $fcs = ($fcs << 1) ^ $genpoly;
  2241. } else {
  2242. $fcs = ($fcs << 1);
  2243. }
  2244. $fcs &= 0x7FF;
  2245. $data <<= 1;
  2246. }
  2247. // do rest of bytes
  2248. for ($byte = 1; $byte < 13; ++$byte) {
  2249. $data = hexdec($code_arr[$byte]) << 3;
  2250. for ($bit = 0; $bit < 8; ++$bit) {
  2251. if (($fcs ^ $data) & 0x400) {
  2252. $fcs = ($fcs << 1) ^ $genpoly;
  2253. } else {
  2254. $fcs = ($fcs << 1);
  2255. }
  2256. $fcs &= 0x7FF;
  2257. $data <<= 1;
  2258. }
  2259. }
  2260. return $fcs;
  2261. }
  2262. /**
  2263. * Reverse unsigned short value
  2264. * @param $num (int) value to reversr
  2265. * @return int reversed value
  2266. * @protected
  2267. */
  2268. protected function imb_reverse_us($num) {
  2269. $rev = 0;
  2270. for ($i = 0; $i < 16; ++$i) {
  2271. $rev <<= 1;
  2272. $rev |= ($num & 1);
  2273. $num >>= 1;
  2274. }
  2275. return $rev;
  2276. }
  2277. /**
  2278. * generate Nof13 tables used for Intelligent Mail Barcode
  2279. * @param $n (int) is the type of table: 2 for 2of13 table, 5 for 5of13table
  2280. * @param $size (int) size of table (78 for n=2 and 1287 for n=5)
  2281. * @return array requested table
  2282. * @protected
  2283. */
  2284. protected function imb_tables($n, $size) {
  2285. $table = array();
  2286. $lli = 0; // LUT lower index
  2287. $lui = $size - 1; // LUT upper index
  2288. for ($count = 0; $count < 8192; ++$count) {
  2289. $bit_count = 0;
  2290. for ($bit_index = 0; $bit_index < 13; ++$bit_index) {
  2291. $bit_count += intval(($count & (1 << $bit_index)) != 0);
  2292. }
  2293. // if we don't have the right number of bits on, go on to the next value
  2294. if ($bit_count == $n) {
  2295. $reverse = ($this->imb_reverse_us($count) >> 3);
  2296. // if the reverse is less than count, we have already visited this pair before
  2297. if ($reverse >= $count) {
  2298. // If count is symmetric, place it at the first free slot from the end of the list.
  2299. // 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
  2300. if ($reverse == $count) {
  2301. $table[$lui] = $count;
  2302. --$lui;
  2303. } else {
  2304. $table[$lli] = $count;
  2305. ++$lli;
  2306. $table[$lli] = $reverse;
  2307. ++$lli;
  2308. }
  2309. }
  2310. }
  2311. }
  2312. return $table;
  2313. }
  2314. } // end of class
  2315. //============================================================+
  2316. // END OF FILE
  2317. //============================================================+