From d303c731f857390eb57155d90f62192c41a6f9bf Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 15 Sep 2011 21:16:33 +0200 Subject: [PATCH] 5.9.117 --- 2dbarcodes.php | 80 ++++++++++++++++++++++++++++++++++++++++++++------ CHANGELOG.TXT | 3 ++ README.TXT | 4 +-- barcodes.php | 71 ++++++++++++++++++++++++++++++++++++++++---- tcpdf.php | 10 +++---- 5 files changed, 147 insertions(+), 21 deletions(-) diff --git a/2dbarcodes.php b/2dbarcodes.php index 9d1ad93..3448ae7 100755 --- a/2dbarcodes.php +++ b/2dbarcodes.php @@ -1,9 +1,9 @@ getBarcodeSVGcode($w, $h, $color); header('Content-Type: application/svg+xml'); header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 @@ -114,11 +114,10 @@ class TCPDF2DBarcode { $svg .= "\t".''.strtr($this->barcode_array['code'], $repstr).''."\n"; $svg .= "\t".''."\n"; // print barcode elements - $xstart = 0; $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { - $x = $xstart; + $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { @@ -146,11 +145,10 @@ class TCPDF2DBarcode { // replace table for special characters $html = '
'."\n"; // print barcode elements - $xstart = 0; $y = 0; // for each row for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { - $x = $xstart; + $x = 0; // for each column for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { if ($this->barcode_array['bcode'][$r][$c] == 1) { @@ -165,6 +163,70 @@ class TCPDF2DBarcode { return $html; } + /** + * Return a PNG image representation of barcode (requires GD or Imagick library). + * @param $w (int) Width of a single rectangle element in pixels. + * @param $h (int) Height of a single rectangle element in pixels. + * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent). + * @return image or false in case of error. + * @public + */ + public function getBarcodePNG($w=3, $h=3, $color=array(0,0,0)) { + // calculate image size + $width = ($this->barcode_array['num_cols'] * $w); + $height = ($this->barcode_array['num_rows'] * $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 barcode elements + $y = 0; + // for each row + for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) { + $x = 0; + // for each column + for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) { + if ($this->barcode_array['bcode'][$r][$c] == 1) { + // draw a single barcode cell + if ($imagick) { + $bar->rectangle($x, $y, ($x + $w), ($y + $h)); + } else { + imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol); + } + } + $x += $w; + } + $y += $h; + } + // 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'); + if ($imagick) { + $png->drawimage($bar); + echo $png; + } else { + imagepng($png); + imagedestroy($png); + } + } + /** * Set the barcode. * @param $code (string) code to print diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 0d3d7f9..c26b5c7 100755 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -1,3 +1,6 @@ +5.9.117 (2011-09-17) + - TCPDFBarcode and TCPDF2DBarcode were extended to include a method for exporting barcodes as PNG images. + 5.9.116 (2011-09-14) - Datamatrix class was improved and documentation was fixed. diff --git a/README.TXT b/README.TXT index bde1211..7ee7011 100755 --- a/README.TXT +++ b/README.TXT @@ -8,8 +8,8 @@ http://sourceforge.net/donate/index.php?group_id=128076 ------------------------------------------------------------ Name: TCPDF -Version: 5.9.116 -Release date: 2011-09-14 +Version: 5.9.117 +Release date: 2011-09-15 Author: Nicola Asuni Copyright (c) 2002-2011: diff --git a/barcodes.php b/barcodes.php index 1195879..e444ca0 100755 --- a/barcodes.php +++ b/barcodes.php @@ -1,9 +1,9 @@ * @package com.tecnick.tcpdf - * @version 1.0.020 + * @version 1.0.021 * @author Nicola Asuni */ class TCPDFBarcode { @@ -91,7 +91,7 @@ class TCPDFBarcode { * @public */ public function getBarcodeSVG($w=2, $h=30, $color='black') { - // send XML headers + // send headers $code = $this->getBarcodeSVGcode($w, $h, $color); header('Content-Type: application/svg+xml'); header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 @@ -163,6 +163,67 @@ class TCPDFBarcode { return $html; } + /** + * Return a PNG image representation of barcode (requires GD or Imagick library). + * @param $w (int) Width of a single bar element in pixels. + * @param $h (int) Height of a single bar element in pixels. + * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent). + * @return image or false in case of error. + * @public + */ + public function getBarcodePNG($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), ($y + $bh)); + } else { + imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol); + } + } + $x += $bw; + } + // 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'); + if ($imagick) { + $png->drawimage($bar); + echo $png; + } else { + imagepng($png); + imagedestroy($png); + } + } + /** * Set the barcode. * @param $code (string) code to print diff --git a/tcpdf.php b/tcpdf.php index be2aeaf..26fa358 100755 --- a/tcpdf.php +++ b/tcpdf.php @@ -1,9 +1,9 @@ * @package com.tecnick.tcpdf * @author Nicola Asuni - * @version 5.9.116 + * @version 5.9.117 */ // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file. @@ -148,7 +148,7 @@ require_once(dirname(__FILE__).'/config/tcpdf_config.php'); * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.
* @package com.tecnick.tcpdf * @brief PHP class for generating PDF documents without requiring external extensions. - * @version 5.9.116 + * @version 5.9.117 * @author Nicola Asuni - info@tecnick.com */ class TCPDF { @@ -159,7 +159,7 @@ class TCPDF { * Current TCPDF version. * @private */ - private $tcpdf_version = '5.9.116'; + private $tcpdf_version = '5.9.117'; // Protected properties