PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_breezingforms/libraries/tcpdf/barcodes.php

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