cvs: pear /Image_Barcode ChangeLog Readme.txt barcode_img.php package.xml test-image-barcode.php /Image_Barcode/Barcode Code39.php ean13.php
int25.php upca.php

From: Date: Wed, 25 May 2005 15:31:10 +0000
Subject: cvs: pear /Image_Barcode ChangeLog Readme.txt barcode_img.php package.xml test-image-barcode.php /Image_Barcode/Barcode Code39.php ean13.php
int25.php upca.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32119@lists.php.net to get a copy of this message
msmarcal Wed May 25 11:31:10 2005 EDT Added files: /pear/Image_Barcode ChangeLog Readme.txt barcode_img.php package.xml test-image-barcode.php /pear/Image_Barcode/Barcode Code39.php ean13.php int25.php upca.php Log: Initial import into CVS

http://cvs.php.net/co.php/pear/Image_Barcode/Readme.txt?r=1.1&p=1 Index: pear/Image_Barcode/Readme.txt +++ pear/Image_Barcode/Readme.txt = Image_Barcode - a package to render barcodes = ------------------------------------------------ With PEAR::Image_Barcode class you can create a barcode representation of a given string. This class uses GD function because this the generated graphic can be any of GD supported supported image types. = Installation = ---------------- You can install Image_Barcode issuing the following command (as root): # pear install Image_Barcode If you don't have the 'pear' command, please consult PEAR::The PHP Extension and Application Repository homepage at http://pear.php.net = Getting Started = ------------------- Just load the class in your script: require_once('Image/Barcode.php'); Call the Image_Barcode::draw() as the follow: Image_Barcode::draw('1234', 'int25', 'png'); Where: = '1234' : string you want to draw as barcode; = 'int25': barcode type (check the avaible types at 'Barcode' subdir); = 'png' : generated graphic type. = Current State = ----------------- You can get the latest code at the PEAR site: http://pear.php.net/package-info.php?package=Image_Barcode = Contributing = ---------------- Help from people who want code new barcode module types are very welcome. Just send your module directly to msmarcal@php.net = Credits = ----------- Core class written by Marcelo Subtil Marcal <msmarcal@php.net> Interleaved 2 of 5 barcode module type written by Marcelo Subtil Marcal <msmarcal@php.net> EAN13 barcode module type written by Didier FOURNOUT <didier.fournout@nyc.fr> Code39 barcode module type written by Ryan Briones <ryanbriones@webxdesign.org> UPC-A barcode module type written by Jeffrey K. Brown <jkb@darkfantastic.net> = Thanks to = ------------- Mark A.R. <mark@mark.org.il> = Author = ---------- Written by Marcelo Subtil Marcal <msmarcal@php.net> = Reporting Bugs = ------------------ Report bugs to Marcelo Subtil Marcal <msmarcal@php.net> http://cvs.php.net/co.php/pear/Image_Barcode/barcode_img.php?r=1.1&p=1 Index: pear/Image_Barcode/barcode_img.php +++ pear/Image_Barcode/barcode_img.php <?php require_once("Image/Barcode.php"); $num = '15101967'; $type = 'int25'; $imgtype = 'png'; $num = isset($_REQUEST) && is_array($_REQUEST) && isset($_REQUEST['num']) ? $_REQUEST['num'] : $num; $type = isset($_REQUEST) && is_array($_REQUEST) && isset($_REQUEST['type']) ? $_REQUEST['type'] : $type; $imgtype = isset($_REQUEST) && is_array($_REQUEST) && isset($_REQUEST['imgtype']) ? $_REQUEST['imgtype'] : $imgtype; Image_Barcode::draw($num, $type, $imgtype); ?> http://cvs.php.net/co.php/pear/Image_Barcode/package.xml?r=1.1&p=1 Index: pear/Image_Barcode/package.xml +++ pear/Image_Barcode/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0"> <name>Image_Barcode</name> <summary>Barcode generation</summary> <description>With PEAR::Image_Barcode class you can create a barcode representation of a given string. This class uses GD function because of this the generated graphic can be any of GD supported supported image types.</description> <maintainers> <maintainer> <user>msmarcal</user> <name>Marcelo Subtil Marcal</name> <email>msmarcal@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>1.0.2</version> <date>2005-05-24</date> <license>PHP License</license> <state>stable</state> <notes> - Added UPC-A barcode rendering (thanks to Jeffrey K. Brown) </notes> <filelist> <file role="php" baseinstalldir="Image" name="Barcode.php"/> <file role="php" baseinstalldir="Image" name="Barcode/int25.php"/> <file role="php" baseinstalldir="Image" name="Barcode/ean13.php"/> <file role="php" baseinstalldir="Image" name="Barcode/Code39.php"/> <file role="php" baseinstalldir="Image" name="Barcode/upca.php" /> <file role="test" baseinstalldir="Image" name="test-image-barcode.php"/> <file role="test" baseinstalldir="Image" name="barcode_img.php"/> <file role="doc" baseinstalldir="Image" name="ChangeLog"/> <file role="doc" baseinstalldir="Image" name="Readme.txt"/> </filelist> </release> <deps> <dep type="pkg" rel="has" optional="yes">PHP_Compat</dep> <dep type="ext" rel="has">gd</dep> </deps> </package> http://cvs.php.net/co.php/pear/Image_Barcode/test-image-barcode.php?r=1.1&p=1 Index: pear/Image_Barcode/test-image-barcode.php +++ pear/Image_Barcode/test-image-barcode.php <html> <head> <title>Image_Barcode Class Test</title> <body background="#FFFFFF"> <h1>Image_Barcode Class test</h1> <?php $num = "019671015005"; $num = isset($_REQUEST) && is_array($_REQUEST) && isset($_REQUEST['num']) ? $_REQUEST['num'] : $num; echo "Test number: <b>$num</b>\n<p>\n"; ?> Interleave 2 of 5 (png):<br> <img src="barcode_img.php?num=<?php echo($num)?>&type=int25&imgtype=png" alt="PNG: <?php echo($num)?>" title="PNG: <?php echo($num)?>"> <p> Ean13 (png):<br> <img src="barcode_img.php?num=<?php echo($num)?>&type=ean13&imgtype=png" alt="PNG: <?php echo($num)?>" title="PNG: <?php echo($num)?>"> <p> Code39 (png):<br> <img src="barcode_img.php?num=<?php echo($num)?>&type=Code39&imgtype=png" alt="PNG: <?php echo($num)?>" title="PNG: <?php echo($num)?>"> <p> UPC-A (png):<br> <img src="barcode_img.php?num=<?php echo($num)?>&type=upca&imgtype=png" alt="PNG: <?php echo($num)?>" title="PNG: <?php echo($num)?>"> </body> </html> http://cvs.php.net/co.php/pear/Image_Barcode/Barcode/Code39.php?r=1.1&p=1 Index: pear/Image_Barcode/Barcode/Code39.php +++ pear/Image_Barcode/Barcode/Code39.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Ryan Briones <ryanbriones@webxdesign.org> | // | | // +----------------------------------------------------------------------+ // // Last Modified: 2003/12/09 - Ryan Briones /** * * Image_Barcode_Code39 creates Code 3 of 9 ( Code39 ) barcode images. It's * implementation borrows heavily for the perl module GD::Barcode::Code39 * * @author Ryan Briones <ryanbriones@webxdesign.org> * @todo add a function to print images directly to browser not using the draw() function * */ require_once "PEAR.php"; require_once "Image/Barcode.php"; /* * * the function str_split() has been including, not part of the class for * it's ability to emulate Perl's split( //, $text ). This function was * stolen from the PHP function documentation comments on PHP's str_split() * which is to be included in PHP5. * * @param string $str * @param int number of characters you wish to split on * @return array|false Returns an array or false when $num is less than 1 */ function str_split( $str, $num = '1' ) { if($num < 1) return FALSE; $arr = array(); for ($j = 0; $j < strlen($str); $j= $j+$num) { $arr[] = substr($str,$j,$num); } return $arr; } class Image_Barcode_Code39 extends Image_Barcode { /** * Barcode type * @var string */ var $_type = 'Code39'; /** * Barcode height * * @var integer */ var $_barcodeheight = 50; /** * Bar thin width * * @var integer */ var $_barthinwidth = 1; /** * Bar thick width * * @var integer */ var $_barthickwidth = 3; /** * Coding map * @var array */ var $_coding_map = array( '0' => '000110100', '1' => '100100001', '2' => '001100001', '3' => '101100000', '4' => '000110001', '5' => '100110000', '6' => '001110000', '7' => '000100101', '8' => '100100100', '9' => '001100100', 'A' => '100001001', 'B' => '001001001', 'C' => '101001000', 'D' => '000011001', 'E' => '100011000', 'F' => '001011000', 'G' => '000001101', 'H' => '100001100', 'I' => '001001100', 'J' => '000011100', 'K' => '100000011', 'L' => '001000011', 'M' => '101000010', 'N' => '000010011', 'O' => '100010010', 'P' => '001010010', 'Q' => '000000111', 'R' => '100000110', 'S' => '001000110', 'T' => '000010110', 'U' => '110000001', 'V' => '011000001', 'W' => '111000000', 'X' => '010010001', 'Y' => '110010000', 'Z' => '011010000', '-' => '010000101', '*' => '010010100', '+' => '010001010', '$' => '010101000', '%' => '000101010', '/' => '010100010', '.' => '110000100', ' ' => '011000100' ); /* * * constructor * * @param string $text * @param int $wThin Width of the thin lines on the barcode * @param int $wThick Width of the thick lines on the barcode * */ function Image_Barcode_Code39( $text = '', $wThin = 0, $wThick = 0 ) { // Check $text for invalid characters if ( $this->checkInvalid( $text ) ) { return false; } $this->text = $text; if ( $wThin > 0 ) $this->_barthinwidth = $wThin; if ( $wThick > 0 ) $this->_barthickwidth = $wThick; return true; } /* * * make an image resource using the GD image library * * @param bool $noText Set to true if you'd like your barcode to be sans text * @param int $bHeight height of the barcode image including text * @return resource The Barcode Image (TM) * */ function plot( $noText = false, $bHeight = 0 ) { // add start and stop * characters $final_text = "*" . $this->text . "*"; if ( $bHeight > 0 ) { $this->_barcodeheight = $bHeight; } $barcode = ''; foreach ( str_split( $final_text ) as $character ) { $barcode .= $this->_dumpCode( $this->_coding_map[$character] . '0' ); } $barcode_len = strlen( $barcode ); // Create GD image object $img = imagecreate( $barcode_len, $this->_barcodeheight ); // Allocate black and white colors to the image $black = imagecolorallocate( $img, 0, 0, 0 ); $white = imagecolorallocate( $img, 255, 255, 255 ); $font_height = ( $noText ? 0 : imagefontheight( "gdFontSmall" ) ); $font_width = imagefontwidth( "gdFontSmall" ); // fill background with white color imagefill( $img, 0, 0, $white ); // Initialize X position $xpos = 0; // draw barcode bars to image if ( $noText ) { foreach ( str_split( $barcode ) as $character_code ) { if ( $character_code == 0 ) { imageline( $img, $xpos, 0, $xpos, $this->_barcodeheight, $white ); } else { imageline( $img, $xpos, 0, $xpos, $this->_barcodeheight, $black ); } $xpos++; } } else { foreach ( str_split( $barcode ) as $character_code ) { if ( $character_code == 0 ) { imageline( $img, $xpos, 0, $xpos, $this->_barcodeheight - $font_height - 1, $white ); } else { imageline( $img, $xpos, 0, $xpos, $this->_barcodeheight - $font_height - 1, $black ); } $xpos++; } // draw text under barcode imagestring( $img, "gdFontSmall", ( $barcode_len - $font_width * strlen( $this->text ) )/2, $this->_barcodeheight - $font_height, $this->text, $black ); } return $img; } /* * * send image to the browser; for Image_Barcode compaitbility * * @param string $text * @param string $imgtype Image type; accepts jpg, png, and gif, but gif only works if you've payed for licensing * @param bool $noText Set to true if you'd like your barcode to be sans text * @param int $bHeight height of the barcode image including text * @return bool true or die * */ function draw( $text, $imgtype = 'png', $noText = false, $bHeight = 0 ) { // Check $text for invalid characters if ( $this->checkInvalid( $text ) ) { return false; } $this->text = $text; $img = $this->plot( $noText, $bHeight ); // Send image to browser switch($imgtype) { case 'gif': header("Content-type: image/gif"); imagegif($img); imagedestroy($img); break; case 'jpg': header("Content-type: image/jpg"); imagejpeg($img); imagedestroy($img); break; default: header("Content-type: image/png"); imagepng($img); imagedestroy($img); break; } return true; } /* * * _dumpCode is a PHP implementation of dumpCode from the Perl module * GD::Barcode::Code39. I royally screwed up when trying to do the thing * my own way the first time. This way works. * * @param string $code Code39 barcode code * @return string $result barcode line code * * @access private * */ function _dumpCode( $code ) { $result = ''; $color = 1; // 1: Black, 0: White // if $bit is 1, line is wide; if $bit is 0 line is thin foreach ( str_split( $code ) as $bit ) { $result .= ( ( $bit == 1 ) ? str_repeat( "$color", $this->_barthickwidth ) : str_repeat( "$color", $this->_barthinwidth ) ); $color = ( ( $color == 0 ) ? 1 : 0 ); } return $result; } /* * * check for invalid characters * * @param string $text * @return bool returns true when invalid characters have been found * */ function checkInvalid( $text ) { return preg_match( "/[^0-9A-Z\-*+\$%\/. ]/", $text ); } } ?> http://cvs.php.net/co.php/pear/Image_Barcode/Barcode/ean13.php?r=1.1&p=1 Index: pear/Image_Barcode/Barcode/ean13.php +++ pear/Image_Barcode/Barcode/ean13.php <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Didier FOURNOUT <didier.fournout@nyc.fr> | // +----------------------------------------------------------------------+ // // $Id: ean13.php,v 1.1 2005/05/25 15:31:09 msmarcal Exp $ // require_once "PEAR.php"; require_once "Image/Barcode.php"; /** * Class to create a EAN 13 barcode * * @author Didier FOURNOUT <didier.fournout@nyc.fr> */ class Image_Barcode_ean13 extends Image_Barcode { /** * Barcode type * @var string */ var $_type = 'ean13'; /** * Barcode height * * @var integer */ var $_barcodeheight = 50; /** * Font use to display text * * @var integer */ var $_font = 2; // gd internal small font /** * Bar width * * @var integer */ var $_barwidth = 1; /** * Number set * @var array */ var $_number_set = array( '0' => array( 'A' => array(0,0,0,1,1,0,1), 'B' => array(0,1,0,0,1,1,1), 'C' => array(1,1,1,0,0,1,0) ), '1' => array( 'A' => array(0,0,1,1,0,0,1), 'B' => array(0,1,1,0,0,1,1), 'C' => array(1,1,0,0,1,1,0) ), '2' => array( 'A' => array(0,0,1,0,0,1,1), 'B' => array(0,0,1,1,0,1,1), 'C' => array(1,1,0,1,1,0,0) ), '3' => array( 'A' => array(0,1,1,1,1,0,1), 'B' => array(0,1,0,0,0,0,1), 'C' => array(1,0,0,0,0,1,0) ), '4' => array( 'A' => array(0,1,0,0,0,1,1), 'B' => array(0,0,1,1,1,0,1), 'C' => array(1,0,1,1,1,0,0) ), '5' => array( 'A' => array(0,1,1,0,0,0,1), 'B' => array(0,1,1,1,0,0,1), 'C' => array(1,0,0,1,1,1,0) ), '6' => array( 'A' => array(0,1,0,1,1,1,1), 'B' => array(0,0,0,0,1,0,1), 'C' => array(1,0,1,0,0,0,0) ), '7' => array( 'A' => array(0,1,1,1,0,1,1), 'B' => array(0,0,1,0,0,0,1), 'C' => array(1,0,0,0,1,0,0) ), '8' => array( 'A' => array(0,1,1,0,1,1,1), 'B' => array(0,0,0,1,0,0,1), 'C' => array(1,0,0,1,0,0,0) ), '9' => array( 'A' => array(0,0,0,1,0,1,1), 'B' => array(0,0,1,0,1,1,1), 'C' => array(1,1,1,0,1,0,0) ) ); var $_number_set_left_coding = array( '0' => array('A','A','A','A','A','A'), '1' => array('A','A','B','A','B','B'), '2' => array('A','A','B','B','A','B'), '3' => array('A','A','B','B','B','A'), '4' => array('A','B','A','A','B','B'), '5' => array('A','B','B','A','A','B'), '6' => array('A','B','B','B','A','A'), '7' => array('A','B','A','B','A','B'), '8' => array('A','B','A','B','B','A'), '9' => array('A','B','B','A','B','A') ); function draw($text, $imgtype = 'png') { //TODO: Check if $text is number and len=13 // Calculate the barcode width $barcodewidth = (strlen($text)) * (7 * $this->_barwidth) + 3 // left + 5 // center + 3 // right + imagefontwidth($this->_font)+1 ; $barcodelongheight = (int) (imagefontheight($this->_font)/2)+$this->_barcodeheight; // Create the image $img = ImageCreate($barcodewidth, $barcodelongheight+ imagefontheight($this->_font)+1); // Alocate the black and white colors $black = ImageColorAllocate($img, 0, 0, 0); $white = ImageColorAllocate($img, 255, 255, 255); // Fill image with white color imagefill($img, 0, 0, $white); // get the first digit which is the key for creating the first 6 bars $key = substr($text,0,1); // Initiate x position $xpos = 0; // print first digit imagestring($img, $this->_font, $xpos, $this->_barcodeheight, $key, $black); $xpos= imagefontwidth($this->_font) + 1; // Draws the left guard pattern (bar-space-bar) // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // Draw left $text contents $set_array=$this->_number_set_left_coding[$key]; for ($idx = 1; $idx < 7; $idx ++) { $value=substr($text,$idx,1); imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black); foreach ($this->_number_set[$value][$set_array[$idx-1]] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black); } $xpos += $this->_barwidth; } } // Draws the center pattern (space-bar-space-bar-space) // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // Draw right $text contents for ($idx = 7; $idx < 13; $idx ++) { $value=substr($text,$idx,1); imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black); foreach ($this->_number_set[$value]['C'] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black); } $xpos += $this->_barwidth; } } // Draws the right guard pattern (bar-space-bar) // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // Send image to browser switch($imgtype) { case 'gif': header("Content-type: image/gif"); imagegif($img); imagedestroy($img); break; case 'jpg': header("Content-type: image/jpg"); imagejpeg($img); imagedestroy($img); break; default: header("Content-type: image/png"); imagepng($img); imagedestroy($img); break; } return; } // function create } // class http://cvs.php.net/co.php/pear/Image_Barcode/Barcode/int25.php?r=1.1&p=1 Index: pear/Image_Barcode/Barcode/int25.php +++ pear/Image_Barcode/Barcode/int25.php <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Marcelo Subtil Marcal <msmarcal@php.net> | // +----------------------------------------------------------------------+ // // $Id: int25.php,v 1.1 2005/05/25 15:31:09 msmarcal Exp $ // require_once "PEAR.php"; require_once "Image/Barcode.php"; /** * Class to create a Interleaved 2 of 5 barcode * * @author Marcelo Subtil Marcal <msmarcal@php.net> */ class Image_Barcode_int25 extends Image_Barcode { /** * Barcode type * @var string */ var $_type = 'int25'; /** * Barcode height * * @var integer */ var $_barcodeheight = 50; /** * Bar thin width * * @var integer */ var $_barthinwidth = 1; /** * Bar thick width * * @var integer */ var $_barthickwidth = 3; /** * Coding map * @var array */ var $_coding_map = array( '0' => '00110', '1' => '10001', '2' => '01001', '3' => '11000', '4' => '00101', '5' => '10100', '6' => '01100', '7' => '00011', '8' => '10010', '9' => '01010' ); function draw($text, $imgtype = 'png') { $text = trim($text); if (!preg_match("/[0-9]/",$text)) return; // if odd $text lenght adds a '0' at string beginning $text = strlen($text) % 2 ? '0' . $text : $text; // Calculate the barcode width $barcodewidth = (strlen($text)) * (3 * $this->_barthinwidth + 2 * $this->_barthickwidth) + (strlen($text)) * 2.5 + (7 * $this->_barthinwidth + $this->_barthickwidth) + 3; // Create the image $img = ImageCreate($barcodewidth, $this->_barcodeheight); // Alocate the black and white colors $black = ImageColorAllocate($img, 0, 0, 0); $white = ImageColorAllocate($img, 255, 255, 255); // Fill image with white color imagefill($img, 0, 0, $white); // Initiate x position $xpos = 0; // Draws the leader for ($i=0; $i < 2; $i++) { $elementwidth = $this->_barthinwidth; imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black); $xpos += $elementwidth; $xpos += $this->_barthinwidth; $xpos ++; } // Draw $text contents for ($idx = 0; $idx < strlen($text); $idx += 2) { // Draw 2 chars at a time $oddchar = substr($text, $idx, 1); // get odd char $evenchar = substr($text, $idx + 1, 1); // get even char // interleave for ($baridx = 0; $baridx < 5; $baridx++) { // Draws odd char corresponding bar (black) $elementwidth = (substr($this->_coding_map[$oddchar], $baridx, 1)) ? $this->_barthickwidth : $this->_barthinwidth; imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black); $xpos += $elementwidth; // Left enought space to draw even char (white) $elementwidth = (substr($this->_coding_map[$evenchar], $baridx, 1)) ? $this->_barthickwidth : $this->_barthinwidth; $xpos += $elementwidth; $xpos ++; } } // Draws the trailer $elementwidth = $this->_barthickwidth; imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black); $xpos += $elementwidth; $xpos += $this->_barthinwidth; $xpos ++; $elementwidth = $this->_barthinwidth; imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black); // Send image to browser switch($imgtype) { case 'gif': header("Content-type: image/gif"); imagegif($img); imagedestroy($img); break; case 'jpg': header("Content-type: image/jpg"); imagejpeg($img); imagedestroy($img); break; default: header("Content-type: image/png"); imagepng($img); imagedestroy($img); break; } return; } // function create } // class http://cvs.php.net/co.php/pear/Image_Barcode/Barcode/upca.php?r=1.1&p=1 Index: pear/Image_Barcode/Barcode/upca.php +++ pear/Image_Barcode/Barcode/upca.php <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Didier FOURNOUT <didier.fournout@nyc.fr> | // +----------------------------------------------------------------------+ // // $Id: upca.php,v 1.1 2005/05/25 15:31:09 msmarcal Exp $ // // +----------------------------------------------------------+ // Jeffrey K. Brown 5/22/2005 // Slightly Modified ean13.php to get upca.php // I needed a way to print UPC-A bar codes on a // PHP page. The Image_Barcode class seemed like // the best way to do it, so I modified ean13 to // print in the UPC-A format. Checked the bar code // tables against some documentation below (no errors) // and validated the changes with my trusty cue-cat. // // http://www.indiana.edu/~atmat/units/barcodes/bar_t4.htm // +----------------------------------------------------------+ require_once "PEAR.php"; require_once "Image/Barcode.php"; class Image_Barcode_upca extends Image_Barcode { /** * Barcode type * @var string */ var $_type = 'upca'; /** * Barcode height * * @var integer */ var $_barcodeheight = 50; /** * Font use to display text * * @var integer */ var $_font = 2; // gd internal small font /** * Bar width * * @var integer */ var $_barwidth = 1; /** * Number set * @var array */ var $_number_set = array( '0' => array( 'A' => array(0,0,0,1,1,0,1), 'B' => array(0,1,0,0,1,1,1), 'C' => array(1,1,1,0,0,1,0) ), '1' => array( 'A' => array(0,0,1,1,0,0,1), 'B' => array(0,1,1,0,0,1,1), 'C' => array(1,1,0,0,1,1,0) ), '2' => array( 'A' => array(0,0,1,0,0,1,1), 'B' => array(0,0,1,1,0,1,1), 'C' => array(1,1,0,1,1,0,0) ), '3' => array( 'A' => array(0,1,1,1,1,0,1), 'B' => array(0,1,0,0,0,0,1), 'C' => array(1,0,0,0,0,1,0) ), '4' => array( 'A' => array(0,1,0,0,0,1,1), 'B' => array(0,0,1,1,1,0,1), 'C' => array(1,0,1,1,1,0,0) ), '5' => array( 'A' => array(0,1,1,0,0,0,1), 'B' => array(0,1,1,1,0,0,1), 'C' => array(1,0,0,1,1,1,0) ), '6' => array( 'A' => array(0,1,0,1,1,1,1), 'B' => array(0,0,0,0,1,0,1), 'C' => array(1,0,1,0,0,0,0) ), '7' => array( 'A' => array(0,1,1,1,0,1,1), 'B' => array(0,0,1,0,0,0,1), 'C' => array(1,0,0,0,1,0,0) ), '8' => array( 'A' => array(0,1,1,0,1,1,1), 'B' => array(0,0,0,1,0,0,1), 'C' => array(1,0,0,1,0,0,0) ), '9' => array( 'A' => array(0,0,0,1,0,1,1), 'B' => array(0,0,1,0,1,1,1), 'C' => array(1,1,1,0,1,0,0) ) ); var $_number_set_left_coding = array( '0' => array('A','A','A','A','A','A'), '1' => array('A','A','B','A','B','B'), '2' => array('A','A','B','B','A','B'), '3' => array('A','A','B','B','B','A'), '4' => array('A','B','A','A','B','B'), '5' => array('A','B','B','A','A','B'), '6' => array('A','B','B','B','A','A'), '7' => array('A','B','A','B','A','B'), '8' => array('A','B','A','B','B','A'), '9' => array('A','B','B','A','B','A') ); //========================================================================================== function draw($text, $imgtype = 'png') { if ( (is_numeric($text)==false) || (strlen($text)!=12) ) { $barcodewidth= (12 * 7 * $this->_barwidth) + 3 + 5 + 3 + 2 * (imagefontwidth($this->_font)+1); $error = 1; } else { // Calculate the barcode width $barcodewidth = (strlen($text)) * (7 * $this->_barwidth) + 3 // left + 5 // center + 3 // right + imagefontwidth($this->_font)+1 + imagefontwidth($this->_font)+1 // check digit's padding ; } $barcodelongheight = (int) (imagefontheight($this->_font)/2)+$this->_barcodeheight; // Create the image $img = ImageCreate($barcodewidth, $barcodelongheight+ imagefontheight($this->_font)+1); // Alocate the black and white colors $black = ImageColorAllocate($img, 0, 0, 0); $white = ImageColorAllocate($img, 255, 255, 255); // Fill image with white color imagefill($img, 0, 0, $white); if ($error==1) { $imgerror = ImageCreate($barcodewidth, $barcodelongheight+imagefontheight($this->_font)+1); $red = ImageColorAllocate($imgerror, 255, 0, 0); $black = ImageColorAllocate($imgerror, 0,0,0); imagefill($imgerror,0,0,$red); imagestring($imgerror, $this->_font, $barcodewidth/2-(10/2* imagefontwidth($this->_font)), $this->_barcodeheight/2, "Code Error", $black); } // get the first digit which is the key for creating the first 6 bars $key = substr($text,0,1); // Initiate x position $xpos = 0; // print first digit imagestring($img, $this->_font, $xpos, $this->_barcodeheight, $key, $black); $xpos= imagefontwidth($this->_font) + 1; //=========================================================================================== // Draws the left guard pattern (bar-space-bar) // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; $set_array = $this->_number_set_left_coding[$key]; //========================================================================================= foreach ($this->_number_set['0'][$set_array[0]] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); } $xpos += $this->_barwidth; } //========================================================================================= // Draw left $text contents for ($idx = 1; $idx < 6; $idx ++) { $value=substr($text,$idx,1); imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black); //foreach ($this->_number_set[$value][$set_array[$idx-1]] as $bar) { foreach ($this->_number_set[$value][$set_array[$idx]] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black); } $xpos += $this->_barwidth; } } //========================================================================================= // Draws the center pattern (space-bar-space-bar-space) // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; //==================================================================================== // Draw right $text contents for ($idx = 6; $idx < 11; $idx ++) { $value=substr($text,$idx,1); imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black); foreach ($this->_number_set[$value]['C'] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black); } $xpos += $this->_barwidth; } } //====================================================================================== $value = substr($text,11,1); foreach ($this->_number_set[$value]['C'] as $bar) { if ($bar) { imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); } $xpos += $this->_barwidth; } // =================================================================================== // Draws the right guard pattern (bar-space-bar) // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // space $xpos += $this->_barwidth; // bar imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black); $xpos += $this->_barwidth; // ==================================================================================== // Print Check Digit imagestring($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black); // ====================================================================================== // Send image to browser switch($imgtype) { case 'gif': header("Content-type: image/gif"); if ($error==1) { imagegif($imgerror); imagedestroy($imgerror); } else { imagegif($img); imagedestroy($img); } break; case 'jpg': header("Content-type: image/jpg"); if ($error==1) { imageijpeg($imgerror); imagedestroy($imgerror); } else { imagejpeg($img); imagedestroy($img); } break; default: header("Content-type: image/png"); if ($error==1) { imagepng($imgerror); imagedestroy($imgerror); } else { imagepng($img); imagedestroy($img); } break; } return; } // function create } // class
« previous php.pear.cvs (#32119) next »