PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/phpexcel/PHPExcel/Shared/PDF/barcodes.php

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