PageRenderTime 316ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/tcpdf/tcpdf_barcodes_1d.php

https://bitbucket.org/moodle/moodle
PHP | 2357 lines | 1884 code | 40 blank | 433 comment | 247 complexity | 1d0bf7d7fb094be1e0804dfc130abea0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0

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

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

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