.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : PHP class to creates array representations for
// common 1D barcodes to be used with TCPDF.
//
//============================================================+
/**
* @file
* PHP class to creates array representations for common 1D barcodes to be used with TCPDF.
* @package com.tecnick.tcpdf
* @author Nicola Asuni
* @version 1.0.027
*/
/**
* @class TCPDFBarcode
* PHP class to creates array representations for common 1D barcodes to be used with TCPDF (http://www.tcpdf.org).
* @package com.tecnick.tcpdf
* @version 1.0.027
* @author Nicola Asuni
*/
class TCPDFBarcode {
/**
* Array representation of barcode.
* @protected
*/
protected $barcode_array;
/**
* This is the class constructor.
* Return an array representations for common 1D barcodes:
'."\n";
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round(($v['w'] * $w), 3);
$bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
if ($v['t']) {
$y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
// draw a vertical bar
$html .= '
'."\n";
}
$x += $bw;
}
$html .= '
'."\n";
return $html;
}
/**
* Send a PNG image representation of barcode (requires GD or Imagick library).
* @param int $w Width of a single bar element in pixels.
* @param int $h Height of a single bar element in pixels.
* @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
* @public
*/
public function getBarcodePNG($w=2, $h=30, $color=array(0,0,0)) {
$data = $this->getBarcodePngData($w, $h, $color);
// send headers
header('Content-Type: image/png');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
//header('Content-Length: '.strlen($data));
echo $data;
}
/**
* Return a PNG image representation of barcode (requires GD or Imagick library).
* @param int $w Width of a single bar element in pixels.
* @param int $h Height of a single bar element in pixels.
* @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
* @return string|Imagick|false image or false in case of error.
* @public
*/
public function getBarcodePngData($w=2, $h=30, $color=array(0,0,0)) {
// calculate image size
$width = ($this->barcode_array['maxw'] * $w);
$height = $h;
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new imagickpixel('rgb(255,255,255');
$fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
$png = new Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print bars
$x = 0;
foreach ($this->barcode_array['bcode'] as $k => $v) {
$bw = round(($v['w'] * $w), 3);
$bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
if ($v['t']) {
$y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
// draw a vertical bar
if ($imagick) {
$bar->rectangle($x, $y, ($x + $bw - 1), ($y + $bh - 1));
} else {
imagefilledrectangle($png, $x, $y, ($x + $bw - 1), ($y + $bh - 1), $fgcol);
}
}
$x += $bw;
}
if ($imagick) {
$png->drawimage($bar);
return $png;
} else {
ob_start();
imagepng($png);
$imagedata = ob_get_clean();
imagedestroy($png);
return $imagedata;
}
}
/**
* Set the barcode.
* @param string $code code to print
* @param string $type type of barcode: