PageRenderTime 77ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Vendor/html2pdf/_tcpdf_5.0.002/barcodes.php

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