PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/tcpdf/barcodes.php

https://bitbucket.org/machaven/limesurvey
PHP | 2288 lines | 1835 code | 39 blank | 414 comment | 244 complexity | 20a584b0129038aa113bb3a9edc686db MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. //============================================================+
  3. // File name : barcodes.php
  4. // Version : 1.0.021
  5. // Begin : 2008-06-09
  6. // Last Update : 2011-09-15
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2008-2011 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // Description : PHP class to creates array representations for
  31. // common 1D barcodes to be used with TCPDF.
  32. //
  33. //============================================================+
  34. /**
  35. * @file
  36. * PHP class to creates array representations for common 1D barcodes to be used with TCPDF.
  37. * @package com.tecnick.tcpdf
  38. * @author Nicola Asuni
  39. * @version 1.0.021
  40. */
  41. /**
  42. * @class TCPDFBarcode
  43. * PHP class to creates array representations for common 1D barcodes to be used with TCPDF (http://www.tcpdf.org).<br>
  44. * @package com.tecnick.tcpdf
  45. * @version 1.0.021
  46. * @author Nicola Asuni
  47. */
  48. class TCPDFBarcode {
  49. /**
  50. * Array representation of barcode.
  51. * @protected
  52. */
  53. protected $barcode_array;
  54. /**
  55. * This is the class constructor.
  56. * Return an array representations for common 1D barcodes:<ul>
  57. * <li>$arrcode['code'] code to be printed on text label</li>
  58. * <li>$arrcode['maxh'] max barcode height</li>
  59. * <li>$arrcode['maxw'] max barcode width</li>
  60. * <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
  61. * <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
  62. * <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
  63. * <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
  64. * <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
  65. * @param $code (string) code to print
  66. * @param $type (string) 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>C128 : CODE 128</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>
  67. * @public
  68. */
  69. public function __construct($code, $type) {
  70. $this->setBarcode($code, $type);
  71. }
  72. /**
  73. * Return an array representations of barcode.
  74. * @return array
  75. * @public
  76. */
  77. public function getBarcodeArray() {
  78. return $this->barcode_array;
  79. }
  80. /**
  81. * Send barcode as SVG image object to the standard output.
  82. * @param $w (int) Minimum width of a single bar in user units.
  83. * @param $h (int) Height of barcode in user units.
  84. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  85. * @public
  86. */
  87. public function getBarcodeSVG($w=2, $h=30, $color='black') {
  88. // send headers
  89. $code = $this->getBarcodeSVGcode($w, $h, $color);
  90. header('Content-Type: application/svg+xml');
  91. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  92. header('Pragma: public');
  93. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  94. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  95. header('Content-Disposition: inline; filename="'.md5($code).'.svg";');
  96. //header('Content-Length: '.strlen($code));
  97. echo $code;
  98. }
  99. /**
  100. * Return a SVG string representation of barcode.
  101. * @param $w (int) Minimum width of a single bar in user units.
  102. * @param $h (int) Height of barcode in user units.
  103. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  104. * @return string SVG code.
  105. * @public
  106. */
  107. public function getBarcodeSVGcode($w=2, $h=30, $color='black') {
  108. // replace table for special characters
  109. $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  110. $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
  111. $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
  112. $svg .= '<svg width="'.round(($this->barcode_array['maxw'] * $w), 3).'" height="'.$h.'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n";
  113. $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
  114. $svg .= "\t".'<g id="bars" fill="'.$color.'" stroke="none">'."\n";
  115. // print bars
  116. $x = 0;
  117. foreach ($this->barcode_array['bcode'] as $k => $v) {
  118. $bw = round(($v['w'] * $w), 3);
  119. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  120. if ($v['t']) {
  121. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  122. // draw a vertical bar
  123. $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$bw.'" height="'.$bh.'" />'."\n";
  124. }
  125. $x += $bw;
  126. }
  127. $svg .= "\t".'</g>'."\n";
  128. $svg .= '</svg>'."\n";
  129. return $svg;
  130. }
  131. /**
  132. * Return an HTML representation of barcode.
  133. * @param $w (int) Width of a single bar element in pixels.
  134. * @param $h (int) Height of a single bar element in pixels.
  135. * @param $color (string) Foreground color for bar elements (background is transparent).
  136. * @return string HTML code.
  137. * @public
  138. */
  139. public function getBarcodeHTML($w=2, $h=30, $color='black') {
  140. // replace table for special characters
  141. $html = '<div style="font-size:0;position:relative;">'."\n";
  142. // print bars
  143. $x = 0;
  144. foreach ($this->barcode_array['bcode'] as $k => $v) {
  145. $bw = round(($v['w'] * $w), 3);
  146. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  147. if ($v['t']) {
  148. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  149. // draw a vertical bar
  150. $html .= '<div style="background-color:'.$color.';width:'.$bw.'px;height:'.$bh.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;">&nbsp;</div>'."\n";
  151. }
  152. $x += $bw;
  153. }
  154. $html .= '</div>'."\n";
  155. return $html;
  156. }
  157. /**
  158. * Return a PNG image representation of barcode (requires GD or Imagick library).
  159. * @param $w (int) Width of a single bar element in pixels.
  160. * @param $h (int) Height of a single bar element in pixels.
  161. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  162. * @return image or false in case of error.
  163. * @public
  164. */
  165. public function getBarcodePNG($w=2, $h=30, $color=array(0,0,0)) {
  166. // calculate image size
  167. $width = ($this->barcode_array['maxw'] * $w);
  168. $height = $h;
  169. if (function_exists('imagecreate')) {
  170. // GD library
  171. $imagick = false;
  172. $png = imagecreate($width, $height);
  173. $bgcol = imagecolorallocate($png, 255, 255, 255);
  174. imagecolortransparent($png, $bgcol);
  175. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  176. } elseif (extension_loaded('imagick')) {
  177. $imagick = true;
  178. $bgcol = new imagickpixel('rgb(255,255,255');
  179. $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
  180. $png = new Imagick();
  181. $png->newImage($width, $height, 'none', 'png');
  182. $bar = new imagickdraw();
  183. $bar->setfillcolor($fgcol);
  184. } else {
  185. return false;
  186. }
  187. // print bars
  188. $x = 0;
  189. foreach ($this->barcode_array['bcode'] as $k => $v) {
  190. $bw = round(($v['w'] * $w), 3);
  191. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  192. if ($v['t']) {
  193. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  194. // draw a vertical bar
  195. if ($imagick) {
  196. $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
  197. } else {
  198. imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
  199. }
  200. }
  201. $x += $bw;
  202. }
  203. // send headers
  204. header('Content-Type: image/png');
  205. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  206. header('Pragma: public');
  207. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  208. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  209. if ($imagick) {
  210. $png->drawimage($bar);
  211. echo $png;
  212. } else {
  213. imagepng($png);
  214. imagedestroy($png);
  215. }
  216. }
  217. /**
  218. * Set the barcode.
  219. * @param $code (string) code to print
  220. * @param $type (string) 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>C128 : CODE 128</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>
  221. * @return array barcode array
  222. * @public
  223. */
  224. public function setBarcode($code, $type) {
  225. switch (strtoupper($type)) {
  226. case 'C39': { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  227. $arrcode = $this->barcode_code39($code, false, false);
  228. break;
  229. }
  230. case 'C39+': { // CODE 39 with checksum
  231. $arrcode = $this->barcode_code39($code, false, true);
  232. break;
  233. }
  234. case 'C39E': { // CODE 39 EXTENDED
  235. $arrcode = $this->barcode_code39($code, true, false);
  236. break;
  237. }
  238. case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
  239. $arrcode = $this->barcode_code39($code, true, true);
  240. break;
  241. }
  242. case 'C93': { // CODE 93 - USS-93
  243. $arrcode = $this->barcode_code93($code);
  244. break;
  245. }
  246. case 'S25': { // Standard 2 of 5
  247. $arrcode = $this->barcode_s25($code, false);
  248. break;
  249. }
  250. case 'S25+': { // Standard 2 of 5 + CHECKSUM
  251. $arrcode = $this->barcode_s25($code, true);
  252. break;
  253. }
  254. case 'I25': { // Interleaved 2 of 5
  255. $arrcode = $this->barcode_i25($code, false);
  256. break;
  257. }
  258. case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
  259. $arrcode = $this->barcode_i25($code, true);
  260. break;
  261. }
  262. case 'C128': { // CODE 128
  263. $arrcode = $this->barcode_c128($code, '');
  264. break;
  265. }
  266. case 'C128A': { // CODE 128 A
  267. $arrcode = $this->barcode_c128($code, 'A');
  268. break;
  269. }
  270. case 'C128B': { // CODE 128 B
  271. $arrcode = $this->barcode_c128($code, 'B');
  272. break;
  273. }
  274. case 'C128C': { // CODE 128 C
  275. $arrcode = $this->barcode_c128($code, 'C');
  276. break;
  277. }
  278. case 'EAN2': { // 2-Digits UPC-Based Extention
  279. $arrcode = $this->barcode_eanext($code, 2);
  280. break;
  281. }
  282. case 'EAN5': { // 5-Digits UPC-Based Extention
  283. $arrcode = $this->barcode_eanext($code, 5);
  284. break;
  285. }
  286. case 'EAN8': { // EAN 8
  287. $arrcode = $this->barcode_eanupc($code, 8);
  288. break;
  289. }
  290. case 'EAN13': { // EAN 13
  291. $arrcode = $this->barcode_eanupc($code, 13);
  292. break;
  293. }
  294. case 'UPCA': { // UPC-A
  295. $arrcode = $this->barcode_eanupc($code, 12);
  296. break;
  297. }
  298. case 'UPCE': { // UPC-E
  299. $arrcode = $this->barcode_eanupc($code, 6);
  300. break;
  301. }
  302. case 'MSI': { // MSI (Variation of Plessey code)
  303. $arrcode = $this->barcode_msi($code, false);
  304. break;
  305. }
  306. case 'MSI+': { // MSI + CHECKSUM (modulo 11)
  307. $arrcode = $this->barcode_msi($code, true);
  308. break;
  309. }
  310. case 'POSTNET': { // POSTNET
  311. $arrcode = $this->barcode_postnet($code, false);
  312. break;
  313. }
  314. case 'PLANET': { // PLANET
  315. $arrcode = $this->barcode_postnet($code, true);
  316. break;
  317. }
  318. case 'RMS4CC': { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
  319. $arrcode = $this->barcode_rms4cc($code, false);
  320. break;
  321. }
  322. case 'KIX': { // KIX (Klant index - Customer index)
  323. $arrcode = $this->barcode_rms4cc($code, true);
  324. break;
  325. }
  326. case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  327. $arrcode = $this->barcode_imb($code);
  328. break;
  329. }
  330. case 'CODABAR': { // CODABAR
  331. $arrcode = $this->barcode_codabar($code);
  332. break;
  333. }
  334. case 'CODE11': { // CODE 11
  335. $arrcode = $this->barcode_code11($code);
  336. break;
  337. }
  338. case 'PHARMA': { // PHARMACODE
  339. $arrcode = $this->barcode_pharmacode($code);
  340. break;
  341. }
  342. case 'PHARMA2T': { // PHARMACODE TWO-TRACKS
  343. $arrcode = $this->barcode_pharmacode2t($code);
  344. break;
  345. }
  346. default: {
  347. $this->barcode_array = false;
  348. $arrcode = false;
  349. break;
  350. }
  351. }
  352. $this->barcode_array = $arrcode;
  353. }
  354. /**
  355. * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  356. * General-purpose code in very wide use world-wide
  357. * @param $code (string) code to represent.
  358. * @param $extended (boolean) if true uses the extended mode.
  359. * @param $checksum (boolean) if true add a checksum to the code.
  360. * @return array barcode representation.
  361. * @protected
  362. */
  363. protected function barcode_code39($code, $extended=false, $checksum=false) {
  364. $chr['0'] = '111331311';
  365. $chr['1'] = '311311113';
  366. $chr['2'] = '113311113';
  367. $chr['3'] = '313311111';
  368. $chr['4'] = '111331113';
  369. $chr['5'] = '311331111';
  370. $chr['6'] = '113331111';
  371. $chr['7'] = '111311313';
  372. $chr['8'] = '311311311';
  373. $chr['9'] = '113311311';
  374. $chr['A'] = '311113113';
  375. $chr['B'] = '113113113';
  376. $chr['C'] = '313113111';
  377. $chr['D'] = '111133113';
  378. $chr['E'] = '311133111';
  379. $chr['F'] = '113133111';
  380. $chr['G'] = '111113313';
  381. $chr['H'] = '311113311';
  382. $chr['I'] = '113113311';
  383. $chr['J'] = '111133311';
  384. $chr['K'] = '311111133';
  385. $chr['L'] = '113111133';
  386. $chr['M'] = '313111131';
  387. $chr['N'] = '111131133';
  388. $chr['O'] = '311131131';
  389. $chr['P'] = '113131131';
  390. $chr['Q'] = '111111333';
  391. $chr['R'] = '311111331';
  392. $chr['S'] = '113111331';
  393. $chr['T'] = '111131331';
  394. $chr['U'] = '331111113';
  395. $chr['V'] = '133111113';
  396. $chr['W'] = '333111111';
  397. $chr['X'] = '131131113';
  398. $chr['Y'] = '331131111';
  399. $chr['Z'] = '133131111';
  400. $chr['-'] = '131111313';
  401. $chr['.'] = '331111311';
  402. $chr[' '] = '133111311';
  403. $chr['$'] = '131313111';
  404. $chr['/'] = '131311131';
  405. $chr['+'] = '131113131';
  406. $chr['%'] = '111313131';
  407. $chr['*'] = '131131311';
  408. $code = strtoupper($code);
  409. if ($extended) {
  410. // extended mode
  411. $code = $this->encode_code39_ext($code);
  412. }
  413. if ($code === false) {
  414. return false;
  415. }
  416. if ($checksum) {
  417. // checksum
  418. $code .= $this->checksum_code39($code);
  419. }
  420. // add start and stop codes
  421. $code = '*'.$code.'*';
  422. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  423. $k = 0;
  424. $clen = strlen($code);
  425. for ($i = 0; $i < $clen; ++$i) {
  426. $char = $code{$i};
  427. if(!isset($chr[$char])) {
  428. // invalid character
  429. return false;
  430. }
  431. for ($j = 0; $j < 9; ++$j) {
  432. if (($j % 2) == 0) {
  433. $t = true; // bar
  434. } else {
  435. $t = false; // space
  436. }
  437. $w = $chr[$char]{$j};
  438. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  439. $bararray['maxw'] += $w;
  440. ++$k;
  441. }
  442. // intercharacter gap
  443. $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
  444. $bararray['maxw'] += 1;
  445. ++$k;
  446. }
  447. return $bararray;
  448. }
  449. /**
  450. * Encode a string to be used for CODE 39 Extended mode.
  451. * @param $code (string) code to represent.
  452. * @return encoded string.
  453. * @protected
  454. */
  455. protected function encode_code39_ext($code) {
  456. $encode = array(
  457. chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
  458. chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
  459. chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
  460. chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
  461. chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
  462. chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
  463. chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
  464. chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
  465. chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
  466. chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
  467. chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
  468. chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
  469. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  470. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  471. chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
  472. chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
  473. chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  474. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  475. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  476. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  477. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  478. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  479. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
  480. chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
  481. chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
  482. chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
  483. chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
  484. chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
  485. chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
  486. chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
  487. chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
  488. chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
  489. $code_ext = '';
  490. $clen = strlen($code);
  491. for ($i = 0 ; $i < $clen; ++$i) {
  492. if (ord($code{$i}) > 127) {
  493. return false;
  494. }
  495. $code_ext .= $encode[$code{$i}];
  496. }
  497. return $code_ext;
  498. }
  499. /**
  500. * Calculate CODE 39 checksum (modulo 43).
  501. * @param $code (string) code to represent.
  502. * @return char checksum.
  503. * @protected
  504. */
  505. protected function checksum_code39($code) {
  506. $chars = array(
  507. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  508. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  509. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  510. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
  511. $sum = 0;
  512. $clen = strlen($code);
  513. for ($i = 0 ; $i < $clen; ++$i) {
  514. $k = array_keys($chars, $code{$i});
  515. $sum += $k[0];
  516. }
  517. $j = ($sum % 43);
  518. return $chars[$j];
  519. }
  520. /**
  521. * CODE 93 - USS-93
  522. * Compact code similar to Code 39
  523. * @param $code (string) code to represent.
  524. * @return array barcode representation.
  525. * @protected
  526. */
  527. protected function barcode_code93($code) {
  528. $chr[48] = '131112'; // 0
  529. $chr[49] = '111213'; // 1
  530. $chr[50] = '111312'; // 2
  531. $chr[51] = '111411'; // 3
  532. $chr[52] = '121113'; // 4
  533. $chr[53] = '121212'; // 5
  534. $chr[54] = '121311'; // 6
  535. $chr[55] = '111114'; // 7
  536. $chr[56] = '131211'; // 8
  537. $chr[57] = '141111'; // 9
  538. $chr[65] = '211113'; // A
  539. $chr[66] = '211212'; // B
  540. $chr[67] = '211311'; // C
  541. $chr[68] = '221112'; // D
  542. $chr[69] = '221211'; // E
  543. $chr[70] = '231111'; // F
  544. $chr[71] = '112113'; // G
  545. $chr[72] = '112212'; // H
  546. $chr[73] = '112311'; // I
  547. $chr[74] = '122112'; // J
  548. $chr[75] = '132111'; // K
  549. $chr[76] = '111123'; // L
  550. $chr[77] = '111222'; // M
  551. $chr[78] = '111321'; // N
  552. $chr[79] = '121122'; // O
  553. $chr[80] = '131121'; // P
  554. $chr[81] = '212112'; // Q
  555. $chr[82] = '212211'; // R
  556. $chr[83] = '211122'; // S
  557. $chr[84] = '211221'; // T
  558. $chr[85] = '221121'; // U
  559. $chr[86] = '222111'; // V
  560. $chr[87] = '112122'; // W
  561. $chr[88] = '112221'; // X
  562. $chr[89] = '122121'; // Y
  563. $chr[90] = '123111'; // Z
  564. $chr[45] = '121131'; // -
  565. $chr[46] = '311112'; // .
  566. $chr[32] = '311211'; //
  567. $chr[36] = '321111'; // $
  568. $chr[47] = '112131'; // /
  569. $chr[43] = '113121'; // +
  570. $chr[37] = '211131'; // %
  571. $chr[128] = '121221'; // ($)
  572. $chr[129] = '311121'; // (/)
  573. $chr[130] = '122211'; // (+)
  574. $chr[131] = '312111'; // (%)
  575. $chr[42] = '111141'; // start-stop
  576. $code = strtoupper($code);
  577. $encode = array(
  578. chr(0) => chr(131).'U', chr(1) => chr(128).'A', chr(2) => chr(128).'B', chr(3) => chr(128).'C',
  579. chr(4) => chr(128).'D', chr(5) => chr(128).'E', chr(6) => chr(128).'F', chr(7) => chr(128).'G',
  580. chr(8) => chr(128).'H', chr(9) => chr(128).'I', chr(10) => chr(128).'J', chr(11) => '£K',
  581. chr(12) => chr(128).'L', chr(13) => chr(128).'M', chr(14) => chr(128).'N', chr(15) => chr(128).'O',
  582. chr(16) => chr(128).'P', chr(17) => chr(128).'Q', chr(18) => chr(128).'R', chr(19) => chr(128).'S',
  583. chr(20) => chr(128).'T', chr(21) => chr(128).'U', chr(22) => chr(128).'V', chr(23) => chr(128).'W',
  584. chr(24) => chr(128).'X', chr(25) => chr(128).'Y', chr(26) => chr(128).'Z', chr(27) => chr(131).'A',
  585. chr(28) => chr(131).'B', chr(29) => chr(131).'C', chr(30) => chr(131).'D', chr(31) => chr(131).'E',
  586. chr(32) => ' ', chr(33) => chr(129).'A', chr(34) => chr(129).'B', chr(35) => chr(129).'C',
  587. chr(36) => chr(129).'D', chr(37) => chr(129).'E', chr(38) => chr(129).'F', chr(39) => chr(129).'G',
  588. chr(40) => chr(129).'H', chr(41) => chr(129).'I', chr(42) => chr(129).'J', chr(43) => chr(129).'K',
  589. chr(44) => chr(129).'L', chr(45) => '-', chr(46) => '.', chr(47) => chr(129).'O',
  590. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  591. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  592. chr(56) => '8', chr(57) => '9', chr(58) => chr(129).'Z', chr(59) => chr(131).'F',
  593. chr(60) => chr(131).'G', chr(61) => chr(131).'H', chr(62) => chr(131).'I', chr(63) => chr(131).'J',
  594. chr(64) => chr(131).'V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  595. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  596. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  597. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  598. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  599. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  600. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => chr(131).'K',
  601. chr(92) => chr(131).'L', chr(93) => chr(131).'M', chr(94) => chr(131).'N', chr(95) => chr(131).'O',
  602. chr(96) => chr(131).'W', chr(97) => chr(130).'A', chr(98) => chr(130).'B', chr(99) => chr(130).'C',
  603. chr(100) => chr(130).'D', chr(101) => chr(130).'E', chr(102) => chr(130).'F', chr(103) => chr(130).'G',
  604. chr(104) => chr(130).'H', chr(105) => chr(130).'I', chr(106) => chr(130).'J', chr(107) => chr(130).'K',
  605. chr(108) => chr(130).'L', chr(109) => chr(130).'M', chr(110) => chr(130).'N', chr(111) => chr(130).'O',
  606. chr(112) => chr(130).'P', chr(113) => chr(130).'Q', chr(114) => chr(130).'R', chr(115) => chr(130).'S',
  607. chr(116) => chr(130).'T', chr(117) => chr(130).'U', chr(118) => chr(130).'V', chr(119) => chr(130).'W',
  608. chr(120) => chr(130).'X', chr(121) => chr(130).'Y', chr(122) => chr(130).'Z', chr(123) => chr(131).'P',
  609. chr(124) => chr(131).'Q', chr(125) => chr(131).'R', chr(126) => chr(131).'S', chr(127) => chr(131).'T');
  610. $code_ext = '';
  611. $clen = strlen($code);
  612. for ($i = 0 ; $i < $clen; ++$i) {
  613. if (ord($code{$i}) > 127) {
  614. return false;
  615. }
  616. $code_ext .= $encode[$code{$i}];
  617. }
  618. // checksum
  619. $code_ext .= $this->checksum_code93($code_ext);
  620. // add start and stop codes
  621. $code = '*'.$code_ext.'*';
  622. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  623. $k = 0;
  624. $clen = strlen($code);
  625. for ($i = 0; $i < $clen; ++$i) {
  626. $char = ord($code{$i});
  627. if(!isset($chr[$char])) {
  628. // invalid character
  629. return false;
  630. }
  631. for ($j = 0; $j < 6; ++$j) {
  632. if (($j % 2) == 0) {
  633. $t = true; // bar
  634. } else {
  635. $t = false; // space
  636. }
  637. $w = $chr[$char]{$j};
  638. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  639. $bararray['maxw'] += $w;
  640. ++$k;
  641. }
  642. }
  643. $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0);
  644. $bararray['maxw'] += 1;
  645. ++$k;
  646. return $bararray;
  647. }
  648. /**
  649. * Calculate CODE 93 checksum (modulo 47).
  650. * @param $code (string) code to represent.
  651. * @return string checksum code.
  652. * @protected
  653. */
  654. protected function checksum_code93($code) {
  655. $chars = array(
  656. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  657. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  658. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  659. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%',
  660. '<', '=', '>', '?');
  661. // translate special characters
  662. $code = strtr($code, chr(128).chr(131).chr(129).chr(130), '<=>?');
  663. $len = strlen($code);
  664. // calculate check digit C
  665. $p = 1;
  666. $check = 0;
  667. for ($i = ($len - 1); $i >= 0; --$i) {
  668. $k = array_keys($chars, $code{$i});
  669. $check += ($k[0] * $p);
  670. ++$p;
  671. if ($p > 20) {
  672. $p = 1;
  673. }
  674. }
  675. $check %= 47;
  676. $c = $chars[$check];
  677. $code .= $c;
  678. // calculate check digit K
  679. $p = 1;
  680. $check = 0;
  681. for ($i = $len; $i >= 0; --$i) {
  682. $k = array_keys($chars, $code{$i});
  683. $check += ($k[0] * $p);
  684. ++$p;
  685. if ($p > 15) {
  686. $p = 1;
  687. }
  688. }
  689. $check %= 47;
  690. $k = $chars[$check];
  691. $checksum = $c.$k;
  692. // resto respecial characters
  693. $checksum = strtr($checksum, '<=>?', chr(128).chr(131).chr(129).chr(130));
  694. return $checksum;
  695. }
  696. /**
  697. * Checksum for standard 2 of 5 barcodes.
  698. * @param $code (string) code to process.
  699. * @return int checksum.
  700. * @protected
  701. */
  702. protected function checksum_s25($code) {
  703. $len = strlen($code);
  704. $sum = 0;
  705. for ($i = 0; $i < $len; $i+=2) {
  706. $sum += $code{$i};
  707. }
  708. $sum *= 3;
  709. for ($i = 1; $i < $len; $i+=2) {
  710. $sum += ($code{$i});
  711. }
  712. $r = $sum % 10;
  713. if($r > 0) {
  714. $r = (10 - $r);
  715. }
  716. return $r;
  717. }
  718. /**
  719. * MSI.
  720. * Variation of Plessey code, with similar applications
  721. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  722. * @param $code (string) code to represent.
  723. * @param $checksum (boolean) if true add a checksum to the code (modulo 11)
  724. * @return array barcode representation.
  725. * @protected
  726. */
  727. protected function barcode_msi($code, $checksum=false) {
  728. $chr['0'] = '100100100100';
  729. $chr['1'] = '100100100110';
  730. $chr['2'] = '100100110100';
  731. $chr['3'] = '100100110110';
  732. $chr['4'] = '100110100100';
  733. $chr['5'] = '100110100110';
  734. $chr['6'] = '100110110100';
  735. $chr['7'] = '100110110110';
  736. $chr['8'] = '110100100100';
  737. $chr['9'] = '110100100110';
  738. $chr['A'] = '110100110100';
  739. $chr['B'] = '110100110110';
  740. $chr['C'] = '110110100100';
  741. $chr['D'] = '110110100110';
  742. $chr['E'] = '110110110100';
  743. $chr['F'] = '110110110110';
  744. if ($checksum) {
  745. // add checksum
  746. $clen = strlen($code);
  747. $p = 2;
  748. $check = 0;
  749. for ($i = ($clen - 1); $i >= 0; --$i) {
  750. $check += (hexdec($code{$i}) * $p);
  751. ++$p;
  752. if ($p > 7) {
  753. $p = 2;
  754. }
  755. }
  756. $check %= 11;
  757. if ($check > 0) {
  758. $check = 11 - $check;
  759. }
  760. $code .= $check;
  761. }
  762. $seq = '110'; // left guard
  763. $clen = strlen($code);
  764. for ($i = 0; $i < $clen; ++$i) {
  765. $digit = $code{$i};
  766. if (!isset($chr[$digit])) {
  767. // invalid character
  768. return false;
  769. }
  770. $seq .= $chr[$digit];
  771. }
  772. $seq .= '1001'; // right guard
  773. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  774. return $this->binseq_to_array($seq, $bararray);
  775. }
  776. /**
  777. * Standard 2 of 5 barcodes.
  778. * Used in airline ticket marking, photofinishing
  779. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  780. * @param $code (string) code to represent.
  781. * @param $checksum (boolean) if true add a checksum to the code
  782. * @return array barcode representation.
  783. * @protected
  784. */
  785. protected function barcode_s25($code, $checksum=false) {
  786. $chr['0'] = '10101110111010';
  787. $chr['1'] = '11101010101110';
  788. $chr['2'] = '10111010101110';
  789. $chr['3'] = '11101110101010';
  790. $chr['4'] = '10101110101110';
  791. $chr['5'] = '11101011101010';
  792. $chr['6'] = '10111011101010';
  793. $chr['7'] = '10101011101110';
  794. $chr['8'] = '10101110111010';
  795. $chr['9'] = '10111010111010';
  796. if ($checksum) {
  797. // add checksum
  798. $code .= $this->checksum_s25($code);
  799. }
  800. if((strlen($code) % 2) != 0) {
  801. // add leading zero if code-length is odd
  802. $code = '0'.$code;
  803. }
  804. $seq = '11011010';
  805. $clen = strlen($code);
  806. for ($i = 0; $i < $clen; ++$i) {
  807. $digit = $code{$i};
  808. if (!isset($chr[$digit])) {
  809. // invalid character
  810. return false;
  811. }
  812. $seq .= $chr[$digit];
  813. }
  814. $seq .= '1101011';
  815. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  816. return $this->binseq_to_array($seq, $bararray);
  817. }
  818. /**
  819. * Convert binary barcode sequence to TCPDF barcode array.
  820. * @param $seq (string) barcode as binary sequence.
  821. * @param $bararray (array) barcode array.
  822. * òparam array $bararray TCPDF barcode array to fill up
  823. * @return array barcode representation.
  824. * @protected
  825. */
  826. protected function binseq_to_array($seq, $bararray) {
  827. $len = strlen($seq);
  828. $w = 0;
  829. $k = 0;
  830. for ($i = 0; $i < $len; ++$i) {
  831. $w += 1;
  832. if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq{$i} != $seq{($i+1)}))) {
  833. if ($seq{$i} == '1') {
  834. $t = true; // bar
  835. } else {
  836. $t = false; // space
  837. }
  838. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  839. $bararray['maxw'] += $w;
  840. ++$k;
  841. $w = 0;
  842. }
  843. }
  844. return $bararray;
  845. }
  846. /**
  847. * Interleaved 2 of 5 barcodes.
  848. * Compact numeric code, widely used in industry, air cargo
  849. * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
  850. * @param $code (string) code to represent.
  851. * @param $checksum (boolean) if true add a checksum to the code
  852. * @return array barcode representation.
  853. * @protected
  854. */
  855. protected function barcode_i25($code, $checksum=false) {
  856. $chr['0'] = '11221';
  857. $chr['1'] = '21112';
  858. $chr['2'] = '12112';
  859. $chr['3'] = '22111';
  860. $chr['4'] = '11212';
  861. $chr['5'] = '21211';
  862. $chr['6'] = '12211';
  863. $chr['7'] = '11122';
  864. $chr['8'] = '21121';
  865. $chr['9'] = '12121';
  866. $chr['A'] = '11';
  867. $chr['Z'] = '21';
  868. if ($checksum) {
  869. // add checksum
  870. $code .= $this->checksum_s25($code);
  871. }
  872. if((strlen($code) % 2) != 0) {
  873. // add leading zero if code-length is odd
  874. $code = '0'.$code;
  875. }
  876. // add start and stop codes
  877. $code = 'AA'.strtolower($code).'ZA';
  878. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  879. $k = 0;
  880. $clen = strlen($code);
  881. for ($i = 0; $i < $clen; $i = ($i + 2)) {
  882. $char_bar = $code{$i};
  883. $char_space = $code{$i+1};
  884. if((!isset($chr[$char_bar])) OR (!isset($chr[$char_space]))) {
  885. // invalid character
  886. return false;
  887. }
  888. // create a bar-space sequence
  889. $seq = '';
  890. $chrlen = strlen($chr[$char_bar]);
  891. for ($s = 0; $s < $chrlen; $s++){
  892. $seq .= $chr[$char_bar]{$s} . $chr[$char_space]{$s};
  893. }
  894. $seqlen = strlen($seq);
  895. for ($j = 0; $j < $seqlen; ++$j) {
  896. if (($j % 2) == 0) {
  897. $t = true; // bar
  898. } else {
  899. $t = false; // space
  900. }
  901. $w = $seq{$j};
  902. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  903. $bararray['maxw'] += $w;
  904. ++$k;
  905. }
  906. }
  907. return $bararray;
  908. }
  909. /**
  910. * C128 barcodes.
  911. * Very capable code, excellent density, high reliability; in very wide use world-wide
  912. * @param $code (string) code to represent.
  913. * @param $type (string) barcode type: A, B, C or empty for automatic switch (AUTO mode)
  914. * @return array barcode representation.
  915. * @protected
  916. */
  917. protected function barcode_c128($code, $type='') {
  918. $chr = array(
  919. '212222', /* 00 */
  920. '222122', /* 01 */
  921. '222221', /* 02 */
  922. '121223', /* 03 */
  923. '121322', /* 04 */
  924. '131222', /* 05 */
  925. '122213', /* 06 */
  926. '122312', /* 07 */
  927. '132212', /* 08 */
  928. '221213', /* 09 */
  929. '221312', /* 10 */
  930. '231212', /* 11 */
  931. '112232', /* 12 */
  932. '122132', /* 13 */
  933. '122231', /* 14 */
  934. '113222', /* 15 */
  935. '123122', /* 16 */
  936. '123221', /* 17 */
  937. '223211', /* 18 */
  938. '221132', /* 19 */
  939. '221231', /* 20 */
  940. '213212', /* 21 */
  941. '223112', /* 22 */
  942. '312131', /* 23 */
  943. '311222', /* 24 */
  944. '321122', /* 25 */
  945. '321221', /* 26 */
  946. '312212', /* 27 */
  947. '322112', /* 28 */
  948. '322211', /* 29 */
  949. '212123', /* 30 */
  950. '212321', /* 31 */
  951. '232121', /* 32 */
  952. '111323', /* 33 */
  953. '131123', /* 34 */
  954. '131321', /* 35 */
  955. '112313', /* 36 */
  956. '132113', /* 37 */
  957. '132311', /* 38 */
  958. '211313', /* 39 */
  959. '231113', /* 40 */
  960. '231311', /* 41 */
  961. '112133', /* 42 */
  962. '112331', /* 43 */
  963. '132131', /* 44 */
  964. '113123', /* 45 */
  965. '113321', /* 46 */
  966. '133121', /* 47 */
  967. '313121', /* 48 */
  968. '211331', /* 49 */
  969. '231131', /* 50 */
  970. '213113', /* 51 */
  971. '213311', /* 52 */
  972. '213131', /* 53 */
  973. '311123', /* 54 */
  974. '311321', /* 55 */
  975. '331121', /* 56 */
  976. '312113', /* 57 */
  977. '312311', /* 58 */
  978. '332111', /* 59 */
  979. '314111', /* 60 */
  980. '221411', /* 61 */
  981. '431111', /* 62 */
  982. '111224', /* 63 */
  983. '111422', /* 64 */
  984. '121124', /* 65 */
  985. '121421', /* 66 */
  986. '141122', /* 67 */
  987. '141221', /* 68 */
  988. '112214', /* 69 */
  989. '112412', /* 70 */
  990. '122114', /* 71 */
  991. '122411', /* 72 */
  992. '142112', /* 73 */
  993. '142211', /* 74 */
  994. '241211', /* 75 */
  995. '221114', /* 76 */
  996. '413111', /* 77 */
  997. '241112', /* 78 */
  998. '134111', /* 79 */
  999. '111242', /* 80 */
  1000. '121142', /* 81 */
  1001. '121241', /* 82 */
  1002. '114212', /* 83 */
  1003. '124112', /* 84 */
  1004. '124211', /* 85 */
  1005. '411212', /* 86 */
  1006. '421112', /* 87 */
  1007. '421211', /* 88 */
  1008. '212141', /* 89 */
  1009. '214121', /* 90 */
  1010. '412121', /* 91 */
  1011. '111143', /* 92 */
  1012. '111341', /* 93 */
  1013. '131141', /* 94 */
  1014. '114113', /* 95 */
  1015. '114311', /* 96 */
  1016. '411113', /* 97 */
  1017. '411311', /* 98 */
  1018. '113141', /* 99 */
  1019. '114131', /* 100 */
  1020. '311141', /* 101 */
  1021. '411131', /* 102 */
  1022. '211412', /* 103 START A */
  1023. '211214', /* 104 START B */
  1024. '211232', /* 105 START C */
  1025. '233111', /* STOP */
  1026. '200000' /* END */
  1027. );
  1028. // ASCII characters for code A (ASCII 00 - 95)
  1029. $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
  1030. $keys_a .= chr(0).chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8).chr(9);
  1031. $keys_a .= chr(10).chr(11).chr(12).chr(13).chr(14).chr(15).chr(16).chr(17).chr(18).chr(19);
  1032. $keys_a .= chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29);
  1033. $keys_a .= chr(30).chr(31);
  1034. // ASCII characters for code B (ASCII 32 - 127)
  1035. $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.chr(127);
  1036. // special codes
  1037. $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);
  1038. $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100);
  1039. // array of symbols
  1040. $code_data = array();
  1041. // lenght of the code
  1042. $len = strlen($code);
  1043. switch(strtoupper($type)) {
  1044. case 'A': { // MODE A
  1045. $startid = 103;
  1046. for ($i = 0; $i < $len; ++$i) {
  1047. $char = $code{$i};
  1048. $char_id = ord($char);
  1049. if (($char_id >= 241) AND ($char_id <= 244)) {
  1050. $code_data[] = $fnc_a[$char_id];
  1051. } elseif (($char_id >= 0) AND ($char_id <= 95)) {
  1052. $code_data[] = strpos($keys_a, $char);
  1053. } else {
  1054. return false;
  1055. }
  1056. }
  1057. break;
  1058. }
  1059. case 'B': { // MODE B
  1060. $startid = 104;
  1061. for ($i = 0; $i < $len; ++$i) {
  1062. $char = $code{$i};
  1063. $char_id = ord($char);
  1064. if (($char_id >= 241) AND ($char_id <= 244)) {
  1065. $code_data[] = $fnc_b[$char_id];
  1066. } elseif (($char_id >= 32) AND ($char_id <= 127)) {
  1067. $code_data[] = strpos($keys_b, $char);
  1068. } else {
  1069. return false;
  1070. }
  1071. }
  1072. break;
  1073. }
  1074. case 'C': { // MODE C
  1075. $startid = 105;
  1076. if (ord($code{0}) == 241) {
  1077. $code_data[] = 102;
  1078. $code = substr($code, 1);
  1079. --$len;
  1080. }
  1081. if (($len % 2) != 0) {
  1082. // the length must be even
  1083. return false;
  1084. }
  1085. for ($i = 0; $i < $len; $i+=2) {
  1086. $chrnum = $code{$i}.$code{$i+1};
  1087. if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
  1088. $code_data[] = intval($chrnum);
  1089. } else {
  1090. return false;
  1091. }
  1092. }
  1093. break;
  1094. }
  1095. default: { // MODE AUTO
  1096. // split code into sequences
  1097. $sequence = array();
  1098. // get numeric sequences (if any)
  1099. $numseq = array();
  1100. preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1101. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1102. $end_offset = 0;
  1103. foreach ($numseq[1] as $val) {
  1104. $offset = $val[1];
  1105. if ($offset > $end_offset) {
  1106. // non numeric sequence
  1107. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset))));
  1108. }
  1109. // numeric sequence
  1110. $slen = strlen($val[0]);
  1111. if (($slen % 2) != 0) {
  1112. // the length must be even
  1113. --$slen;
  1114. }
  1115. $sequence[] = array('C', substr($code, $offset, $slen), $slen);
  1116. $end_offset = $offset + $slen;
  1117. }
  1118. if ($end_offset < $len) {
  1119. $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset)));
  1120. }
  1121. } else {
  1122. // text code (non C mode)
  1123. $sequence = array_merge($sequence, $this->get128ABsequence($code));
  1124. }
  1125. // process the sequence
  1126. foreach ($sequence as $key => $seq) {
  1127. switch($seq[0]) {
  1128. case 'A': {
  1129. if ($key == 0) {
  1130. $startid = 103;
  1131. } elseif ($sequence[($key - 1)][0] != 'A') {
  1132. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND (!isset($sequence[($key - 1)][3]))) {
  1133. // single character shift
  1134. $code_data[] = 98;
  1135. // mark shift
  1136. $sequence[$key][3] = true;
  1137. } elseif (!isset($sequence[($key - 1)][3])) {
  1138. $code_data[] = 101;
  1139. }
  1140. }
  1141. for ($i = 0; $i < $seq[2]; ++$i) {
  1142. $char = $seq[1]{$i};
  1143. $char_id = ord($char);
  1144. if (($char_id >= 241) AND ($char_id <= 244)) {
  1145. $code_data[] = $fnc_a[$char_id];
  1146. } else {
  1147. $code_data[] = strpos($keys_a, $char);
  1148. }
  1149. }
  1150. break;
  1151. }
  1152. case 'B': {
  1153. if ($key == 0) {
  1154. $tmpchr = ord($seq[1]{0});
  1155. if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) {
  1156. switch ($sequence[($key + 1)][0]) {
  1157. case 'A': {
  1158. $startid = 103;
  1159. $sequence[$key][0] = 'A';
  1160. $code_data[] = $fnc_a[$tmpchr];
  1161. break;
  1162. }
  1163. case 'C': {
  1164. $startid = 105;
  1165. $sequence[$key][0] = 'C';
  1166. $code_data[] = $fnc_a[$tmpchr];
  1167. break;
  1168. }
  1169. }
  1170. break;
  1171. } else {
  1172. $startid = 104;
  1173. }
  1174. } elseif ($sequence[($key - 1)][0] != 'B') {
  1175. if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND (!isset($sequence[($key - 1)][3]))) {
  1176. // single character shift
  1177. $code_data[] = 98;
  1178. // mark shift
  1179. $sequence[$key][3] = true;
  1180. } elseif (!isset($sequence[($key - 1)][3])) {
  1181. $code_data[] = 100;
  1182. }
  1183. }
  1184. for ($i = 0; $i < $seq[2]; ++$i) {
  1185. $char = $seq[1]{$i};
  1186. $char_id = ord($char);
  1187. if (($char_id >= 241) AND ($char_id <= 244)) {
  1188. $code_data[] = $fnc_b[$char_id];
  1189. } else {
  1190. $code_data[] = strpos($keys_b, $char);
  1191. }
  1192. }
  1193. break;
  1194. }
  1195. case 'C': {
  1196. if ($key == 0) {
  1197. $startid = 105;
  1198. } elseif ($sequence[($key - 1)][0] != 'C') {
  1199. $code_data[] = 99;
  1200. }
  1201. for ($i = 0; $i < $seq[2]; $i+=2) {
  1202. $chrnum = $seq[1]{$i}.$seq[1]{$i+1};
  1203. $code_data[] = intval($chrnum);
  1204. }
  1205. break;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. // calculate check character
  1212. $sum = $startid;
  1213. foreach ($code_data as $key => $val) {
  1214. $sum += ($val * ($key + 1));
  1215. }
  1216. // add check character
  1217. $code_data[] = ($sum % 103);
  1218. // add stop sequence
  1219. $code_data[] = 106;
  1220. $code_data[] = 107;
  1221. // add start code at the beginning
  1222. array_unshift($code_data, $startid);
  1223. // build barcode array
  1224. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1225. foreach ($code_data as $val) {
  1226. $seq = $chr[$val];
  1227. for ($j = 0; $j < 6; ++$j) {
  1228. if (($j % 2) == 0) {
  1229. $t = true; // bar
  1230. } else {
  1231. $t = false; // space
  1232. }
  1233. $w = $seq{$j};
  1234. $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1235. $bararray['maxw'] += $w;
  1236. }
  1237. }
  1238. return $bararray;
  1239. }
  1240. /**
  1241. * Split text code in A/B sequence for 128 code
  1242. * @param $code (string) code to split.
  1243. * @return array sequence
  1244. * @protected
  1245. */
  1246. protected function get128ABsequence($code) {
  1247. $len = strlen($code);
  1248. $sequence = array();
  1249. // get A sequences (if any)
  1250. $numseq = array();
  1251. preg_match_all('/([\0-\31])/', $code, $numseq, PREG_OFFSET_CAPTURE);
  1252. if (isset($numseq[1]) AND !empty($numseq[1])) {
  1253. $end_offset = 0;
  1254. foreach ($numseq[1] as $val) {
  1255. $offset = $val[1];
  1256. if ($offset > $end_offset) {
  1257. // B sequence
  1258. $sequence[] = array('B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset));
  1259. }
  1260. // A sequence
  1261. $slen = strlen($val[0]);
  1262. $sequence[] = array('A', substr($code, $offset, $slen), $slen);
  1263. $end_offset = $offset + $slen;
  1264. }
  1265. if ($end_offset < $len) {
  1266. $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
  1267. }
  1268. } else {
  1269. // only B sequence
  1270. $sequence[] = array('B', $code, $len);
  1271. }
  1272. return $sequence;
  1273. }
  1274. /**
  1275. * EAN13 and UPC-A barcodes.
  1276. * EAN13: European Article Numbering international retail product code
  1277. * UPC-A: Universal product code seen on almost all retail products in the USA and Canada
  1278. * UPC-E: Short version of UPC symbol
  1279. * @param $code (string) code to represent.
  1280. * @param $len (string) barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A
  1281. * @return array barcode representation.
  1282. * @protected
  1283. */
  1284. protected function barcode_eanupc($code, $len=13) {
  1285. $upce = false;
  1286. if ($len == 6) {
  1287. $len = 12; // UPC-A
  1288. $upce = true; // UPC-E mode
  1289. }
  1290. $data_len = $len - 1;
  1291. //Padding
  1292. $code = str_pad($code, $data_len, '0', STR_PAD_LEFT);
  1293. $code_len = strlen($code);
  1294. // calculate check digit
  1295. $sum_a = 0;
  1296. for ($i = 1; $i < $data_len; $i+=2) {
  1297. $sum_a += $code{$i};
  1298. }
  1299. if ($len > 12) {
  1300. $sum_a *= 3;
  1301. }
  1302. $sum_b = 0;
  1303. for ($i = 0; $i < $data_len; $i+=2) {
  1304. $sum_b += ($code{$i});
  1305. }
  1306. if ($len < 13) {
  1307. $sum_b *= 3;
  1308. }
  1309. $r = ($sum_a + $sum_b) % 10;
  1310. if($r > 0) {
  1311. $r = (10 - $r);
  1312. }
  1313. if ($code_len == $data_len) {
  1314. // add check digit
  1315. $code .= $r;
  1316. } elseif ($r !== intval($code{$data_len})) {
  1317. // wrong checkdigit
  1318. return false;
  1319. }
  1320. if ($len == 12) {
  1321. // UPC-A
  1322. $code = '0'.$code;
  1323. ++$len;
  1324. }
  1325. if ($upce) {
  1326. // convert UPC-A to UPC-E
  1327. $tmp = substr($code, 4, 3);
  1328. if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) {
  1329. // manufacturer code ends in 000, 100, or 200
  1330. $upce_code = substr($code, 2, 2).substr($code, 9, 3).substr($code, 4, 1);
  1331. } else {
  1332. $tmp = substr($code, 5, 2);
  1333. if ($tmp == '00') {
  1334. // manufacturer code ends in 00
  1335. $upce_code = substr($code, 2, 3).substr($code, 10, 2).'3';
  1336. } else {
  1337. $tmp = substr($code, 6, 1);
  1338. if ($tmp == '0') {
  1339. // manufacturer code ends in 0
  1340. $upce_code = substr($code, 2, 4).substr($code, 11, 1).'4';
  1341. } else {
  1342. // manufacturer code does not end in zero
  1343. $upce_code = substr($code, 2, 5).substr($code, 11, 1);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. //Convert digits to bars
  1349. $codes = array(
  1350. 'A'=>array( // left odd parity
  1351. '0'=>'0001101',
  1352. '1'=>'0011001',
  1353. '2'=>'0010011',
  1354. '3'=>'0111101',
  1355. '4'=>'0100011',
  1356. '5'=>'0110001',
  1357. '6'=>'0101111',
  1358. '7'=>'0111011',
  1359. '8'=>'0110111',
  1360. '9'=>'0001011'),
  1361. 'B'=>array( // left even parity
  1362. '0'=>'0100111',
  1363. '1'=>'0110011',
  1364. '2'=>'0011011',
  1365. '3'=>'0100001',
  1366. '4'=>'0011101',
  1367. '5'=>'0111001',
  1368. '6'=>'0000101',
  1369. '7'=>'0010001',
  1370. '8'=>'0001001',
  1371. '9'=>'0010111'),
  1372. 'C'=>array( // right
  1373. '0'=>'1110010',
  1374. '1'=>'1100110',
  1375. '2'=>'1101100',
  1376. '3'=>'1000010',
  1377. '4'=>'1011100',
  1378. '5'=>'1001110',
  1379. '6'=>'1010000',
  1380. '7'=>'1000100',
  1381. '8'=>'1001000',
  1382. '9'=>'1110100')
  1383. );
  1384. $parities = array(
  1385. '0'=>array('A','A','A','A','A','A'),
  1386. '1'=>array('A','A','B','A','B','B'),
  1387. '2'=>array('A','A','B','B','A','B'),
  1388. '3'=>array('A','A','B','B','B','A'),
  1389. '4'=>array('A','B','A','A','B','B'),
  1390. '5'=>array('A','B','B','A','A','B'),
  1391. '6'=>array('A','B','B','B','A','A'),
  1392. '7'=>array('A','B','A','B','A','B'),
  1393. '8'=>array('A','B','A','B','B','A'),
  1394. '9'=>array('A','B','B','A','B','A')
  1395. );
  1396. $upce_parities = array();
  1397. $upce_parities[0] = array(
  1398. '0'=>array('B','B','B','A','A','A'),
  1399. '1'=>array('B','B','A','B','A','A'),
  1400. '2'=>array('B','B','A','A','B','A'),
  1401. '3'=>array('B','B','A','A','A','B'),
  1402. '4'=>array('B','A','B','B','A','A'),
  1403. '5'=>array('B','A','A','B','B','A'),
  1404. '6'=>array('B','A','A','A','B','B'),
  1405. '7'=>array('B','A','B','A','B','A'),
  1406. '8'=>array('B','A','B','A','A','B'),
  1407. '9'=>array('B','A','A','B','A','B')
  1408. );
  1409. $upce_parities[1] = array(
  1410. '0'=>array('A','A','A','B','B','B'),
  1411. '1'=>array('A','A','B','A','B','B'),
  1412. '2'=>array('A','A','B','B','A','B'),
  1413. '3'=>array('A','A','B','B','B','A'),
  1414. '4'=>array('A','B','A','A','B','B'),
  1415. '5'=>array('A','B','B','A','A','B'),
  1416. '6'=>array('A','B','B','B','A','A'),
  1417. '7'=>array('A','B','A','B','A','B'),
  1418. '8'=>array('A','B','A','B','B','A'),
  1419. '9'=>array('A','B','B','A','B','A')
  1420. );
  1421. $k = 0;
  1422. $seq = '101'; // left guard bar
  1423. if ($upce) {
  1424. $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1425. $p = $upce_parities[$code{1}][$r];
  1426. for ($i = 0; $i < 6; ++$i) {
  1427. $seq .= $codes[$p[$i]][$upce_code{$i}];
  1428. }
  1429. $seq .= '010101'; // right guard bar
  1430. } else {
  1431. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1432. $half_len = ceil($len / 2);
  1433. if ($len == 8) {
  1434. for ($i = 0; $i < $half_len; ++$i) {
  1435. $seq .= $codes['A'][$code{$i}];
  1436. }
  1437. } else {
  1438. $p = $parities[$code{0}];
  1439. for ($i = 1; $i < $half_len; ++$i) {
  1440. $seq .= $codes[$p[$i-1]][$code{$i}];
  1441. }
  1442. }
  1443. $seq .= '01010'; // center guard bar
  1444. for ($i = $half_len; $i < $len; ++$i) {
  1445. $seq .= $codes['C'][$code{$i}];
  1446. }
  1447. $seq .= '101'; // right guard bar
  1448. }
  1449. $clen = strlen($seq);
  1450. $w = 0;
  1451. for ($i = 0; $i < $clen; ++$i) {
  1452. $w += 1;
  1453. if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq{$i} != $seq{($i+1)}))) {
  1454. if ($seq{$i} == '1') {
  1455. $t = true; // bar
  1456. } else {
  1457. $t = false; // space
  1458. }
  1459. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1460. $bararray['maxw'] += $w;
  1461. ++$k;
  1462. $w = 0;
  1463. }
  1464. }
  1465. return $bararray;
  1466. }
  1467. /**
  1468. * UPC-Based Extentions
  1469. * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
  1470. * 5-Digit Ext.: Used to mark suggested retail price of books
  1471. * @param $code (string) code to represent.
  1472. * @param $len (string) barcode type: 2 = 2-Digit, 5 = 5-Digit
  1473. * @return array barcode representation.
  1474. * @protected
  1475. */
  1476. protected function barcode_eanext($code, $len=5) {
  1477. //Padding
  1478. $code = str_pad($code, $len, '0', STR_PAD_LEFT);
  1479. // calculate check digit
  1480. if ($len == 2) {
  1481. $r = $code % 4;
  1482. } elseif ($len == 5) {
  1483. $r = (3 * ($code{0} + $code{2} + $code{4})) + (9 * ($code{1} + $code{3}));
  1484. $r %= 10;
  1485. } else {
  1486. return false;
  1487. }
  1488. //Convert digits to bars
  1489. $codes = array(
  1490. 'A'=>array( // left odd parity
  1491. '0'=>'0001101',
  1492. '1'=>'0011001',
  1493. '2'=>'0010011',
  1494. '3'=>'0111101',
  1495. '4'=>'0100011',
  1496. '5'=>'0110001',
  1497. '6'=>'0101111',
  1498. '7'=>'0111011',
  1499. '8'=>'0110111',
  1500. '9'=>'0001011'),
  1501. 'B'=>array( // left even parity
  1502. '0'=>'0100111',
  1503. '1'=>'0110011',
  1504. '2'=>'0011011',
  1505. '3'=>'0100001',
  1506. '4'=>'0011101',
  1507. '5'=>'0111001',
  1508. '6'=>'0000101',
  1509. '7'=>'0010001',
  1510. '8'=>'0001001',
  1511. '9'=>'0010111')
  1512. );
  1513. $parities = array();
  1514. $parities[2] = array(
  1515. '0'=>array('A','A'),
  1516. '1'=>array('A','B'),
  1517. '2'=>array('B','A'),
  1518. '3'=>array('B','B')
  1519. );
  1520. $parities[5] = array(
  1521. '0'=>array('B','B','A','A','A'),
  1522. '1'=>array('B','A','B','A','A'),
  1523. '2'=>array('B','A','A','B','A'),
  1524. '3'=>array('B','A','A','A','B'),
  1525. '4'=>array('A','B','B','A','A'),
  1526. '5'=>array('A','A','B','B','A'),
  1527. '6'=>arr

Large files files are truncated, but you can click here to view the full file