PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/SugarCE-6.5.4/SugarCE-Full-6.5.4/include/tcpdf/barcodes.php

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