PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/barcodes.php

https://github.com/felipegirotti/2D-3D-Barcodes-Generator
PHP | 2709 lines | 2132 code | 56 blank | 521 comment | 298 complexity | 663df485ef7d628a29cdaca2c4bc7185 MD5 | raw file

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

  1. <?php
  2. //============================================================+
  3. // File name : barcodes.php
  4. // @author DNS
  5. class DNS2DBarcode {
  6. /**
  7. * Array representation of barcode.
  8. * @protected
  9. */
  10. protected $barcode_array = false;
  11. /**
  12. *path to save png in getBarcodePNGPath
  13. * @var <type>
  14. */
  15. public $save_path;
  16. /**
  17. * Return an array representations of barcode.
  18. * @return array
  19. */
  20. public function getBarcodeArray() {
  21. return $this->barcode_array;
  22. }
  23. /**
  24. * <li>$arrcode['code'] code to be printed on text label</li>
  25. * <li>$arrcode['num_rows'] required number of rows</li>
  26. * <li>$arrcode['num_cols'] required number of columns</li>
  27. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  28. * @param $code (string) code to print
  29. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  30. * Send barcode as SVG image object to the standard output.
  31. * @param $w (int) Width of a single rectangle element in user units.
  32. * @param $h (int) Height of a single rectangle element in user units.
  33. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  34. * @public
  35. */
  36. public function getBarcodeSVG($code, $type, $w=3, $h=3, $color='black') {
  37. //set barcode code and type
  38. $this->setBarcode($code, $type);
  39. // send headers
  40. $code = $this->getBarcodeSVGcode($w, $h, $color);
  41. header('Content-Type: application/svg+xml');
  42. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  43. header('Pragma: public');
  44. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  45. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  46. header('Content-Disposition: inline; filename="' . md5($code) . '.svg";');
  47. //header('Content-Length: '.strlen($code));
  48. echo $code;
  49. }
  50. /**
  51. * Return a SVG string representation of barcode.
  52. * <li>$arrcode['code'] code to be printed on text label</li>
  53. * <li>$arrcode['num_rows'] required number of rows</li>
  54. * <li>$arrcode['num_cols'] required number of columns</li>
  55. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  56. * @param $code (string) code to print
  57. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  58. * @param $w (int) Width of a single rectangle element in user units.
  59. * @param $h (int) Height of a single rectangle element in user units.
  60. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  61. * @return string SVG code.
  62. * @public
  63. */
  64. public function getBarcodeSVGcode($code, $type, $w=3, $h=3, $color='black') {
  65. //set barcode code and type
  66. $this->setBarcode($code, $type);
  67. // replace table for special characters
  68. $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  69. $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
  70. $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
  71. $svg .= '<svg width="' . round(($this->barcode_array['num_cols'] * $w), 3) . '" height="' . round(($this->barcode_array['num_rows'] * $h), 3) . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
  72. $svg .= "\t" . '<desc>' . strtr($this->barcode_array['code'], $repstr) . '</desc>' . "\n";
  73. $svg .= "\t" . '<g id="elements" fill="' . $color . '" stroke="none">' . "\n";
  74. // print barcode elements
  75. $y = 0;
  76. // for each row
  77. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  78. $x = 0;
  79. // for each column
  80. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  81. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  82. // draw a single barcode cell
  83. $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $w . '" height="' . $h . '" />' . "\n";
  84. }
  85. $x += $w;
  86. }
  87. $y += $h;
  88. }
  89. $svg .= "\t" . '</g>' . "\n";
  90. $svg .= '</svg>' . "\n";
  91. return $svg;
  92. }
  93. /**
  94. * Return an HTML representation of barcode.
  95. * <li>$arrcode['code'] code to be printed on text label</li>
  96. * <li>$arrcode['num_rows'] required number of rows</li>
  97. * <li>$arrcode['num_cols'] required number of columns</li>
  98. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  99. * @param $code (string) code to print
  100. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  101. * @param $w (int) Width of a single rectangle element in pixels.
  102. * @param $h (int) Height of a single rectangle element in pixels.
  103. * @param $color (string) Foreground color for bar elements (background is transparent).
  104. * @return string HTML code.
  105. * @public
  106. */
  107. public function getBarcodeHTML($code, $type, $w=10, $h=10, $color='black') {
  108. //set barcode code and type
  109. $this->setBarcode($code, $type);
  110. $html = '<div style="font-size:0;position:relative;width:' . ($w * $this->barcode_array['num_cols']) . 'px;height:' . ($h * $this->barcode_array['num_rows']) . 'px;">' . "\n";
  111. // print barcode elements
  112. $y = 0;
  113. // for each row
  114. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  115. $x = 0;
  116. // for each column
  117. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  118. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  119. // draw a single barcode cell
  120. $html .= '<div style="background-color:' . $color . ';width:' . $w . 'px;height:' . $h . 'px;position:absolute;left:' . $x . 'px;top:' . $y . 'px;">&nbsp;</div>' . "\n";
  121. }
  122. $x += $w;
  123. }
  124. $y += $h;
  125. }
  126. $html .= '</div>' . "\n";
  127. return $html;
  128. }
  129. /**
  130. * Return a PNG image representation of barcode (requires GD or Imagick library).
  131. * <li>$arrcode['code'] code to be printed on text label</li>
  132. * <li>$arrcode['num_rows'] required number of rows</li>
  133. * <li>$arrcode['num_cols'] required number of columns</li>
  134. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  135. * @param $code (string) code to print
  136. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  137. * @param $w (int) Width of a single rectangle element in pixels.
  138. * @param $h (int) Height of a single rectangle element in pixels.
  139. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  140. * @return image or false in case of error.
  141. * @public
  142. */
  143. public function getBarcodePNG($code, $type, $w=3, $h=3, $color=array(0, 0, 0)) {
  144. //set barcode code and type
  145. $this->setBarcode($code, $type);
  146. // calculate image size
  147. $width = ($this->barcode_array['num_cols'] * $w);
  148. $height = ($this->barcode_array['num_rows'] * $h);
  149. if (function_exists('imagecreate')) {
  150. // GD library
  151. $imagick = false;
  152. $png = imagecreate($width, $height);
  153. $bgcol = imagecolorallocate($png, 255, 255, 255);
  154. imagecolortransparent($png, $bgcol);
  155. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  156. } elseif (extension_loaded('imagick')) {
  157. $imagick = true;
  158. $bgcol = new imagickpixel('rgb(255,255,255');
  159. $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
  160. $png = new Imagick();
  161. $png->newImage($width, $height, 'none', 'png');
  162. $bar = new imagickdraw();
  163. $bar->setfillcolor($fgcol);
  164. } else {
  165. return false;
  166. }
  167. // print barcode elements
  168. $y = 0;
  169. // for each row
  170. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  171. $x = 0;
  172. // for each column
  173. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  174. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  175. // draw a single barcode cell
  176. if ($imagick) {
  177. $bar->rectangle($x, $y, ($x + $w), ($y + $h));
  178. } else {
  179. imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
  180. }
  181. }
  182. $x += $w;
  183. }
  184. $y += $h;
  185. }
  186. // send headers
  187. header('Content-Type: image/png');
  188. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  189. header('Pragma: public');
  190. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  191. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  192. if ($imagick) {
  193. $png->drawimage($bar);
  194. echo $png;
  195. } else {
  196. imagepng($png);
  197. imagedestroy($png);
  198. }
  199. }
  200. /**
  201. * Return a .png file path which create in server
  202. * <li>$arrcode['code'] code to be printed on text label</li>
  203. * <li>$arrcode['num_rows'] required number of rows</li>
  204. * <li>$arrcode['num_cols'] required number of columns</li>
  205. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  206. * @param $code (string) code to print
  207. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  208. * @param $w (int) Width of a single rectangle element in pixels.
  209. * @param $h (int) Height of a single rectangle element in pixels.
  210. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  211. * @return path of image whice created
  212. * @public
  213. */
  214. public function getBarcodePNGPath($code, $type, $w=3, $h=3, $color=array(0, 0, 0)) {
  215. //set barcode code and type
  216. $this->setBarcode($code, $type);
  217. // calculate image size
  218. $width = ($this->barcode_array['num_cols'] * $w);
  219. $height = ($this->barcode_array['num_rows'] * $h);
  220. if (function_exists('imagecreate')) {
  221. // GD library
  222. $imagick = false;
  223. $png = imagecreate($width, $height);
  224. $bgcol = imagecolorallocate($png, 255, 255, 255);
  225. imagecolortransparent($png, $bgcol);
  226. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  227. } elseif (extension_loaded('imagick')) {
  228. $imagick = true;
  229. $bgcol = new imagickpixel('rgb(255,255,255');
  230. $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
  231. $png = new Imagick();
  232. $png->newImage($width, $height, 'none', 'png');
  233. $bar = new imagickdraw();
  234. $bar->setfillcolor($fgcol);
  235. } else {
  236. return false;
  237. }
  238. // print barcode elements
  239. $y = 0;
  240. // for each row
  241. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  242. $x = 0;
  243. // for each column
  244. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  245. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  246. // draw a single barcode cell
  247. if ($imagick) {
  248. $bar->rectangle($x, $y, ($x + $w), ($y + $h));
  249. } else {
  250. imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
  251. }
  252. }
  253. $x += $w;
  254. }
  255. $y += $h;
  256. }
  257. $save_file = $this->checkfile($this->save_path.$code . ".png");
  258. if ($imagick) {
  259. $png->drawimage($bar);
  260. //echo $png;
  261. }
  262. if (ImagePng($png, $save_file)) {
  263. imagedestroy($png);
  264. return $save_file;
  265. } else {
  266. imagedestroy($png);
  267. return $code;
  268. }
  269. }
  270. /**
  271. * Set the barcode.
  272. * @param $code (string) code to print
  273. * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
  274. * @return array
  275. */
  276. public function setBarcode($code, $type) {
  277. $mode = explode(',', $type);
  278. $qrtype = strtoupper($mode[0]);
  279. switch ($qrtype) {
  280. case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
  281. require_once(dirname(__FILE__) . '/datamatrix.php');
  282. $qrcode = new Datamatrix($code);
  283. $this->barcode_array = $qrcode->getBarcodeArray();
  284. $this->barcode_array['code'] = $code;
  285. break;
  286. }
  287. case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
  288. require_once(dirname(__FILE__) . '/pdf417.php');
  289. if (!isset($mode[1]) OR ($mode[1] === '')) {
  290. $aspectratio = 2; // default aspect ratio (width / height)
  291. } else {
  292. $aspectratio = floatval($mode[1]);
  293. }
  294. if (!isset($mode[2]) OR ($mode[2] === '')) {
  295. $ecl = -1; // default error correction level (auto)
  296. } else {
  297. $ecl = intval($mode[2]);
  298. }
  299. // set macro block
  300. $macro = array();
  301. if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) {
  302. $macro['segment_total'] = intval($mode[3]);
  303. $macro['segment_index'] = intval($mode[4]);
  304. $macro['file_id'] = strtr($mode[5], "\xff", ',');
  305. for ($i = 0; $i < 7; ++$i) {
  306. $o = $i + 6;
  307. if (isset($mode[$o]) AND ($mode[$o] !== '')) {
  308. // add option
  309. $macro['option_' . $i] = strtr($mode[$o], "\xff", ',');
  310. }
  311. }
  312. }
  313. $qrcode = new PDF417($code, $ecl, $aspectratio, $macro);
  314. $this->barcode_array = $qrcode->getBarcodeArray();
  315. $this->barcode_array['code'] = $code;
  316. break;
  317. }
  318. case 'QRCODE': { // QR-CODE
  319. require_once(dirname(__FILE__) . '/qrcode.php');
  320. if (!isset($mode[1]) OR (!in_array($mode[1], array('L', 'M', 'Q', 'H')))) {
  321. $mode[1] = 'L'; // Ddefault: Low error correction
  322. }
  323. $qrcode = new QRcode($code, strtoupper($mode[1]));
  324. $this->barcode_array = $qrcode->getBarcodeArray();
  325. $this->barcode_array['code'] = $code;
  326. break;
  327. }
  328. case 'RAW':
  329. case 'RAW2': { // RAW MODE
  330. // remove spaces
  331. $code = preg_replace('/[\s]*/si', '', $code);
  332. if (strlen($code) < 3) {
  333. break;
  334. }
  335. if ($qrtype == 'RAW') {
  336. // comma-separated rows
  337. $rows = explode(',', $code);
  338. } else { // RAW2
  339. // rows enclosed in square parentheses
  340. $code = substr($code, 1, -1);
  341. $rows = explode('][', $code);
  342. }
  343. $this->barcode_array['num_rows'] = count($rows);
  344. $this->barcode_array['num_cols'] = strlen($rows[0]);
  345. $this->barcode_array['bcode'] = array();
  346. foreach ($rows as $r) {
  347. $this->barcode_array['bcode'][] = str_split($r, 1);
  348. }
  349. $this->barcode_array['code'] = $code;
  350. break;
  351. }
  352. default: {
  353. $this->barcode_array = false;
  354. }
  355. }
  356. }
  357. public function checkfile($path) {
  358. if (file_exists($path)) {
  359. $base_name = pathinfo($path, PATHINFO_BASENAME);
  360. return $this->checkfile(str_replace($base_name, rand(0, 9999) . $base_name, $path));
  361. } else {
  362. return $path;
  363. }
  364. }
  365. }
  366. // end of class
  367. //============================================================+
  368. class DNS1DBarcode {
  369. /**
  370. * Array representation of barcode.
  371. * @protected
  372. */
  373. protected $barcode_array;
  374. /**
  375. *path to save png in getBarcodePNGPath
  376. * @var <type>
  377. */
  378. public $save_path;
  379. /**
  380. * This is the class constructor.
  381. * Return an array representations for common 1D barcodes:<ul>
  382. * <li>$arrcode['code'] code to be printed on text label</li>
  383. * <li>$arrcode['maxh'] max barcode height</li>
  384. * <li>$arrcode['maxw'] max barcode width</li>
  385. * <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
  386. * <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
  387. * <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
  388. * <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
  389. * <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
  390. * @param $code (string) code to print
  391. * @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>
  392. * @public
  393. */
  394. /**
  395. * Return an array representations of barcode.
  396. * @return array
  397. * @public
  398. */
  399. public function getBarcodeArray() {
  400. return $this->barcode_array;
  401. }
  402. /**
  403. * Send barcode as SVG image object to the standard output.
  404. * @param $code (string) code to print
  405. * @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>
  406. * @param $w (int) Minimum width of a single bar in user units.
  407. * @param $h (int) Height of barcode in user units.
  408. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  409. * @public
  410. */
  411. public function getBarcodeSVG($code, $type,$w=2, $h=30, $color='black') {
  412. $this->setBarcode($code, $type);
  413. // send headers
  414. $code = $this->getBarcodeSVGcode($w, $h, $color);
  415. header('Content-Type: application/svg+xml');
  416. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  417. header('Pragma: public');
  418. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  419. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  420. header('Content-Disposition: inline; filename="'.md5($code).'.svg";');
  421. //header('Content-Length: '.strlen($code));
  422. echo $code;
  423. }
  424. /**
  425. * Return a SVG string representation of barcode.
  426. * @param $code (string) code to print
  427. * @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>
  428. * @param $w (int) Minimum width of a single bar in user units.
  429. * @param $h (int) Height of barcode in user units.
  430. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  431. * @return string SVG code.
  432. * @public
  433. */
  434. public function getBarcodeSVGcode($code, $type,$w=2, $h=30, $color='black') {
  435. $this->setBarcode($code, $type);
  436. // replace table for special characters
  437. $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  438. $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
  439. $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
  440. $svg .= '<svg width="'.round(($this->barcode_array['maxw'] * $w), 3).'" height="'.$h.'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n";
  441. $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
  442. $svg .= "\t".'<g id="bars" fill="'.$color.'" stroke="none">'."\n";
  443. // print bars
  444. $x = 0;
  445. foreach ($this->barcode_array['bcode'] as $k => $v) {
  446. $bw = round(($v['w'] * $w), 3);
  447. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  448. if ($v['t']) {
  449. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  450. // draw a vertical bar
  451. $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$bw.'" height="'.$bh.'" />'."\n";
  452. }
  453. $x += $bw;
  454. }
  455. $svg .= "\t".'</g>'."\n";
  456. $svg .= '</svg>'."\n";
  457. return $svg;
  458. }
  459. /**
  460. * Return an HTML representation of barcode.
  461. * @param $code (string) code to print
  462. * @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>
  463. * @param $w (int) Width of a single bar element in pixels.
  464. * @param $h (int) Height of a single bar element in pixels.
  465. * @param $color (string) Foreground color for bar elements (background is transparent).
  466. * @return string HTML code.
  467. * @public
  468. */
  469. public function getBarcodeHTML($code, $type,$w=2, $h=30, $color='black') {
  470. $this->setBarcode($code, $type);
  471. $html = '<div style="font-size:0;position:relative;">'."\n";
  472. $html = '<div style="font-size:0;position:relative;width:'.($this->barcode_array['maxw'] * $w).'px;height:'.($h).'px;">'."\n";
  473. // print bars
  474. $x = 0;
  475. foreach ($this->barcode_array['bcode'] as $k => $v) {
  476. $bw = round(($v['w'] * $w), 3);
  477. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  478. if ($v['t']) {
  479. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  480. // draw a vertical bar
  481. $html .= '<div style="background-color:'.$color.';width:'.$bw.'px;height:'.$bh.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;">&nbsp;</div>'."\n";
  482. }
  483. $x += $bw;
  484. }
  485. $html .= '</div>'."\n";
  486. return $html;
  487. }
  488. /**
  489. * Return a PNG image representation of barcode (requires GD or Imagick library).
  490. * @param $code (string) code to print
  491. * @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>
  492. * @param $w (int) Width of a single bar element in pixels.
  493. * @param $h (int) Height of a single bar element in pixels.
  494. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  495. * @return image or false in case of error.
  496. * @public
  497. */
  498. public function getBarcodePNG($code, $type,$w=2, $h=30, $color=array(0,0,0)) {
  499. $this->setBarcode($code, $type);
  500. // calculate image size
  501. $width = ($this->barcode_array['maxw'] * $w);
  502. $height = $h;
  503. if (function_exists('imagecreate')) {
  504. // GD library
  505. $imagick = false;
  506. $png = imagecreate($width, $height);
  507. $bgcol = imagecolorallocate($png, 255, 255, 255);
  508. imagecolortransparent($png, $bgcol);
  509. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  510. } elseif (extension_loaded('imagick')) {
  511. $imagick = true;
  512. $bgcol = new imagickpixel('rgb(255,255,255');
  513. $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
  514. $png = new Imagick();
  515. $png->newImage($width, $height, 'none', 'png');
  516. $bar = new imagickdraw();
  517. $bar->setfillcolor($fgcol);
  518. } else {
  519. return false;
  520. }
  521. // print bars
  522. $x = 0;
  523. foreach ($this->barcode_array['bcode'] as $k => $v) {
  524. $bw = round(($v['w'] * $w), 3);
  525. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  526. if ($v['t']) {
  527. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  528. // draw a vertical bar
  529. if ($imagick) {
  530. $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
  531. } else {
  532. imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
  533. }
  534. }
  535. $x += $bw;
  536. }
  537. // send headers
  538. header('Content-Type: image/png');
  539. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  540. header('Pragma: public');
  541. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  542. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  543. if ($imagick) {
  544. $png->drawimage($bar);
  545. echo $png;
  546. } else {
  547. imagepng($png);
  548. imagedestroy($png);
  549. }
  550. }
  551. /**
  552. * Return a .png file path which create in server
  553. * @param $code (string) code to print
  554. * @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>
  555. * @param $w (int) Width of a single bar element in pixels.
  556. * @param $h (int) Height of a single bar element in pixels.
  557. * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
  558. * @return image or false in case of error.
  559. * @public
  560. */
  561. public function getBarcodePNGPath($code, $type,$w=2, $h=30, $color=array(0,0,0)) {
  562. $this->setBarcode($code, $type);
  563. // calculate image size
  564. $width = ($this->barcode_array['maxw'] * $w);
  565. $height = $h;
  566. if (function_exists('imagecreate')) {
  567. // GD library
  568. $imagick = false;
  569. $png = imagecreate($width, $height);
  570. $bgcol = imagecolorallocate($png, 255, 255, 255);
  571. imagecolortransparent($png, $bgcol);
  572. $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
  573. } elseif (extension_loaded('imagick')) {
  574. $imagick = true;
  575. $bgcol = new imagickpixel('rgb(255,255,255');
  576. $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
  577. $png = new Imagick();
  578. $png->newImage($width, $height, 'none', 'png');
  579. $bar = new imagickdraw();
  580. $bar->setfillcolor($fgcol);
  581. } else {
  582. return false;
  583. }
  584. // print bars
  585. $x = 0;
  586. foreach ($this->barcode_array['bcode'] as $k => $v) {
  587. $bw = round(($v['w'] * $w), 3);
  588. $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
  589. if ($v['t']) {
  590. $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
  591. // draw a vertical bar
  592. if ($imagick) {
  593. $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
  594. } else {
  595. imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
  596. }
  597. }
  598. $x += $bw;
  599. }
  600. $save_file = $this->checkfile($this->save_path.$code . ".png");
  601. if ($imagick) {
  602. $png->drawimage($bar);
  603. //echo $png;
  604. }
  605. if (ImagePng($png, $save_file)) {
  606. imagedestroy($png);
  607. return $save_file;
  608. } else {
  609. imagedestroy($png);
  610. return $code;
  611. }
  612. }
  613. /**
  614. * Set the barcode.
  615. * @param $code (string) code to print
  616. * @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>
  617. * @return array barcode array
  618. * @public
  619. */
  620. public function setBarcode($code, $type) {
  621. switch (strtoupper($type)) {
  622. case 'C39': { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  623. $arrcode = $this->barcode_code39($code, false, false);
  624. break;
  625. }
  626. case 'C39+': { // CODE 39 with checksum
  627. $arrcode = $this->barcode_code39($code, false, true);
  628. break;
  629. }
  630. case 'C39E': { // CODE 39 EXTENDED
  631. $arrcode = $this->barcode_code39($code, true, false);
  632. break;
  633. }
  634. case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
  635. $arrcode = $this->barcode_code39($code, true, true);
  636. break;
  637. }
  638. case 'C93': { // CODE 93 - USS-93
  639. $arrcode = $this->barcode_code93($code);
  640. break;
  641. }
  642. case 'S25': { // Standard 2 of 5
  643. $arrcode = $this->barcode_s25($code, false);
  644. break;
  645. }
  646. case 'S25+': { // Standard 2 of 5 + CHECKSUM
  647. $arrcode = $this->barcode_s25($code, true);
  648. break;
  649. }
  650. case 'I25': { // Interleaved 2 of 5
  651. $arrcode = $this->barcode_i25($code, false);
  652. break;
  653. }
  654. case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
  655. $arrcode = $this->barcode_i25($code, true);
  656. break;
  657. }
  658. case 'C128': { // CODE 128
  659. $arrcode = $this->barcode_c128($code, '');
  660. break;
  661. }
  662. case 'C128A': { // CODE 128 A
  663. $arrcode = $this->barcode_c128($code, 'A');
  664. break;
  665. }
  666. case 'C128B': { // CODE 128 B
  667. $arrcode = $this->barcode_c128($code, 'B');
  668. break;
  669. }
  670. case 'C128C': { // CODE 128 C
  671. $arrcode = $this->barcode_c128($code, 'C');
  672. break;
  673. }
  674. case 'EAN2': { // 2-Digits UPC-Based Extention
  675. $arrcode = $this->barcode_eanext($code, 2);
  676. break;
  677. }
  678. case 'EAN5': { // 5-Digits UPC-Based Extention
  679. $arrcode = $this->barcode_eanext($code, 5);
  680. break;
  681. }
  682. case 'EAN8': { // EAN 8
  683. $arrcode = $this->barcode_eanupc($code, 8);
  684. break;
  685. }
  686. case 'EAN13': { // EAN 13
  687. $arrcode = $this->barcode_eanupc($code, 13);
  688. break;
  689. }
  690. case 'UPCA': { // UPC-A
  691. $arrcode = $this->barcode_eanupc($code, 12);
  692. break;
  693. }
  694. case 'UPCE': { // UPC-E
  695. $arrcode = $this->barcode_eanupc($code, 6);
  696. break;
  697. }
  698. case 'MSI': { // MSI (Variation of Plessey code)
  699. $arrcode = $this->barcode_msi($code, false);
  700. break;
  701. }
  702. case 'MSI+': { // MSI + CHECKSUM (modulo 11)
  703. $arrcode = $this->barcode_msi($code, true);
  704. break;
  705. }
  706. case 'POSTNET': { // POSTNET
  707. $arrcode = $this->barcode_postnet($code, false);
  708. break;
  709. }
  710. case 'PLANET': { // PLANET
  711. $arrcode = $this->barcode_postnet($code, true);
  712. break;
  713. }
  714. case 'RMS4CC': { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
  715. $arrcode = $this->barcode_rms4cc($code, false);
  716. break;
  717. }
  718. case 'KIX': { // KIX (Klant index - Customer index)
  719. $arrcode = $this->barcode_rms4cc($code, true);
  720. break;
  721. }
  722. case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  723. $arrcode = $this->barcode_imb($code);
  724. break;
  725. }
  726. case 'CODABAR': { // CODABAR
  727. $arrcode = $this->barcode_codabar($code);
  728. break;
  729. }
  730. case 'CODE11': { // CODE 11
  731. $arrcode = $this->barcode_code11($code);
  732. break;
  733. }
  734. case 'PHARMA': { // PHARMACODE
  735. $arrcode = $this->barcode_pharmacode($code);
  736. break;
  737. }
  738. case 'PHARMA2T': { // PHARMACODE TWO-TRACKS
  739. $arrcode = $this->barcode_pharmacode2t($code);
  740. break;
  741. }
  742. default: {
  743. $this->barcode_array = false;
  744. $arrcode = false;
  745. break;
  746. }
  747. }
  748. $this->barcode_array = $arrcode;
  749. }
  750. /**
  751. * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  752. * General-purpose code in very wide use world-wide
  753. * @param $code (string) code to represent.
  754. * @param $extended (boolean) if true uses the extended mode.
  755. * @param $checksum (boolean) if true add a checksum to the code.
  756. * @return array barcode representation.
  757. * @protected
  758. */
  759. protected function barcode_code39($code, $extended=false, $checksum=false) {
  760. $chr['0'] = '111331311';
  761. $chr['1'] = '311311113';
  762. $chr['2'] = '113311113';
  763. $chr['3'] = '313311111';
  764. $chr['4'] = '111331113';
  765. $chr['5'] = '311331111';
  766. $chr['6'] = '113331111';
  767. $chr['7'] = '111311313';
  768. $chr['8'] = '311311311';
  769. $chr['9'] = '113311311';
  770. $chr['A'] = '311113113';
  771. $chr['B'] = '113113113';
  772. $chr['C'] = '313113111';
  773. $chr['D'] = '111133113';
  774. $chr['E'] = '311133111';
  775. $chr['F'] = '113133111';
  776. $chr['G'] = '111113313';
  777. $chr['H'] = '311113311';
  778. $chr['I'] = '113113311';
  779. $chr['J'] = '111133311';
  780. $chr['K'] = '311111133';
  781. $chr['L'] = '113111133';
  782. $chr['M'] = '313111131';
  783. $chr['N'] = '111131133';
  784. $chr['O'] = '311131131';
  785. $chr['P'] = '113131131';
  786. $chr['Q'] = '111111333';
  787. $chr['R'] = '311111331';
  788. $chr['S'] = '113111331';
  789. $chr['T'] = '111131331';
  790. $chr['U'] = '331111113';
  791. $chr['V'] = '133111113';
  792. $chr['W'] = '333111111';
  793. $chr['X'] = '131131113';
  794. $chr['Y'] = '331131111';
  795. $chr['Z'] = '133131111';
  796. $chr['-'] = '131111313';
  797. $chr['.'] = '331111311';
  798. $chr[' '] = '133111311';
  799. $chr['$'] = '131313111';
  800. $chr['/'] = '131311131';
  801. $chr['+'] = '131113131';
  802. $chr['%'] = '111313131';
  803. $chr['*'] = '131131311';
  804. $code = strtoupper($code);
  805. if ($extended) {
  806. // extended mode
  807. $code = $this->encode_code39_ext($code);
  808. }
  809. if ($code === false) {
  810. return false;
  811. }
  812. if ($checksum) {
  813. // checksum
  814. $code .= $this->checksum_code39($code);
  815. }
  816. // add start and stop codes
  817. $code = '*'.$code.'*';
  818. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  819. $k = 0;
  820. $clen = strlen($code);
  821. for ($i = 0; $i < $clen; ++$i) {
  822. $char = $code{$i};
  823. if(!isset($chr[$char])) {
  824. // invalid character
  825. return false;
  826. }
  827. for ($j = 0; $j < 9; ++$j) {
  828. if (($j % 2) == 0) {
  829. $t = true; // bar
  830. } else {
  831. $t = false; // space
  832. }
  833. $w = $chr[$char]{$j};
  834. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  835. $bararray['maxw'] += $w;
  836. ++$k;
  837. }
  838. // intercharacter gap
  839. $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
  840. $bararray['maxw'] += 1;
  841. ++$k;
  842. }
  843. return $bararray;
  844. }
  845. /**
  846. * Encode a string to be used for CODE 39 Extended mode.
  847. * @param $code (string) code to represent.
  848. * @return encoded string.
  849. * @protected
  850. */
  851. protected function encode_code39_ext($code) {
  852. $encode = array(
  853. chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
  854. chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
  855. chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
  856. chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
  857. chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
  858. chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
  859. chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
  860. chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
  861. chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
  862. chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
  863. chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
  864. chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
  865. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  866. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  867. chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
  868. chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
  869. chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  870. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  871. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  872. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  873. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  874. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  875. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
  876. chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
  877. chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
  878. chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
  879. chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
  880. chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
  881. chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
  882. chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
  883. chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
  884. chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
  885. $code_ext = '';
  886. $clen = strlen($code);
  887. for ($i = 0 ; $i < $clen; ++$i) {
  888. if (ord($code{$i}) > 127) {
  889. return false;
  890. }
  891. $code_ext .= $encode[$code{$i}];
  892. }
  893. return $code_ext;
  894. }
  895. /**
  896. * Calculate CODE 39 checksum (modulo 43).
  897. * @param $code (string) code to represent.
  898. * @return char checksum.
  899. * @protected
  900. */
  901. protected function checksum_code39($code) {
  902. $chars = array(
  903. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  904. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  905. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  906. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
  907. $sum = 0;
  908. $clen = strlen($code);
  909. for ($i = 0 ; $i < $clen; ++$i) {
  910. $k = array_keys($chars, $code{$i});
  911. $sum += $k[0];
  912. }
  913. $j = ($sum % 43);
  914. return $chars[$j];
  915. }
  916. /**
  917. * CODE 93 - USS-93
  918. * Compact code similar …

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