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

/lib/tcpdf/tcpdf_barcodes_1d.php

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

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