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 / int25.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.2 KB  |  174 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode_int25 class
  6.  *
  7.  * Renders Interleaved 2 of 5 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     Marcelo Subtil Marcal <msmarcal@php.net>
  20.  * @copyright  2005 The PHP Group
  21.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  22.  * @version    CVS: $Id: int25.php,v 1.4 2006/12/13 19:29:30 cweiske Exp $
  23.  * @link       http://pear.php.net/package/Image_Barcode
  24.  */
  25.  
  26. require_once "PEAR.php";
  27. require_once "Image/Barcode.php";
  28.  
  29.  
  30. /**
  31.  * Image_Barcode_int25 class
  32.  *
  33.  * Package which provides a method to create Interleaved 2 of 5 barcode using GD library.
  34.  *
  35.  * @category   Image
  36.  * @package    Image_Barcode
  37.  * @author     Marcelo Subtil Marcal <msmarcal@php.net>
  38.  * @copyright  2005 The PHP Group
  39.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  40.  * @version    Release: @package_version@
  41.  * @link       http://pear.php.net/package/Image_Barcode
  42.  */
  43. class Image_Barcode_int25 extends Image_Barcode
  44. {
  45.     /**
  46.      * Barcode type
  47.      * @var string
  48.      */
  49.     var $_type = 'int25';
  50.  
  51.     /**
  52.      * Barcode height
  53.      *
  54.      * @var integer
  55.      */
  56.     var $_barcodeheight = 50;
  57.  
  58.     /**
  59.      * Bar thin width
  60.      *
  61.      * @var integer
  62.      */
  63.     var $_barthinwidth = 1;
  64.  
  65.     /**
  66.      * Bar thick width
  67.      *
  68.      * @var integer
  69.      */
  70.     var $_barthickwidth = 3;
  71.  
  72.     /**
  73.      * Coding map
  74.      * @var array
  75.      */
  76.     var $_coding_map = array(
  77.            '0' => '00110',
  78.            '1' => '10001',
  79.            '2' => '01001',
  80.            '3' => '11000',
  81.            '4' => '00101',
  82.            '5' => '10100',
  83.            '6' => '01100',
  84.            '7' => '00011',
  85.            '8' => '10010',
  86.            '9' => '01010'
  87.         );
  88.  
  89.     /**
  90.      * Draws a Interleaved 2 of 5 image barcode
  91.      *
  92.      * @param  string $text     A text that should be in the image barcode
  93.      * @param  string $imgtype  The image type that will be generated
  94.      *
  95.      * @return image            The corresponding Interleaved 2 of 5 image barcode
  96.      *
  97.      * @access public
  98.      *
  99.      * @author Marcelo Subtil Marcal <msmarcal@php.net>
  100.      * @since  Image_Barcode 0.3
  101.      */
  102.  
  103.     function &draw($text, $imgtype = 'png')
  104.     {
  105.  
  106.         $text = trim($text);
  107.  
  108.         if (!preg_match("/[0-9]/",$text)) return;
  109.  
  110.         // if odd $text lenght adds a '0' at string beginning
  111.         $text = strlen($text) % 2 ? '0' . $text : $text;
  112.  
  113.         // Calculate the barcode width
  114.         $barcodewidth = (strlen($text)) * (3 * $this->_barthinwidth + 2 * $this->_barthickwidth) +
  115.             (strlen($text)) * 2.5 +
  116.             (7 * $this->_barthinwidth + $this->_barthickwidth) + 3;
  117.  
  118.         // Create the image
  119.         $img = ImageCreate($barcodewidth, $this->_barcodeheight);
  120.  
  121.         // Alocate the black and white colors
  122.         $black = ImageColorAllocate($img, 0, 0, 0);
  123.         $white = ImageColorAllocate($img, 255, 255, 255);
  124.  
  125.         // Fill image with white color
  126.         imagefill($img, 0, 0, $white);
  127.  
  128.         // Initiate x position
  129.         $xpos = 0;
  130.  
  131.         // Draws the leader
  132.         for ($i=0; $i < 2; $i++) {
  133.             $elementwidth = $this->_barthinwidth;
  134.             imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black);
  135.             $xpos += $elementwidth;
  136.             $xpos += $this->_barthinwidth;
  137.             $xpos ++;
  138.         }
  139.  
  140.         // Draw $text contents
  141.         for ($idx = 0; $idx < strlen($text); $idx += 2) {       // Draw 2 chars at a time
  142.             $oddchar  = substr($text, $idx, 1);                 // get odd char
  143.             $evenchar = substr($text, $idx + 1, 1);             // get even char
  144.  
  145.             // interleave
  146.             for ($baridx = 0; $baridx < 5; $baridx++) {
  147.  
  148.                 // Draws odd char corresponding bar (black)
  149.                 $elementwidth = (substr($this->_coding_map[$oddchar], $baridx, 1)) ?  $this->_barthickwidth : $this->_barthinwidth;
  150.                 imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black);
  151.                 $xpos += $elementwidth;
  152.  
  153.                 // Left enought space to draw even char (white)
  154.                 $elementwidth = (substr($this->_coding_map[$evenchar], $baridx, 1)) ?  $this->_barthickwidth : $this->_barthinwidth;
  155.                 $xpos += $elementwidth; 
  156.                 $xpos ++;
  157.             }
  158.         }
  159.  
  160.  
  161.         // Draws the trailer
  162.         $elementwidth = $this->_barthickwidth;
  163.         imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black);
  164.         $xpos += $elementwidth;
  165.         $xpos += $this->_barthinwidth;
  166.         $xpos ++;
  167.         $elementwidth = $this->_barthinwidth;
  168.         imagefilledrectangle($img, $xpos, 0, $xpos + $elementwidth - 1, $this->_barcodeheight, $black);
  169.  
  170.         return $img;
  171.     } // function create
  172.  
  173. } // class
  174. ?>