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

Source for file Ean8.php

Documentation is available at Ean8.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode2_Driver_Ean8 class
  6.  *
  7.  * Renders EAN 8 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    Tobias Frost <tobi@coldtobi.de>
  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.  * @link      http://pear.php.net/package/Image_Barcode2
  24.  */
  25.  
  26. require_once 'Image/Barcode2/Driver.php';
  27. require_once 'Image/Barcode2/Common.php';
  28. require_once 'Image/Barcode2/Exception.php';
  29.  
  30. /**
  31.  * EAN 8
  32.  *
  33.  * Package which provides a method to create EAN 8 barcode using GD library.
  34.  *
  35.  * @category  Image
  36.  * @package   Image_Barcode2
  37.  * @author    Tobias Frost <tobi@coldtobi.de>
  38.  * @author    Didier Fournout <didier.fournout@nyc.fr>
  39.  * @copyright 2005 The PHP Group
  40.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  41.  * @version   Release: @package_version@
  42.  * @link      http://pear.php.net/package/Image_Barcode2
  43.  */
  44. {
  45.     /**
  46.      * Coding map
  47.      * @var array 
  48.      */
  49.     private $_codingmap = array(
  50.         '0' => array(
  51.             'A' => array(0,0,0,1,1,0,1),
  52.             'C' => array(1,1,1,0,0,1,0)
  53.         ),
  54.         '1' => array(
  55.             'A' => array(0,0,1,1,0,0,1),
  56.             'C' => array(1,1,0,0,1,1,0)
  57.         ),
  58.         '2' => array(
  59.             'A' => array(0,0,1,0,0,1,1),
  60.             'C' => array(1,1,0,1,1,0,0)
  61.         ),
  62.         '3' => array(
  63.             'A' => array(0,1,1,1,1,0,1),
  64.             'C' => array(1,0,0,0,0,1,0)
  65.         ),
  66.         '4' => array(
  67.             'A' => array(0,1,0,0,0,1,1),
  68.             'C' => array(1,0,1,1,1,0,0)
  69.         ),
  70.         '5' => array(
  71.             'A' => array(0,1,1,0,0,0,1),
  72.             'C' => array(1,0,0,1,1,1,0)
  73.         ),
  74.         '6' => array(
  75.             'A' => array(0,1,0,1,1,1,1),
  76.             'C' => array(1,0,1,0,0,0,0)
  77.         ),
  78.         '7' => array(
  79.             'A' => array(0,1,1,1,0,1,1),
  80.             'C' => array(1,0,0,0,1,0,0)
  81.         ),
  82.         '8' => array(
  83.             'A' => array(0,1,1,0,1,1,1),
  84.             'C' => array(1,0,0,1,0,0,0)
  85.         ),
  86.         '9' => array(
  87.             'A' => array(0,0,0,1,0,1,1),
  88.             'C' => array(1,1,1,0,1,0,0)
  89.         )
  90.     );
  91.  
  92.     /**
  93.      * Class constructor
  94.      *
  95.      * @param Image_Barcode2_Writer $writer Library to use.
  96.      */
  97.     public function __construct(Image_Barcode2_Writer $writer
  98.     {
  99.         parent::__construct($writer);
  100.         $this->setBarcodeHeight(50);
  101.         $this->setBarcodeWidth(1);
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Validate barcode
  107.      *
  108.      * @return void 
  109.      * @throws Image_Barcode2_Exception
  110.      */
  111.     public function validate()
  112.     {
  113.         // Check barcode for invalid characters
  114.         if (!preg_match('/^[0-9]{8}$/'$this->getBarcode())) {
  115.             throw new Image_Barcode2_Exception('Invalid barcode');
  116.         }
  117.     }
  118.  
  119.  
  120.     /**
  121.      * Draws a EAN 8 image barcode
  122.      *
  123.      * @return resource            The corresponding EAN8 image barcode
  124.      *
  125.      * @access public
  126.      *
  127.      * @author     Tobias Frost tobi@coldtobi.de
  128.      *                 based on the EAN13 code by Didier Fournout <didier.fournout@nyc.fr>
  129.      */
  130.     public function draw()
  131.     {
  132.         $text     $this->getBarcode();
  133.         $writer   $this->getWriter();
  134.         $fontsize $this->getFontSize();
  135.  
  136.         // Calculate the barcode width
  137.         $barcodewidth (strlen($text)) (7 * $this->getBarcodeWidth())
  138.             + 3 * $this->getBarcodeWidth(// left
  139.             + 5 * $this->getBarcodeWidth(// center
  140.             + 3 * $this->getBarcodeWidth(// right
  141.             ;
  142.  
  143.         $barcodelongheight = (int)($writer->imagefontheight($fontsize/ 2)
  144.              + $this->getBarcodeHeight();
  145.  
  146.         // Create the image
  147.         $img $writer->imagecreate(
  148.             $barcodewidth,
  149.             $barcodelongheight $writer->imagefontheight($fontsize+ 1
  150.         );
  151.  
  152.         // Alocate the black and white colors
  153.         $black $writer->imagecolorallocate($img000);
  154.         $white $writer->imagecolorallocate($img255255255);
  155.  
  156.         // Fill image with white color
  157.         $writer->imagefill($img00$white);
  158.  
  159.         // Initiate x position
  160.         $xpos = 0;
  161.  
  162.         // Draws the left guard pattern (bar-space-bar)
  163.         // bar
  164.         $writer->imagefilledrectangle(
  165.             $img,
  166.             $xpos,
  167.             0,
  168.             $xpos $this->getBarcodeWidth(- 1,
  169.             $barcodelongheight,
  170.             $black
  171.         );
  172.         $xpos += $this->getBarcodeWidth();
  173.         // space
  174.         $xpos += $this->getBarcodeWidth();
  175.         // bar
  176.         $writer->imagefilledrectangle(
  177.             $img,
  178.             $xpos,
  179.             0,
  180.             $xpos $this->getBarcodeWidth(- 1,
  181.             $barcodelongheight,
  182.             $black
  183.         );
  184.         $xpos += $this->getBarcodeWidth();
  185.  
  186.         for ($idx = 0; $idx < 4; $idx ++{
  187.             $value substr($text$idx1);
  188.  
  189.             if ($this->showText{
  190.                 $writer->imagestring(
  191.                     $img,
  192.                     $fontsize,
  193.                     $xpos + 1,
  194.                     $this->getBarcodeHeight(),
  195.                     $value,
  196.                     $black
  197.                 );
  198.             }
  199.  
  200.             foreach ($this->_codingmap[$value]['A'as $bar{
  201.                 if ($bar{
  202.                     $writer->imagefilledrectangle(
  203.                         $img,
  204.                         $xpos,
  205.                         0,
  206.                         $xpos $this->getBarcodeWidth(- 1,
  207.                         $this->getBarcodeHeight(),
  208.                         $black
  209.                     );
  210.                 }
  211.                 $xpos += $this->getBarcodeWidth();
  212.             }
  213.         }
  214.  
  215.         // Draws the center pattern (space-bar-space-bar-space)
  216.         // space
  217.         $xpos += $this->getBarcodeWidth();
  218.         // bar
  219.         $writer->imagefilledrectangle(
  220.             $img,
  221.             $xpos,
  222.             0,
  223.             $xpos $this->getBarcodeWidth(- 1,
  224.             $barcodelongheight,
  225.             $black
  226.         );
  227.  
  228.         $xpos += $this->getBarcodeWidth();
  229.         // space
  230.         $xpos += $this->getBarcodeWidth();
  231.         // bar
  232.         $writer->imagefilledrectangle(
  233.             $img,
  234.             $xpos,
  235.             0,
  236.             $xpos $this->getBarcodeWidth(- 1,
  237.             $barcodelongheight,
  238.             $black
  239.         );
  240.  
  241.         $xpos += $this->getBarcodeWidth();
  242.         // space
  243.         $xpos += $this->getBarcodeWidth();
  244.  
  245.  
  246.         // Draw right $text contents
  247.         for ($idx = 4; $idx < 8; $idx ++{
  248.             $value substr($text$idx1);
  249.  
  250.             if ($this->showText{
  251.                 $writer->imagestring(
  252.                     $img,
  253.                     $fontsize,
  254.                     $xpos + 1,
  255.                     $this->getBarcodeHeight(),
  256.                     $value,
  257.                     $black
  258.                 );
  259.             }
  260.  
  261.             foreach ($this->_codingmap[$value]['C'as $bar{
  262.                 if ($bar{
  263.                     $writer->imagefilledrectangle(
  264.                         $img,
  265.                         $xpos,
  266.                         0,
  267.                         $xpos $this->getBarcodeWidth(- 1,
  268.                         $this->getBarcodeHeight(),
  269.                         $black
  270.                     );
  271.                 }
  272.                 $xpos += $this->getBarcodeWidth();
  273.             }
  274.         }
  275.  
  276.         // Draws the right guard pattern (bar-space-bar)
  277.         // bar
  278.         $writer->imagefilledrectangle(
  279.             $img,
  280.             $xpos,
  281.             0,
  282.             $xpos $this->getBarcodeWidth(- 1,
  283.             $barcodelongheight,
  284.             $black
  285.         );
  286.         $xpos += $this->getBarcodeWidth();
  287.         // space
  288.         $xpos += $this->getBarcodeWidth();
  289.         // bar
  290.         $writer->imagefilledrectangle(
  291.             $img,
  292.             $xpos,
  293.             0,
  294.             $xpos $this->getBarcodeWidth(- 1,
  295.             $barcodelongheight,
  296.             $black
  297.         );
  298.  
  299.         return $img;
  300.     // function create
  301.  
  302. // class

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