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

Source for file Ean13.php

Documentation is available at Ean13.php

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

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