Skip to content

Commit 70a5f04

Browse files
committed
tcpdf_5_9_143
1 parent a770c9c commit 70a5f04

File tree

333 files changed

+9493
-108729
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

333 files changed

+9493
-108729
lines changed

2dbarcodes.php

+175-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
//============================================================+
33
// File name : 2dbarcodes.php
4-
// Version : 1.0.007
4+
// Version : 1.0.012
55
// Begin : 2009-04-07
6-
// Last Update : 2010-12-16
7-
// Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - [email protected]
6+
// Last Update : 2011-09-15
7+
// Author : Nicola Asuni - Tecnick.com LTD - Manor Coach House, Church Hill, Aldershot, Hants, GU12 4RQ, UK - www.tecnick.com - [email protected]
88
// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
99
// -------------------------------------------------------------------
10-
// Copyright (C) 2009-2010 Nicola Asuni - Tecnick.com S.r.l.
10+
// Copyright (C) 2009-2012 Nicola Asuni - Tecnick.com LTD
1111
//
1212
// This file is part of TCPDF software library.
1313
//
@@ -37,14 +37,14 @@
3737
* PHP class to creates array representations for 2D barcodes to be used with TCPDF.
3838
* @package com.tecnick.tcpdf
3939
* @author Nicola Asuni
40-
* @version 1.0.007
40+
* @version 1.0.012
4141
*/
4242

4343
/**
4444
* @class TCPDF2DBarcode
4545
* PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org).
4646
* @package com.tecnick.tcpdf
47-
* @version 1.0.007
47+
* @version 1.0.012
4848
* @author Nicola Asuni
4949
*/
5050
class TCPDF2DBarcode {
@@ -63,7 +63,7 @@ class TCPDF2DBarcode {
6363
* <li>$arrcode['num_cols'] required number of columns</li>
6464
* <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
6565
* @param $code (string) code to print
66-
* @param $type (string) type of barcode: <ul><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>QRCODE : QR-CODE Low error correction</li><li>QRCODE,L : QR-CODE Low error correction</li><li>QRCODE,M : QR-CODE Medium error correction</li><li>QRCODE,Q : QR-CODE Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</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></ul>
66+
* @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>
6767
*/
6868
public function __construct($code, $type) {
6969
$this->setBarcode($code, $type);
@@ -77,23 +77,171 @@ public function getBarcodeArray() {
7777
return $this->barcode_array;
7878
}
7979

80+
/**
81+
* Send barcode as SVG image object to the standard output.
82+
* @param $w (int) Width of a single rectangle element in user units.
83+
* @param $h (int) Height of a single rectangle element 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=3, $h=3, $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+
/**
101+
* Return a SVG string representation of barcode.
102+
* @param $w (int) Width of a single rectangle element in user units.
103+
* @param $h (int) Height of a single rectangle element in user units.
104+
* @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
105+
* @return string SVG code.
106+
* @public
107+
*/
108+
public function getBarcodeSVGcode($w=3, $h=3, $color='black') {
109+
// replace table for special characters
110+
$repstr = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
111+
$svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
112+
$svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
113+
$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";
114+
$svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
115+
$svg .= "\t".'<g id="elements" fill="'.$color.'" stroke="none">'."\n";
116+
// print barcode elements
117+
$y = 0;
118+
// for each row
119+
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
120+
$x = 0;
121+
// for each column
122+
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
123+
if ($this->barcode_array['bcode'][$r][$c] == 1) {
124+
// draw a single barcode cell
125+
$svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$w.'" height="'.$h.'" />'."\n";
126+
}
127+
$x += $w;
128+
}
129+
$y += $h;
130+
}
131+
$svg .= "\t".'</g>'."\n";
132+
$svg .= '</svg>'."\n";
133+
return $svg;
134+
}
135+
136+
/**
137+
* Return an HTML representation of barcode.
138+
* @param $w (int) Width of a single rectangle element in pixels.
139+
* @param $h (int) Height of a single rectangle element in pixels.
140+
* @param $color (string) Foreground color for bar elements (background is transparent).
141+
* @return string HTML code.
142+
* @public
143+
*/
144+
public function getBarcodeHTML($w=10, $h=10, $color='black') {
145+
// replace table for special characters
146+
$html = '<div style="font-size:0;position:relative;">'."\n";
147+
// print barcode elements
148+
$y = 0;
149+
// for each row
150+
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
151+
$x = 0;
152+
// for each column
153+
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
154+
if ($this->barcode_array['bcode'][$r][$c] == 1) {
155+
// draw a single barcode cell
156+
$html .= '<div style="background-color:'.$color.';width:'.$w.'px;height:'.$h.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;">&nbsp;</div>'."\n";
157+
}
158+
$x += $w;
159+
}
160+
$y += $h;
161+
}
162+
$html .= '</div>'."\n";
163+
return $html;
164+
}
165+
166+
/**
167+
* Return a PNG image representation of barcode (requires GD or Imagick library).
168+
* @param $w (int) Width of a single rectangle element in pixels.
169+
* @param $h (int) Height of a single rectangle element in pixels.
170+
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
171+
* @return image or false in case of error.
172+
* @public
173+
*/
174+
public function getBarcodePNG($w=3, $h=3, $color=array(0,0,0)) {
175+
// calculate image size
176+
$width = ($this->barcode_array['num_cols'] * $w);
177+
$height = ($this->barcode_array['num_rows'] * $h);
178+
if (function_exists('imagecreate')) {
179+
// GD library
180+
$imagick = false;
181+
$png = imagecreate($width, $height);
182+
$bgcol = imagecolorallocate($png, 255, 255, 255);
183+
imagecolortransparent($png, $bgcol);
184+
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
185+
} elseif (extension_loaded('imagick')) {
186+
$imagick = true;
187+
$bgcol = new imagickpixel('rgb(255,255,255');
188+
$fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
189+
$png = new Imagick();
190+
$png->newImage($width, $height, 'none', 'png');
191+
$bar = new imagickdraw();
192+
$bar->setfillcolor($fgcol);
193+
} else {
194+
return false;
195+
}
196+
// print barcode elements
197+
$y = 0;
198+
// for each row
199+
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
200+
$x = 0;
201+
// for each column
202+
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
203+
if ($this->barcode_array['bcode'][$r][$c] == 1) {
204+
// draw a single barcode cell
205+
if ($imagick) {
206+
$bar->rectangle($x, $y, ($x + $w), ($y + $h));
207+
} else {
208+
imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
209+
}
210+
}
211+
$x += $w;
212+
}
213+
$y += $h;
214+
}
215+
// send headers
216+
header('Content-Type: image/png');
217+
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
218+
header('Pragma: public');
219+
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
220+
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
221+
if ($imagick) {
222+
$png->drawimage($bar);
223+
echo $png;
224+
} else {
225+
imagepng($png);
226+
imagedestroy($png);
227+
}
228+
}
229+
80230
/**
81231
* Set the barcode.
82232
* @param $code (string) code to print
83-
* @param $type (string) type of barcode: <ul><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>QRCODE : QR-CODE Low error correction</li><li>QRCODE,L : QR-CODE Low error correction</li><li>QRCODE,M : QR-CODE Medium error correction</li><li>QRCODE,Q : QR-CODE Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</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></ul>
233+
* @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>
84234
* @return array
85235
*/
86236
public function setBarcode($code, $type) {
87237
$mode = explode(',', $type);
88238
$qrtype = strtoupper($mode[0]);
89239
switch ($qrtype) {
90-
case 'QRCODE': { // QR-CODE
91-
require_once(dirname(__FILE__).'/qrcode.php');
92-
if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) {
93-
$mode[1] = 'L'; // Ddefault: Low error correction
94-
}
95-
$qrcode = new QRcode($code, strtoupper($mode[1]));
240+
case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
241+
require_once(dirname(__FILE__).'/datamatrix.php');
242+
$qrcode = new Datamatrix($code);
96243
$this->barcode_array = $qrcode->getBarcodeArray();
244+
$this->barcode_array['code'] = $code;
97245
break;
98246
}
99247
case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
@@ -124,6 +272,17 @@ public function setBarcode($code, $type) {
124272
}
125273
$qrcode = new PDF417($code, $ecl, $aspectratio, $macro);
126274
$this->barcode_array = $qrcode->getBarcodeArray();
275+
$this->barcode_array['code'] = $code;
276+
break;
277+
}
278+
case 'QRCODE': { // QR-CODE
279+
require_once(dirname(__FILE__).'/qrcode.php');
280+
if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) {
281+
$mode[1] = 'L'; // Ddefault: Low error correction
282+
}
283+
$qrcode = new QRcode($code, strtoupper($mode[1]));
284+
$this->barcode_array = $qrcode->getBarcodeArray();
285+
$this->barcode_array['code'] = $code;
127286
break;
128287
}
129288
case 'RAW':
@@ -147,6 +306,7 @@ public function setBarcode($code, $type) {
147306
foreach ($rows as $r) {
148307
$this->barcode_array['bcode'][] = str_split($r, 1);
149308
}
309+
$this->barcode_array['code'] = $code;
150310
break;
151311
}
152312
case 'TEST': { // TEST MODE
@@ -158,6 +318,7 @@ public function setBarcode($code, $type) {
158318
array(0,1,0,0,1,1,0,0,1,1,1,0,0,1,0),
159319
array(0,1,0,0,1,0,0,0,0,0,1,0,0,1,0),
160320
array(0,1,0,0,1,1,1,0,1,1,1,0,0,1,0));
321+
$this->barcode_array['code'] = $code;
161322
break;
162323
}
163324
default: {

0 commit comments

Comments
 (0)