tcpdf_barcodes_2d.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf_barcodes_2d.php
  4. // Version : 1.0.014
  5. // Begin : 2009-04-07
  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) 2009-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. // 2D barcodes to be used with TCPDF.
  32. //
  33. //============================================================+
  34. /**
  35. * @file
  36. * PHP class to creates array representations for 2D barcodes to be used with TCPDF.
  37. * @package com.tecnick.tcpdf
  38. * @author Nicola Asuni
  39. * @version 1.0.014
  40. */
  41. /**
  42. * @class TCPDF2DBarcode
  43. * PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org).
  44. * @package com.tecnick.tcpdf
  45. * @version 1.0.014
  46. * @author Nicola Asuni
  47. */
  48. class TCPDF2DBarcode {
  49. /**
  50. * Array representation of barcode.
  51. * @protected
  52. */
  53. protected $barcode_array = false;
  54. /**
  55. * This is the class constructor.
  56. * Return an array representations for 2D barcodes:<ul>
  57. * <li>$arrcode['code'] code to be printed on text label</li>
  58. * <li>$arrcode['num_rows'] required number of rows</li>
  59. * <li>$arrcode['num_cols'] required number of columns</li>
  60. * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
  61. * @param $code (string) code to print
  62. * @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>
  63. */
  64. public function __construct($code, $type) {
  65. $this->setBarcode($code, $type);
  66. }
  67. /**
  68. * Return an array representations of barcode.
  69. * @return array
  70. */
  71. public function getBarcodeArray() {
  72. return $this->barcode_array;
  73. }
  74. /**
  75. * Send barcode as SVG image object to the standard output.
  76. * @param $w (int) Width of a single rectangle element in user units.
  77. * @param $h (int) Height of a single rectangle element in user units.
  78. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  79. * @public
  80. */
  81. public function getBarcodeSVG($w=3, $h=3, $color='black') {
  82. // send headers
  83. $code = $this->getBarcodeSVGcode($w, $h, $color);
  84. header('Content-Type: application/svg+xml');
  85. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  86. header('Pragma: public');
  87. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  88. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  89. header('Content-Disposition: inline; filename="'.md5($code).'.svg";');
  90. //header('Content-Length: '.strlen($code));
  91. echo $code;
  92. }
  93. /**
  94. * Return a SVG string representation of barcode.
  95. * @param $w (int) Width of a single rectangle element in user units.
  96. * @param $h (int) Height of a single rectangle element in user units.
  97. * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
  98. * @return string SVG code.
  99. * @public
  100. */
  101. public function getBarcodeSVGcode($w=3, $h=3, $color='black') {
  102. // replace table for special characters
  103. $repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  104. $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
  105. $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
  106. $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";
  107. $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
  108. $svg .= "\t".'<g id="elements" fill="'.$color.'" stroke="none">'."\n";
  109. // print barcode elements
  110. $y = 0;
  111. // for each row
  112. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  113. $x = 0;
  114. // for each column
  115. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  116. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  117. // draw a single barcode cell
  118. $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$w.'" height="'.$h.'" />'."\n";
  119. }
  120. $x += $w;
  121. }
  122. $y += $h;
  123. }
  124. $svg .= "\t".'</g>'."\n";
  125. $svg .= '</svg>'."\n";
  126. return $svg;
  127. }
  128. /**
  129. * Return an HTML representation of barcode.
  130. * @param $w (int) Width of a single rectangle element in pixels.
  131. * @param $h (int) Height of a single rectangle element in pixels.
  132. * @param $color (string) Foreground color for bar elements (background is transparent).
  133. * @return string HTML code.
  134. * @public
  135. */
  136. public function getBarcodeHTML($w=10, $h=10, $color='black') {
  137. $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";
  138. // print barcode elements
  139. $y = 0;
  140. // for each row
  141. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  142. $x = 0;
  143. // for each column
  144. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  145. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  146. // draw a single barcode cell
  147. $html .= '<div style="background-color:'.$color.';width:'.$w.'px;height:'.$h.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;">&nbsp;</div>'."\n";
  148. }
  149. $x += $w;
  150. }
  151. $y += $h;
  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 rectangle element in pixels.
  159. * @param $h (int) Height of a single rectangle 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=3, $h=3, $color=array(0,0,0)) {
  165. // calculate image size
  166. $width = ($this->barcode_array['num_cols'] * $w);
  167. $height = ($this->barcode_array['num_rows'] * $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 barcode elements
  187. $y = 0;
  188. // for each row
  189. for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
  190. $x = 0;
  191. // for each column
  192. for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
  193. if ($this->barcode_array['bcode'][$r][$c] == 1) {
  194. // draw a single barcode cell
  195. if ($imagick) {
  196. $bar->rectangle($x, $y, ($x + $w - 1), ($y + $h - 1));
  197. } else {
  198. imagefilledrectangle($png, $x, $y, ($x + $w - 1), ($y + $h - 1), $fgcol);
  199. }
  200. }
  201. $x += $w;
  202. }
  203. $y += $h;
  204. }
  205. // send headers
  206. header('Content-Type: image/png');
  207. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  208. header('Pragma: public');
  209. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  210. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  211. if ($imagick) {
  212. $png->drawimage($bar);
  213. echo $png;
  214. } else {
  215. imagepng($png);
  216. imagedestroy($png);
  217. }
  218. }
  219. /**
  220. * Set the barcode.
  221. * @param $code (string) code to print
  222. * @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>
  223. * @return array
  224. */
  225. public function setBarcode($code, $type) {
  226. $mode = explode(',', $type);
  227. $qrtype = strtoupper($mode[0]);
  228. switch ($qrtype) {
  229. case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
  230. require_once(dirname(__FILE__).'/include/barcodes/datamatrix.php');
  231. $qrcode = new Datamatrix($code);
  232. $this->barcode_array = $qrcode->getBarcodeArray();
  233. $this->barcode_array['code'] = $code;
  234. break;
  235. }
  236. case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
  237. require_once(dirname(__FILE__).'/include/barcodes/pdf417.php');
  238. if (!isset($mode[1]) OR ($mode[1] === '')) {
  239. $aspectratio = 2; // default aspect ratio (width / height)
  240. } else {
  241. $aspectratio = floatval($mode[1]);
  242. }
  243. if (!isset($mode[2]) OR ($mode[2] === '')) {
  244. $ecl = -1; // default error correction level (auto)
  245. } else {
  246. $ecl = intval($mode[2]);
  247. }
  248. // set macro block
  249. $macro = array();
  250. if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) {
  251. $macro['segment_total'] = intval($mode[3]);
  252. $macro['segment_index'] = intval($mode[4]);
  253. $macro['file_id'] = strtr($mode[5], "\xff", ',');
  254. for ($i = 0; $i < 7; ++$i) {
  255. $o = $i + 6;
  256. if (isset($mode[$o]) AND ($mode[$o] !== '')) {
  257. // add option
  258. $macro['option_'.$i] = strtr($mode[$o], "\xff", ',');
  259. }
  260. }
  261. }
  262. $qrcode = new PDF417($code, $ecl, $aspectratio, $macro);
  263. $this->barcode_array = $qrcode->getBarcodeArray();
  264. $this->barcode_array['code'] = $code;
  265. break;
  266. }
  267. case 'QRCODE': { // QR-CODE
  268. require_once(dirname(__FILE__).'/include/barcodes/qrcode.php');
  269. if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) {
  270. $mode[1] = 'L'; // Ddefault: Low error correction
  271. }
  272. $qrcode = new QRcode($code, strtoupper($mode[1]));
  273. $this->barcode_array = $qrcode->getBarcodeArray();
  274. $this->barcode_array['code'] = $code;
  275. break;
  276. }
  277. case 'RAW':
  278. case 'RAW2': { // RAW MODE
  279. // remove spaces
  280. $code = preg_replace('/[\s]*/si', '', $code);
  281. if (strlen($code) < 3) {
  282. break;
  283. }
  284. if ($qrtype == 'RAW') {
  285. // comma-separated rows
  286. $rows = explode(',', $code);
  287. } else { // RAW2
  288. // rows enclosed in square parentheses
  289. $code = substr($code, 1, -1);
  290. $rows = explode('][', $code);
  291. }
  292. $this->barcode_array['num_rows'] = count($rows);
  293. $this->barcode_array['num_cols'] = strlen($rows[0]);
  294. $this->barcode_array['bcode'] = array();
  295. foreach ($rows as $r) {
  296. $this->barcode_array['bcode'][] = str_split($r, 1);
  297. }
  298. $this->barcode_array['code'] = $code;
  299. break;
  300. }
  301. case 'TEST': { // TEST MODE
  302. $this->barcode_array['num_rows'] = 5;
  303. $this->barcode_array['num_cols'] = 15;
  304. $this->barcode_array['bcode'] = array(
  305. array(1,1,1,0,1,1,1,0,1,1,1,0,1,1,1),
  306. array(0,1,0,0,1,0,0,0,1,0,0,0,0,1,0),
  307. array(0,1,0,0,1,1,0,0,1,1,1,0,0,1,0),
  308. array(0,1,0,0,1,0,0,0,0,0,1,0,0,1,0),
  309. array(0,1,0,0,1,1,1,0,1,1,1,0,0,1,0));
  310. $this->barcode_array['code'] = $code;
  311. break;
  312. }
  313. default: {
  314. $this->barcode_array = false;
  315. }
  316. }
  317. }
  318. } // end of class
  319. //============================================================+
  320. // END OF FILE
  321. //============================================================+