TCPDFBarcode::getBarcodePNG PHP Method

getBarcodePNG() public method

Return a PNG image representation of barcode (requires GD or Imagick library).
public getBarcodePNG ( $w = 2, $h = 30, $color = [0, 0, 0] ) : image
$w (int) Width of a single bar element in pixels.
$h (int) Height of a single bar element in pixels.
$color (array) RGB (0-255) foreground color for bar elements (background is transparent).
return image or false in case of error.
    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 - 1, $y + $bh - 1);
                } else {
                    imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $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);
        }
    }

Usage Example

コード例 #1
0
//
// TCPDF is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with TCPDF.  If not, see <http://www.gnu.org/licenses/>.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : Example for tcpdf_barcodes_2d.php class
//
//============================================================+
/**
 * @file
 * Example for tcpdf_barcodes_2d.php class
 * @package com.tecnick.tcpdf
 * @author Nicola Asuni
 * @version 1.0.000
 */
// include 1D barcode class (search for installation path)
require_once dirname(__FILE__) . '/tcpdf_barcodes_1d_include.php';
// set the barcode content and type
$barcodeobj = new TCPDFBarcode('http://www.tcpdf.org', 'C128');
// output the barcode as PNG image
$barcodeobj->getBarcodePNG(2, 30, array(0, 0, 0));
//============================================================+
// END OF FILE
//============================================================+
All Usage Examples Of TCPDFBarcode::getBarcodePNG