config
2 years ago
fonts
2 years ago
include
4 months ago
LICENSE.TXT
1 year ago
README.md
7 months ago
VERSION
4 months ago
tcpdf.php
4 months ago
tcpdf_autoconfig.php
7 months ago
tcpdf_barcodes_1d.php
2 years ago
tcpdf_barcodes_2d.php
2 years ago
tcpdf_barcodes_2d.php
352 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | //============================================================+ |
| 5 | // File name : tcpdf_barcodes_2d.php |
| 6 | // Version : 1.0.015 |
| 7 | // Begin : 2009-04-07 |
| 8 | // Last Update : 2014-05-20 |
| 9 | // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com |
| 10 | // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html) |
| 11 | // ------------------------------------------------------------------- |
| 12 | // Copyright (C) 2009-2014 Nicola Asuni - Tecnick.com LTD |
| 13 | // |
| 14 | // This file is part of TCPDF software library. |
| 15 | // |
| 16 | // TCPDF is free software: you can redistribute it and/or modify it |
| 17 | // under the terms of the GNU Lesser General Public License as |
| 18 | // published by the Free Software Foundation, either version 3 of the |
| 19 | // License, or (at your option) any later version. |
| 20 | // |
| 21 | // TCPDF is distributed in the hope that it will be useful, but |
| 22 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 24 | // See the GNU Lesser General Public License for more details. |
| 25 | // |
| 26 | // You should have received a copy of the GNU Lesser General Public License |
| 27 | // along with TCPDF. If not, see <http://www.gnu.org/licenses/>. |
| 28 | // |
| 29 | // See LICENSE.TXT file for more information. |
| 30 | // ------------------------------------------------------------------- |
| 31 | // |
| 32 | // Description : PHP class to creates array representations for |
| 33 | // 2D barcodes to be used with TCPDF. |
| 34 | // |
| 35 | //============================================================+ |
| 36 | /** |
| 37 | * @file |
| 38 | * PHP class to creates array representations for 2D barcodes to be used with TCPDF. |
| 39 | * @package com.tecnick.tcpdf |
| 40 | * @author Nicola Asuni |
| 41 | * @version 1.0.015 |
| 42 | */ |
| 43 | /** |
| 44 | * @class TCPDF2DBarcode |
| 45 | * PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org). |
| 46 | * @package com.tecnick.tcpdf |
| 47 | * @version 1.0.015 |
| 48 | * @author Nicola Asuni |
| 49 | */ |
| 50 | class TCPDF2DBarcode |
| 51 | { |
| 52 | /** |
| 53 | * Array representation of barcode. |
| 54 | * @protected |
| 55 | */ |
| 56 | protected $barcode_array = array(); |
| 57 | /** |
| 58 | * This is the class constructor. |
| 59 | * Return an array representations for 2D barcodes:<ul> |
| 60 | * <li>$arrcode['code'] code to be printed on text label</li> |
| 61 | * <li>$arrcode['num_rows'] required number of rows</li> |
| 62 | * <li>$arrcode['num_cols'] required number of columns</li> |
| 63 | * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul> |
| 64 | * @param string $code code to print |
| 65 | * @param string $type 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 parameters 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> |
| 66 | */ |
| 67 | public function __construct($code, $type) |
| 68 | { |
| 69 | $this->setBarcode($code, $type); |
| 70 | } |
| 71 | /** |
| 72 | * Return an array representations of barcode. |
| 73 | * @return array |
| 74 | */ |
| 75 | public function getBarcodeArray() |
| 76 | { |
| 77 | return $this->barcode_array; |
| 78 | } |
| 79 | /** |
| 80 | * Send barcode as SVG image object to the standard output. |
| 81 | * @param int $w Width of a single rectangle element in user units. |
| 82 | * @param int $h Height of a single rectangle element in user units. |
| 83 | * @param string $color Foreground color (in SVG format) for bar elements (background is transparent). |
| 84 | * @public |
| 85 | */ |
| 86 | public function getBarcodeSVG($w = 3, $h = 3, $color = 'black') |
| 87 | { |
| 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'); |
| 92 | // HTTP/1.1 |
| 93 | \header('Pragma: public'); |
| 94 | \header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); |
| 95 | // Date in the past |
| 96 | \header('Last-Modified: ' . \gmdate('D, d M Y H:i:s') . ' GMT'); |
| 97 | \header('Content-Disposition: inline; filename="' . \md5($code) . '.svg";'); |
| 98 | //header('Content-Length: '.strlen($code)); |
| 99 | echo $code; |
| 100 | } |
| 101 | /** |
| 102 | * Return a SVG string representation of barcode. |
| 103 | * @param int $w Width of a single rectangle element in user units. |
| 104 | * @param int $h Height of a single rectangle element in user units. |
| 105 | * @param string $color Foreground color (in SVG format) for bar elements (background is transparent). |
| 106 | * @return string SVG code. |
| 107 | * @public |
| 108 | */ |
| 109 | public function getBarcodeSVGcode($w = 3, $h = 3, $color = 'black') |
| 110 | { |
| 111 | // replace table for special characters |
| 112 | $repstr = array("\x00" => '', '&' => '&', '<' => '<', '>' => '>'); |
| 113 | $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n"; |
| 114 | $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n"; |
| 115 | $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"; |
| 116 | $svg .= "\t" . '<desc>' . \strtr($this->barcode_array['code'], $repstr) . '</desc>' . "\n"; |
| 117 | $svg .= "\t" . '<g id="elements" fill="' . $color . '" stroke="none">' . "\n"; |
| 118 | // print barcode elements |
| 119 | $y = 0; |
| 120 | // for each row |
| 121 | for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { |
| 122 | $x = 0; |
| 123 | // for each column |
| 124 | for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { |
| 125 | if ($this->barcode_array['bcode'][$r][$c] == 1) { |
| 126 | // draw a single barcode cell |
| 127 | $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $w . '" height="' . $h . '" />' . "\n"; |
| 128 | } |
| 129 | $x += $w; |
| 130 | } |
| 131 | $y += $h; |
| 132 | } |
| 133 | $svg .= "\t" . '</g>' . "\n"; |
| 134 | $svg .= '</svg>' . "\n"; |
| 135 | return $svg; |
| 136 | } |
| 137 | /** |
| 138 | * Return an HTML representation of barcode. |
| 139 | * @param int $w Width of a single rectangle element in pixels. |
| 140 | * @param int $h Height of a single rectangle element in pixels. |
| 141 | * @param string $color Foreground color for bar elements (background is transparent). |
| 142 | * @return string HTML code. |
| 143 | * @public |
| 144 | */ |
| 145 | public function getBarcodeHTML($w = 10, $h = 10, $color = 'black') |
| 146 | { |
| 147 | $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"; |
| 148 | // print barcode elements |
| 149 | $y = 0; |
| 150 | // for each row |
| 151 | for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { |
| 152 | $x = 0; |
| 153 | // for each column |
| 154 | for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { |
| 155 | if ($this->barcode_array['bcode'][$r][$c] == 1) { |
| 156 | // draw a single barcode cell |
| 157 | $html .= '<div style="background-color:' . $color . ';width:' . $w . 'px;height:' . $h . 'px;position:absolute;left:' . $x . 'px;top:' . $y . 'px;"> </div>' . "\n"; |
| 158 | } |
| 159 | $x += $w; |
| 160 | } |
| 161 | $y += $h; |
| 162 | } |
| 163 | $html .= '</div>' . "\n"; |
| 164 | return $html; |
| 165 | } |
| 166 | /** |
| 167 | * Send a PNG image representation of barcode (requires GD or Imagick library). |
| 168 | * @param int $w Width of a single rectangle element in pixels. |
| 169 | * @param int $h Height of a single rectangle element in pixels. |
| 170 | * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). |
| 171 | * @public |
| 172 | */ |
| 173 | public function getBarcodePNG($w = 3, $h = 3, $color = array(0, 0, 0)) |
| 174 | { |
| 175 | $data = $this->getBarcodePngData($w, $h, $color); |
| 176 | // send headers |
| 177 | \header('Content-Type: image/png'); |
| 178 | \header('Cache-Control: public, must-revalidate, max-age=0'); |
| 179 | // HTTP/1.1 |
| 180 | \header('Pragma: public'); |
| 181 | \header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); |
| 182 | // Date in the past |
| 183 | \header('Last-Modified: ' . \gmdate('D, d M Y H:i:s') . ' GMT'); |
| 184 | //header('Content-Length: '.strlen($data)); |
| 185 | echo $data; |
| 186 | } |
| 187 | /** |
| 188 | * Return a PNG image representation of barcode (requires GD or Imagick library). |
| 189 | * @param int $w Width of a single rectangle element in pixels. |
| 190 | * @param int $h Height of a single rectangle element in pixels. |
| 191 | * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). |
| 192 | * @return string|Imagick|false image or false in case of error. |
| 193 | * @public |
| 194 | */ |
| 195 | public function getBarcodePngData($w = 3, $h = 3, $color = array(0, 0, 0)) |
| 196 | { |
| 197 | // calculate image size |
| 198 | $width = $this->barcode_array['num_cols'] * $w; |
| 199 | $height = $this->barcode_array['num_rows'] * $h; |
| 200 | if (\function_exists('imagecreate')) { |
| 201 | // GD library |
| 202 | $imagick = \false; |
| 203 | $png = \imagecreate($width, $height); |
| 204 | $bgcol = \imagecolorallocate($png, 255, 255, 255); |
| 205 | \imagecolortransparent($png, $bgcol); |
| 206 | $fgcol = \imagecolorallocate($png, $color[0], $color[1], $color[2]); |
| 207 | } elseif (\extension_loaded('imagick')) { |
| 208 | $imagick = \true; |
| 209 | $bgcol = new \imagickpixel('rgb(255,255,255'); |
| 210 | $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); |
| 211 | $png = new \Imagick(); |
| 212 | $png->newImage($width, $height, 'none', 'png'); |
| 213 | $bar = new \imagickdraw(); |
| 214 | $bar->setfillcolor($fgcol); |
| 215 | } else { |
| 216 | return \false; |
| 217 | } |
| 218 | // print barcode elements |
| 219 | $y = 0; |
| 220 | // for each row |
| 221 | for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { |
| 222 | $x = 0; |
| 223 | // for each column |
| 224 | for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { |
| 225 | if ($this->barcode_array['bcode'][$r][$c] == 1) { |
| 226 | // draw a single barcode cell |
| 227 | if ($imagick) { |
| 228 | $bar->rectangle($x, $y, $x + $w - 1, $y + $h - 1); |
| 229 | } else { |
| 230 | \imagefilledrectangle($png, $x, $y, $x + $w - 1, $y + $h - 1, $fgcol); |
| 231 | } |
| 232 | } |
| 233 | $x += $w; |
| 234 | } |
| 235 | $y += $h; |
| 236 | } |
| 237 | if ($imagick) { |
| 238 | $png->drawimage($bar); |
| 239 | return $png; |
| 240 | } else { |
| 241 | \ob_start(); |
| 242 | \imagepng($png); |
| 243 | $imagedata = \ob_get_clean(); |
| 244 | \imagedestroy($png); |
| 245 | return $imagedata; |
| 246 | } |
| 247 | } |
| 248 | /** |
| 249 | * Set the barcode. |
| 250 | * @param string $code code to print |
| 251 | * @param string $type 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 parameters 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> |
| 252 | * @return void |
| 253 | */ |
| 254 | public function setBarcode($code, $type) |
| 255 | { |
| 256 | $mode = \explode(',', $type); |
| 257 | $qrtype = \strtoupper($mode[0]); |
| 258 | switch ($qrtype) { |
| 259 | case 'DATAMATRIX': |
| 260 | // DATAMATRIX (ISO/IEC 16022) |
| 261 | require_once \dirname(__FILE__) . '/include/barcodes/datamatrix.php'; |
| 262 | $qrcode = new \Datamatrix($code); |
| 263 | $this->barcode_array = $qrcode->getBarcodeArray(); |
| 264 | $this->barcode_array['code'] = $code; |
| 265 | break; |
| 266 | case 'PDF417': |
| 267 | // PDF417 (ISO/IEC 15438:2006) |
| 268 | require_once \dirname(__FILE__) . '/include/barcodes/pdf417.php'; |
| 269 | if (!isset($mode[1]) or $mode[1] === '') { |
| 270 | $aspectratio = 2; |
| 271 | // default aspect ratio (width / height) |
| 272 | } else { |
| 273 | $aspectratio = \floatval($mode[1]); |
| 274 | } |
| 275 | if (!isset($mode[2]) or $mode[2] === '') { |
| 276 | $ecl = -1; |
| 277 | // default error correction level (auto) |
| 278 | } else { |
| 279 | $ecl = \intval($mode[2]); |
| 280 | } |
| 281 | // set macro block |
| 282 | $macro = array(); |
| 283 | if (isset($mode[3]) and $mode[3] !== '' and isset($mode[4]) and $mode[4] !== '' and isset($mode[5]) and $mode[5] !== '') { |
| 284 | $macro['segment_total'] = \intval($mode[3]); |
| 285 | $macro['segment_index'] = \intval($mode[4]); |
| 286 | $macro['file_id'] = \strtr($mode[5], "\xff", ','); |
| 287 | for ($i = 0; $i < 7; ++$i) { |
| 288 | $o = $i + 6; |
| 289 | if (isset($mode[$o]) and $mode[$o] !== '') { |
| 290 | // add option |
| 291 | $macro['option_' . $i] = \strtr($mode[$o], "\xff", ','); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | $qrcode = new \PDF417($code, $ecl, $aspectratio, $macro); |
| 296 | $this->barcode_array = $qrcode->getBarcodeArray(); |
| 297 | $this->barcode_array['code'] = $code; |
| 298 | break; |
| 299 | case 'QRCODE': |
| 300 | // QR-CODE |
| 301 | require_once \dirname(__FILE__) . '/include/barcodes/qrcode.php'; |
| 302 | if (!isset($mode[1]) or !\in_array($mode[1], array('L', 'M', 'Q', 'H'))) { |
| 303 | $mode[1] = 'L'; |
| 304 | // Ddefault: Low error correction |
| 305 | } |
| 306 | $qrcode = new \QRcode($code, \strtoupper($mode[1])); |
| 307 | $this->barcode_array = $qrcode->getBarcodeArray(); |
| 308 | $this->barcode_array['code'] = $code; |
| 309 | break; |
| 310 | case 'RAW': |
| 311 | case 'RAW2': |
| 312 | // RAW MODE |
| 313 | // remove spaces |
| 314 | $code = \preg_replace('/[\\s]*/si', '', $code); |
| 315 | if (\strlen($code) < 3) { |
| 316 | break; |
| 317 | } |
| 318 | if ($qrtype == 'RAW') { |
| 319 | // comma-separated rows |
| 320 | $rows = \explode(',', $code); |
| 321 | } else { |
| 322 | // RAW2 |
| 323 | // rows enclosed in square parentheses |
| 324 | $code = \substr($code, 1, -1); |
| 325 | $rows = \explode('][', $code); |
| 326 | } |
| 327 | $this->barcode_array['num_rows'] = \count($rows); |
| 328 | $this->barcode_array['num_cols'] = \strlen($rows[0]); |
| 329 | $this->barcode_array['bcode'] = array(); |
| 330 | foreach ($rows as $r) { |
| 331 | $this->barcode_array['bcode'][] = \str_split($r, 1); |
| 332 | } |
| 333 | $this->barcode_array['code'] = $code; |
| 334 | break; |
| 335 | case 'TEST': |
| 336 | // TEST MODE |
| 337 | $this->barcode_array['num_rows'] = 5; |
| 338 | $this->barcode_array['num_cols'] = 15; |
| 339 | $this->barcode_array['bcode'] = array(array(1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1), array(0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0), array(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0), array(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), array(0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0)); |
| 340 | $this->barcode_array['code'] = $code; |
| 341 | break; |
| 342 | default: |
| 343 | $this->barcode_array = array(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | // end of class |
| 348 | //============================================================+ |
| 349 | // END OF FILE |
| 350 | //============================================================+ |
| 351 | } |
| 352 |