PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/include/tcpdf/barcodes.php

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