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

Source for file Upca.php

Documentation is available at Upca.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode2_Driver_Upca class
  6.  *
  7.  * Renders UPC-A 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    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.  * @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.  * UPC-A
  32.  *
  33.  * Package which provides a method to create UPC-A barcode using GD library.
  34.  *
  35.  * Slightly Modified Ean13.php to get Upca.php I needed a way to print
  36.  * UPC-A bar codes on a PHP page.  The Image_Barcode2 class seemed like
  37.  * the best way to do it, so I modified ean13 to print in the UPC-A format.
  38.  * Checked the bar code tables against some documentation below (no errors)
  39.  * and validated the changes with my trusty cue-cat.
  40.  * http://www.indiana.edu/~atmat/units/barcodes/bar_t4.htm
  41.  *
  42.  * @category  Image
  43.  * @package   Image_Barcode2
  44.  * @author    Jeffrey K. Brown <jkb@darkfantastic.net>
  45.  * @author    Didier Fournout <didier.fournout@nyc.fr>
  46.  * @copyright 2005 The PHP Group
  47.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  48.  * @version   Release: @package_version@
  49.  * @link      http://pear.php.net/package/Image_Barcode2
  50.  */
  51. {
  52.     /**
  53.      * Coding map
  54.      * @var array 
  55.      */
  56.     private $_codingmap = array(
  57.         '0' => array(
  58.             'L' => array(0,0,0,1,1,0,1),
  59.             'R' => array(1,1,1,0,0,1,0)
  60.         ),
  61.         '1' => array(
  62.             'L' => array(0,0,1,1,0,0,1),
  63.             'R' => array(1,1,0,0,1,1,0)
  64.         ),
  65.         '2' => array(
  66.             'L' => array(0,0,1,0,0,1,1),
  67.             'R' => array(1,1,0,1,1,0,0)
  68.         ),
  69.         '3' => array(
  70.             'L' => array(0,1,1,1,1,0,1),
  71.             'R' => array(1,0,0,0,0,1,0)
  72.         ),
  73.         '4' => array(
  74.             'L' => array(0,1,0,0,0,1,1),
  75.             'R' => array(1,0,1,1,1,0,0)
  76.         ),
  77.         '5' => array(
  78.             'L' => array(0,1,1,0,0,0,1),
  79.             'R' => array(1,0,0,1,1,1,0)
  80.         ),
  81.         '6' => array(
  82.             'L' => array(0,1,0,1,1,1,1),
  83.             'R' => array(1,0,1,0,0,0,0)
  84.         ),
  85.         '7' => array(
  86.             'L' => array(0,1,1,1,0,1,1),
  87.             'R' => array(1,0,0,0,1,0,0)
  88.         ),
  89.         '8' => array(
  90.             'L' => array(0,1,1,0,1,1,1),
  91.             'R' => array(1,0,0,1,0,0,0)
  92.         ),
  93.         '9' => array(
  94.             'L' => array(0,0,0,1,0,1,1),
  95.             'R' => array(1,1,1,0,1,0,0)
  96.         )
  97.     );
  98.  
  99.     /**
  100.      * Class constructor
  101.      *
  102.      * @param Image_Barcode2_Writer $writer Library to use.
  103.      */
  104.     public function __construct(Image_Barcode2_Writer $writer
  105.     {
  106.         parent::__construct($writer);
  107.         $this->setBarcodeHeight(50);
  108.         $this->setBarcodeWidth(1);
  109.     }
  110.  
  111.  
  112.     /**
  113.      * Validate barcode
  114.      *
  115.      * @return void 
  116.      * @throws Image_Barcode2_Exception
  117.      */
  118.     public function validate()
  119.     {
  120.         // Check barcode for invalid characters
  121.         if (!preg_match('/^[0-9]{12}$/'$this->getBarcode())) {
  122.             throw new Image_Barcode2_Exception('Invalid barcode');
  123.         }
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Draws a UPC-A image barcode
  129.      *
  130.      * @return resource            The corresponding UPC-A image barcode
  131.      *
  132.      * @author  Jeffrey K. Brown <jkb@darkfantastic.net>
  133.      * @author  Didier Fournout <didier.fournout@nyc.fr>
  134.      */
  135.     public function draw()
  136.     {
  137.         $text     $this->getBarcode();
  138.         $writer   $this->getWriter();
  139.         $fontsize $this->getFontSize();
  140.  
  141.         // Calculate the barcode width
  142.         $barcodewidth (strlen($text)) (7 * $this->getBarcodeWidth())
  143.             + 3 // left
  144.             + 5 // center
  145.             + 3 // right
  146.             + $writer->imagefontwidth($fontsize+ 1
  147.             + $writer->imagefontwidth($fontsize+ 1 // check digit padding
  148.             ;
  149.  
  150.  
  151.         $barcodelongheight = (int)($writer->imagefontheight($fontsize/ 2)
  152.             + $this->getBarcodeHeight();
  153.  
  154.         // Create the image
  155.         $img $writer->imagecreate(
  156.             $barcodewidth,
  157.             $barcodelongheight $writer->imagefontheight($fontsize+ 1
  158.         );
  159.  
  160.         // Alocate the black and white colors
  161.         $black $writer->imagecolorallocate($img000);
  162.         $white $writer->imagecolorallocate($img255255255);
  163.  
  164.         // Fill image with white color
  165.         $writer->imagefill($img00$white);
  166.  
  167.         // get the first digit which is the key for creating the first 6 bars
  168.         $key substr($text01);
  169.  
  170.         // Initiate x position
  171.         $xpos = 0;
  172.  
  173.         // print first digit
  174.         if ($this->showText{
  175.             $writer->imagestring(
  176.                 $img,
  177.                 $fontsize,
  178.                 $xpos,
  179.                 $this->getBarcodeHeight(),
  180.                 $key,
  181.                 $black
  182.             );
  183.         }
  184.         $xpos $writer->imagefontwidth($fontsize+ 1;
  185.  
  186.  
  187.         // Draws the left guard pattern (bar-space-bar)
  188.         // bar
  189.         $writer->imagefilledrectangle(
  190.             $img,
  191.             $xpos,
  192.             0,
  193.             $xpos $this->getBarcodeWidth(- 1,
  194.             $barcodelongheight,
  195.             $black
  196.         );
  197.  
  198.         $xpos += $this->getBarcodeWidth();
  199.         // space
  200.         $xpos += $this->getBarcodeWidth();
  201.         // bar
  202.         $writer->imagefilledrectangle(
  203.             $img,
  204.             $xpos,
  205.             0,
  206.             $xpos $this->getBarcodeWidth(- 1,
  207.             $barcodelongheight,
  208.             $black
  209.         );
  210.  
  211.         $xpos += $this->getBarcodeWidth();
  212.  
  213.  
  214.         foreach ($this->_codingmap[$key]['L'as $bar{
  215.             if ($bar{
  216.                 $writer->imagefilledrectangle(
  217.                     $img,
  218.                     $xpos,
  219.                     0,
  220.                     $xpos $this->getBarcodeWidth(- 1,
  221.                     $barcodelongheight,
  222.                     $black
  223.                 );
  224.             }
  225.             $xpos += $this->getBarcodeWidth();
  226.         }
  227.  
  228.  
  229.  
  230.         // Draw left $text contents
  231.         for ($idx = 1; $idx < 6; $idx ++{
  232.             $value substr($text$idx1);
  233.  
  234.             if ($this->showText{
  235.                 $writer->imagestring(
  236.                     $img,
  237.                     $fontsize,
  238.                     $xpos + 1,
  239.                     $this->getBarcodeHeight(),
  240.                     $value,
  241.                     $black
  242.                 );
  243.             }
  244.  
  245.             foreach ($this->_codingmap[$value]['L'as $bar{
  246.                 if ($bar{
  247.                     $writer->imagefilledrectangle(
  248.                         $img,
  249.                         $xpos,
  250.                         0,
  251.                         $xpos $this->getBarcodeWidth(- 1,
  252.                         $this->getBarcodeHeight(),
  253.                         $black
  254.                     );
  255.                 }
  256.                 $xpos += $this->getBarcodeWidth();
  257.             }
  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 = 6; $idx < 11; $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]['R'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.  
  321.  
  322.         $value substr($text111);
  323.         foreach ($this->_codingmap[$value]['R'as $bar{
  324.             if ($bar{
  325.                 $writer->imagefilledrectangle(
  326.                     $img,
  327.                     $xpos,
  328.                     0,
  329.                     $xpos $this->getBarcodeWidth(- 1,
  330.                     $barcodelongheight,
  331.                     $black
  332.                 );
  333.  
  334.             }
  335.             $xpos += $this->getBarcodeWidth();
  336.         }
  337.  
  338.  
  339.  
  340.         // Draws the right guard pattern (bar-space-bar)
  341.         // bar
  342.         $writer->imagefilledrectangle(
  343.             $img,
  344.             $xpos,
  345.             0,
  346.             $xpos $this->getBarcodeWidth(- 1,
  347.             $barcodelongheight,
  348.             $black
  349.         );
  350.  
  351.         $xpos += $this->getBarcodeWidth();
  352.         // space
  353.         $xpos += $this->getBarcodeWidth();
  354.         // bar
  355.         $writer->imagefilledrectangle(
  356.             $img,
  357.             $xpos,
  358.             0,
  359.             $xpos $this->getBarcodeWidth(- 1,
  360.             $barcodelongheight,
  361.             $black
  362.         );
  363.  
  364.         $xpos += $this->getBarcodeWidth();
  365.  
  366.  
  367.         // Print Check Digit
  368.         if ($this->showText{
  369.             $writer->imagestring(
  370.                 $img,
  371.                 $fontsize,
  372.                 $xpos + 1,
  373.                 $this->getBarcodeHeight(),
  374.                 $value,
  375.                 $black
  376.             );
  377.         }
  378.  
  379.         return $img;
  380.     }
  381.  
  382. // class

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