home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Image / Barcode / upca.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  10.5 KB  |  324 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode_upca class
  6.  *
  7.  * Renders UPC-A barcodes
  8.  *
  9.  * PHP versions 4
  10.  *
  11.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  12.  * that is available through the world-wide-web at the following URI:
  13.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  14.  * the PHP License and are unable to obtain it through the web, please
  15.  * send a note to license@php.net so we can mail you a copy immediately.
  16.  *
  17.  * @category   Image
  18.  * @package    Image_Barcode
  19.  * @author     Jeffrey K. Brown <jkb@darkfantastic.net>
  20.  * @author     Didier Fournout <didier.fournout@nyc.fr>
  21.  * @copyright  2005 The PHP Group
  22.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  23.  * @version    CVS: $Id: upca.php,v 1.5 2006/12/13 19:33:03 cweiske Exp $
  24.  * @link       http://pear.php.net/package/Image_Barcode
  25.  */
  26.  
  27. require_once 'Image/Barcode.php';
  28.  
  29. /**
  30.  * Image_Barcode_upca class
  31.  *
  32.  * Package which provides a method to create UPC-A barcode using GD library.
  33.  *
  34.  * Slightly Modified ean13.php to get upca.php I needed a way to print
  35.  * UPC-A bar codes on a PHP page.  The Image_Barcode class seemed like
  36.  * the best way to do it, so I modified ean13 to print in the UPC-A format.
  37.  * Checked the bar code tables against some documentation below (no errors)
  38.  * and validated the changes with my trusty cue-cat.
  39.  * http://www.indiana.edu/~atmat/units/barcodes/bar_t4.htm
  40.  *
  41.  * @category   Image
  42.  * @package    Image_Barcode
  43.  * @author     Jeffrey K. Brown <jkb@darkfantastic.net>
  44.  * @author     Didier Fournout <didier.fournout@nyc.fr>
  45.  * @copyright  2005 The PHP Group
  46.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  47.  * @version    Release: @package_version@
  48.  * @link       http://pear.php.net/package/Image_Barcode
  49.  */
  50. class Image_Barcode_upca extends Image_Barcode
  51. {
  52.     /**
  53.      * Barcode type
  54.      * @var string
  55.      */
  56.     var $_type = 'upca';
  57.  
  58.     /**
  59.      * Barcode height
  60.      *
  61.      * @var integer
  62.      */
  63.     var $_barcodeheight = 50;
  64.  
  65.     /**
  66.      * Font use to display text
  67.      *
  68.      * @var integer
  69.      */
  70.     var $_font = 2;  // gd internal small font
  71.  
  72.     /**
  73.      * Bar width
  74.      *
  75.      * @var integer
  76.      */
  77.     var $_barwidth = 1;
  78.  
  79.  
  80.     /**
  81.      * Number set
  82.      * @var array
  83.      */
  84.     var $_number_set = array(
  85.            '0' => array(
  86.                     'A' => array(0,0,0,1,1,0,1),
  87.                     'B' => array(0,1,0,0,1,1,1),
  88.                     'C' => array(1,1,1,0,0,1,0)
  89.                         ),
  90.            '1' => array(
  91.                     'A' => array(0,0,1,1,0,0,1),
  92.                     'B' => array(0,1,1,0,0,1,1),
  93.                     'C' => array(1,1,0,0,1,1,0)
  94.                         ),
  95.            '2' => array(
  96.                     'A' => array(0,0,1,0,0,1,1),
  97.                     'B' => array(0,0,1,1,0,1,1),
  98.                     'C' => array(1,1,0,1,1,0,0)
  99.                         ),
  100.            '3' => array(
  101.                     'A' => array(0,1,1,1,1,0,1),
  102.                     'B' => array(0,1,0,0,0,0,1),
  103.                     'C' => array(1,0,0,0,0,1,0)
  104.                         ),
  105.            '4' => array(
  106.                     'A' => array(0,1,0,0,0,1,1),
  107.                     'B' => array(0,0,1,1,1,0,1),
  108.                     'C' => array(1,0,1,1,1,0,0)
  109.                         ),
  110.            '5' => array(
  111.                     'A' => array(0,1,1,0,0,0,1),
  112.                     'B' => array(0,1,1,1,0,0,1),
  113.                     'C' => array(1,0,0,1,1,1,0)
  114.                         ),
  115.            '6' => array(
  116.                     'A' => array(0,1,0,1,1,1,1),
  117.                     'B' => array(0,0,0,0,1,0,1),
  118.                     'C' => array(1,0,1,0,0,0,0)
  119.                         ),
  120.            '7' => array(
  121.                     'A' => array(0,1,1,1,0,1,1),
  122.                     'B' => array(0,0,1,0,0,0,1),
  123.                     'C' => array(1,0,0,0,1,0,0)
  124.                         ),
  125.            '8' => array(
  126.                     'A' => array(0,1,1,0,1,1,1),
  127.                     'B' => array(0,0,0,1,0,0,1),
  128.                     'C' => array(1,0,0,1,0,0,0)
  129.                         ),
  130.            '9' => array(
  131.                     'A' => array(0,0,0,1,0,1,1),
  132.                     'B' => array(0,0,1,0,1,1,1),
  133.                     'C' => array(1,1,1,0,1,0,0)
  134.                         )
  135.         );
  136.  
  137.  
  138.     var $_number_set_left_coding = array(
  139.            '0' => array('A','A','A','A','A','A'),
  140.            '1' => array('A','A','B','A','B','B'),
  141.            '2' => array('A','A','B','B','A','B'),
  142.            '3' => array('A','A','B','B','B','A'),
  143.            '4' => array('A','B','A','A','B','B'),
  144.            '5' => array('A','B','B','A','A','B'),
  145.            '6' => array('A','B','B','B','A','A'),
  146.            '7' => array('A','B','A','B','A','B'),
  147.            '8' => array('A','B','A','B','B','A'),
  148.            '9' => array('A','B','B','A','B','A')
  149.         );
  150.  
  151.  
  152.  
  153.     /**
  154.      * Draws a UPC-A image barcode
  155.      *
  156.      * @param   string $text     A text that should be in the image barcode
  157.      * @param   string $imgtype  The image type that will be generated
  158.      *
  159.      * @return  image            The corresponding Interleaved 2 of 5 image barcode
  160.      *
  161.      * @access  public
  162.      *
  163.      * @author  Jeffrey K. Brown <jkb@darkfantastic.net>
  164.      * @author  Didier Fournout <didier.fournout@nyc.fr>
  165.      *
  166.      */
  167.     function &draw($text, $imgtype = 'png')
  168.     {
  169.         $error = false;
  170.         if ((is_numeric($text)==false) || (strlen($text)!=12)) {
  171.             $barcodewidth= (12 * 7 * $this->_barwidth) + 3 + 5 + 3 + 2 * (imagefontwidth($this->_font)+1);
  172.             $error = true;
  173.         } else {
  174.             // Calculate the barcode width
  175.             $barcodewidth = (strlen($text)) * (7 * $this->_barwidth)
  176.                 + 3 // left
  177.                 + 5 // center
  178.                 + 3 // right
  179.                 + imagefontwidth($this->_font)+1
  180.                 + imagefontwidth($this->_font)+1   // check digit's padding
  181.                 ;
  182.         }
  183.  
  184.         $barcodelongheight = (int) (imagefontheight($this->_font)/2)+$this->_barcodeheight;
  185.  
  186.         // Create the image
  187.         $img = ImageCreate($barcodewidth, $barcodelongheight+ imagefontheight($this->_font)+1);
  188.  
  189.         // Alocate the black and white colors
  190.         $black = ImageColorAllocate($img, 0, 0, 0);
  191.         $white = ImageColorAllocate($img, 255, 255, 255);
  192.  
  193.         // Fill image with white color
  194.         imagefill($img, 0, 0, $white);
  195.  
  196.         if ($error) {
  197.             $imgerror = ImageCreate($barcodewidth, $barcodelongheight+imagefontheight($this->_font)+1);
  198.             $red      = ImageColorAllocate($imgerror, 255, 0, 0);
  199.             $black    = ImageColorAllocate($imgerror, 0, 0, 0);
  200.             imagefill($imgerror, 0, 0, $red);
  201.  
  202.             imagestring(
  203.                 $imgerror,
  204.                 $this->_font,
  205.                 $barcodewidth / 2 - (10/2 * imagefontwidth($this->_font)),
  206.                 $this->_barcodeheight / 2,
  207.                 'Code Error',
  208.                 $black
  209.             );
  210.         }
  211.  
  212.         // get the first digit which is the key for creating the first 6 bars
  213.         $key = substr($text,0,1);
  214.  
  215.         // Initiate x position
  216.         $xpos = 0;
  217.  
  218.         // print first digit
  219.         imagestring($img, $this->_font, $xpos, $this->_barcodeheight, $key, $black);
  220.         $xpos= imagefontwidth($this->_font) + 1;
  221.  
  222.  
  223.  
  224.         // Draws the left guard pattern (bar-space-bar)
  225.         // bar
  226.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  227.         $xpos += $this->_barwidth;
  228.         // space
  229.         $xpos += $this->_barwidth;
  230.         // bar
  231.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  232.         $xpos += $this->_barwidth;
  233.  
  234.         $set_array = $this->_number_set_left_coding[$key];
  235.  
  236.  
  237.  
  238.         foreach ($this->_number_set['0'][$set_array[0]] as $bar) {
  239.             if ($bar) {
  240.                 imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  241.             }
  242.             $xpos += $this->_barwidth;
  243.         }
  244.  
  245.  
  246.  
  247.         // Draw left $text contents
  248.         for ($idx = 1; $idx < 6; $idx ++) {
  249.             $value=substr($text,$idx,1);
  250.             imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
  251.  
  252.             //foreach ($this->_number_set[$value][$set_array[$idx-1]] as $bar) {
  253.  
  254.             foreach ($this->_number_set[$value][$set_array[$idx]] as $bar) {
  255.                 if ($bar) {
  256.                     imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
  257.                 }
  258.                 $xpos += $this->_barwidth;
  259.             }
  260.         }
  261.  
  262.  
  263.         // Draws the center pattern (space-bar-space-bar-space)
  264.         // space
  265.         $xpos += $this->_barwidth;
  266.         // bar
  267.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  268.         $xpos += $this->_barwidth;
  269.         // space
  270.         $xpos += $this->_barwidth;
  271.         // bar
  272.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  273.         $xpos += $this->_barwidth;
  274.         // space
  275.         $xpos += $this->_barwidth;
  276.  
  277.  
  278.         // Draw right $text contents
  279.         for ($idx = 6; $idx < 11; $idx ++) {
  280.             $value=substr($text,$idx,1);
  281.             imagestring ($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
  282.             foreach ($this->_number_set[$value]['C'] as $bar) {
  283.                 if ($bar) {
  284.                     imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $this->_barcodeheight, $black);
  285.                 }
  286.                 $xpos += $this->_barwidth;
  287.             }
  288.         }
  289.  
  290.  
  291.  
  292.         $value = substr($text,11,1);
  293.         foreach ($this->_number_set[$value]['C'] as $bar) {
  294.             if ($bar) {
  295.                 imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  296.             }
  297.             $xpos += $this->_barwidth;
  298.         }
  299.  
  300.  
  301.  
  302.         // Draws the right guard pattern (bar-space-bar)
  303.         // bar
  304.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  305.         $xpos += $this->_barwidth;
  306.         // space
  307.         $xpos += $this->_barwidth;
  308.         // bar
  309.         imagefilledrectangle($img, $xpos, 0, $xpos + $this->_barwidth - 1, $barcodelongheight, $black);
  310.         $xpos += $this->_barwidth;
  311.  
  312.  
  313.         // Print Check Digit
  314.         imagestring($img, $this->_font, $xpos+1, $this->_barcodeheight, $value, $black);
  315.  
  316.         if ($error) {
  317.             return $imgerror;
  318.         } else {
  319.             return $img;
  320.         }
  321.     } // function create
  322.  
  323. } // class
  324. ?>