Image_Barcode2
[ class tree: Image_Barcode2 ] [ index: Image_Barcode2 ] [ all elements ]

Source for file Barcode2.php

Documentation is available at Barcode2.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode2 class
  6.  *
  7.  * Package to render barcodes
  8.  *
  9.  * PHP versions 5
  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_Barcode2
  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.  * @link      http://pear.php.net/package/Image_Barcode2
  23.  */
  24.  
  25. require_once 'Image/Barcode2/Writer.php';
  26. require_once 'Image/Barcode2/Driver.php';
  27. require_once 'Image/Barcode2/Exception.php';
  28.  
  29. /**
  30.  * Image_Barcode2 class
  31.  *
  32.  * Package which provides a method to create barcode using GD library.
  33.  *
  34.  * @category  Image
  35.  * @package   Image_Barcode2
  36.  * @author    Marcelo Subtil Marcal <msmarcal@php.net>
  37.  * @copyright 2005 The PHP Group
  38.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  39.  * @version   Release: @package_version@
  40.  * @link      http://pear.php.net/package/Image_Barcode2
  41.  */
  42. {
  43.     /**
  44.      * Image type
  45.      */
  46.     const IMAGE_PNG     = 'png';
  47.     const IMAGE_GIF     = 'gif';
  48.     const IMAGE_JPEG    = 'jpg';
  49.  
  50.     /**
  51.      * Barcode type
  52.      */
  53.     const BARCODE_CODE39    = 'code39';
  54.     const BARCODE_INT25     = 'int25';
  55.     const BARCODE_EAN13     = 'ean13';
  56.     const BARCODE_UPCA      = 'upca';
  57.     const BARCODE_UPCE      = 'upce';
  58.     const BARCODE_CODE128   = 'code128';
  59.     const BARCODE_EAN8      = 'ean8';
  60.     const BARCODE_POSTNET   = 'postnet';
  61.  
  62.     /**
  63.      * Rotation type
  64.      */
  65.     const ROTATE_NONE     = 0;
  66.     const ROTATE_RIGHT    = 90;
  67.     const ROTATE_UTURN    = 180;
  68.     const ROTATE_LEFT     = 270;
  69.  
  70.  
  71.     /**
  72.      * Draws a image barcode
  73.      *
  74.      * @param string  $text           A text that should be in the image barcode
  75.      * @param string  $type           The barcode type. Supported types:
  76.      *                                 code39 - Code 3 of 9
  77.      *                                 int25  - 2 Interleaved 5
  78.      *                                 ean13  - EAN 13
  79.      *                                 upca   - UPC-A
  80.      *                                 upce   - UPC-E
  81.      *                                 code128
  82.      *                                 ean8
  83.      *                                 postnet
  84.      * @param string  $imgtype        The image type that will be generated
  85.      *                                 (gif, jpg, png)
  86.      * @param boolean $bSendToBrowser if the image shall be outputted to the
  87.      *                                  browser, or be returned.
  88.      * @param integer $height         The image height
  89.      * @param integer $width          The image width
  90.      * @param boolean $showText       The text should be placed under barcode
  91.      * @param integer $rotation       The rotation angle
  92.      *
  93.      * @return resource The corresponding gd image resource
  94.      *               
  95.      * @throws Image_Barcode2_Exception
  96.      * @access public
  97.      *
  98.      * @author Marcelo Subtil Marcal <msmarcal@php.net>
  99.      * @since  Image_Barcode2 0.3
  100.      */
  101.     public static function draw($text
  102.         $type = Image_Barcode2::BARCODE_INT25,
  103.         $imgtype = Image_Barcode2::IMAGE_PNG
  104.         $bSendToBrowser = true,
  105.         $height = 60,
  106.         $width = 1,
  107.         $showText = true,
  108.         $rotation = Image_Barcode2::ROTATE_NONE
  109.     {
  110.         //Make sure no bad files are included
  111.         if (!preg_match('/^[a-zA-Z0-9]+$/'$type)) {
  112.             throw new Image_Barcode2_Exception('Invalid barcode type ' $type);
  113.         }
  114.  
  115.         if (!include_once 'Image/Barcode2/Driver/' ucfirst($type'.php'{
  116.             throw new Image_Barcode2_Exception($type ' barcode is not supported');
  117.         }
  118.  
  119.         $classname 'Image_Barcode2_Driver_' ucfirst($type);
  120.  
  121.         $obj = new $classname(new Image_Barcode2_Writer());
  122.  
  123.         if (!$obj instanceof Image_Barcode2_Driver{
  124.             throw new Image_Barcode2_Exception(
  125.                 "'$classname' does not implement Image_Barcode2_Driver"
  126.             );
  127.         }
  128.  
  129.         if (!$obj instanceof Image_Barcode2_DualWidth{
  130.             $obj->setBarcodeWidth($width);
  131.         }
  132.  
  133.         if (!$obj instanceof Image_Barcode2_DualHeight{
  134.             $obj->setBarcodeHeight($height);
  135.         }
  136.  
  137.         $obj->setBarcode($text);
  138.         $obj->setShowText($showText);
  139.  
  140.         $obj->validate();
  141.         $img $obj->draw();
  142.  
  143.         // Rotate image on demand
  144.         if ($rotation !== self::ROTATE_NONE{
  145.             $img = imagerotate($img$rotation0);
  146.         }
  147.  
  148.         if ($bSendToBrowser{
  149.             // Send image to browser
  150.             switch ($imgtype{
  151.             case self::IMAGE_GIF:
  152.                 header('Content-type: image/gif');
  153.                 imagegif($img);
  154.                 imagedestroy($img);
  155.                 break;
  156.  
  157.             case self::IMAGE_JPEG:
  158.                 header('Content-type: image/jpg');
  159.                 imagejpeg($img);
  160.                 imagedestroy($img);
  161.                 break;
  162.  
  163.             default:
  164.                 header('Content-type: image/png');
  165.                 imagepng($img);
  166.                 imagedestroy($img);
  167.                 break;
  168.             }
  169.         }
  170.  
  171.         return $img;
  172.     }
  173. }

Documentation generated on Mon, 25 Feb 2013 13:00:03 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.