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

Source for file Upce.php

Documentation is available at Upce.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Image_Barcode2_Driver_Upce class
  6.  *
  7.  * Renders UPC-E 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    Ryan McLaughlin <ryanmclaughlin@gmail.com>
  20.  * @copyright 2012 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.  * UPC-E
  31.  *
  32.  * Package which provides a method to create UPC-A barcode using GD library.
  33.  *
  34.  * Slightly Modified Upca.php to get Upce.php I needed a way to print
  35.  * UPC-E bar codes on a PHP page.  The Image_Barcode2 class seemed like
  36.  * the best way to do it, so I modified UPC-A to print in the UPC-E format.
  37.  * Checked the bar code tables against some documentation below (no errors)
  38.  * and validated the changes with my phone app "Barcode Scanner"
  39.  *
  40.  * @category  Image
  41.  * @package   Image_Barcode2
  42.  * @author    Ryan McLaughlin <ryanmclaughlin@gmail.com>
  43.  * @copyright 2012 The PHP Group
  44.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  45.  * @version   Release: @package_version@
  46.  * @link      http://pear.php.net/package/Image_Barcode2
  47.  */
  48. {
  49.     /**
  50.      * Coding map
  51.      * @var array 
  52.      */
  53.     private $_paritypattern = array(
  54.         '0' => array(1,1,1,0,0,0),
  55.         '1' => array(1,1,0,1,0,0),
  56.         '2' => array(1,1,0,0,1,0),
  57.         '3' => array(1,1,0,0,0,1),
  58.         '4' => array(1,0,1,1,0,0),
  59.         '5' => array(1,0,0,1,1,0),
  60.         '6' => array(1,0,0,0,1,1),
  61.         '7' => array(1,0,1,0,1,0),
  62.         '8' => array(1,0,1,0,0,1),
  63.         '9' => array(1,0,0,1,0,1)
  64.     );
  65.         
  66.     private $_codingmap = array(
  67.         '0' => array(
  68.             'O' => array(0,0,0,1,1,0,1),
  69.             'E' => array(0,1,0,0,1,1,1)
  70.         ),
  71.         '1' => array(
  72.             'O' => array(0,0,1,1,0,0,1),
  73.             'E' => array(0,1,1,0,0,1,1)
  74.         ),
  75.         '2' => array(
  76.             'O' => array(0,0,1,0,0,1,1),
  77.             'E' => array(0,0,1,1,0,1,1)
  78.         ),
  79.         '3' => array(
  80.             'O' => array(0,1,1,1,1,0,1),
  81.             'E' => array(0,1,0,0,0,0,1)
  82.         ),
  83.         '4' => array(
  84.             'O' => array(0,1,0,0,0,1,1),
  85.             'E' => array(0,0,1,1,1,0,1)
  86.         ),
  87.         '5' => array(
  88.             'O' => array(0,1,1,0,0,0,1),
  89.             'E' => array(0,1,1,1,0,0,1)
  90.         ),
  91.         '6' => array(
  92.             'O' => array(0,1,0,1,1,1,1),
  93.             'E' => array(0,0,0,0,1,0,1)
  94.         ),
  95.         '7' => array(
  96.             'O' => array(0,1,1,1,0,1,1),
  97.             'E' => array(0,0,1,0,0,0,1)
  98.         ),
  99.         '8' => array(
  100.             'O' => array(0,1,1,0,1,1,1),
  101.             'E' => array(0,0,0,1,0,0,1)
  102.         ),
  103.         '9' => array(
  104.             'O' => array(0,0,0,1,0,1,1),
  105.             'E' => array(0,0,1,0,1,1,1)
  106.         )
  107.     );
  108.  
  109.     /**
  110.      * Class constructor
  111.      *
  112.      * @param Image_Barcode2_Writer $writer Library to use.
  113.      */
  114.     public function __construct(Image_Barcode2_Writer $writer
  115.     {
  116.         parent::__construct($writer);
  117.         $this->setBarcodeHeight(50);
  118.         $this->setBarcodeWidth(1);
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Validate barcode
  124.      *
  125.      * @return void 
  126.      * @throws Image_Barcode2_Exception
  127.      */
  128.     public function validate()
  129.     {
  130.         // Check barcode for invalid characters
  131.         if (!preg_match('/^[0-9]{8}$/'$this->getBarcode())) {
  132.             throw new Image_Barcode2_Exception('Invalid barcode');
  133.         }
  134.     }
  135.  
  136.  
  137.     /**
  138.      * Draws a UPC-E image barcode
  139.      *
  140.      * @return resource            The corresponding UPC-E image barcode
  141.      *
  142.      * @author  Ryan McLaughlin <ryanmclaughlin@gmail.com>
  143.      */
  144.     public function draw()
  145.     {
  146.         $text     $this->getBarcode();
  147.         $writer   $this->getWriter();
  148.         $fontsize $this->getFontSize();
  149.         
  150.         // Calculate the barcode width
  151.         $barcodewidth (strlen($text)) (7 * $this->getBarcodeWidth())
  152.             + $writer->imagefontwidth($fontsize)
  153.             + $writer->imagefontwidth($fontsize// check digit padding
  154.             ;
  155.         
  156.         
  157.         $barcodelongheight = (int)($writer->imagefontheight($fontsize/ 2)
  158.             + $this->getBarcodeHeight();
  159.         
  160.         // Create the image
  161.         $img $writer->imagecreate(
  162.             $barcodewidth,
  163.             $barcodelongheight $writer->imagefontheight($fontsize+ 1
  164.         );
  165.         
  166.         // Alocate the black and white colors
  167.         $black $writer->imagecolorallocate($img000);
  168.         $white $writer->imagecolorallocate($img255255255);
  169.         
  170.         // Fill image with white color
  171.         $writer->imagefill($img00$white);
  172.         
  173.         // get the first digit which is the key for creating the first 6 bars
  174.         $key substr($text01);
  175.         
  176.         // Initiate x position
  177.         $xpos = 0;
  178.         
  179.         // print first digit
  180.         if ($this->showText{
  181.             $writer->imagestring(
  182.                 $img,
  183.                 $fontsize,
  184.                 $xpos,
  185.                 $this->getBarcodeHeight(),
  186.                 $key,
  187.                 $black
  188.             );
  189.         }
  190.         $xpos $writer->imagefontwidth($fontsize+ 1;
  191.         
  192.         
  193.         // Draws the left guard pattern (bar-space-bar)
  194.         // bar
  195.         $writer->imagefilledrectangle(
  196.             $img,
  197.             $xpos,
  198.             0,
  199.             $xpos $this->getBarcodeWidth(- 1,
  200.             $barcodelongheight,
  201.             $black
  202.         );
  203.         
  204.         $xpos += $this->getBarcodeWidth();
  205.         // space
  206.         $xpos += $this->getBarcodeWidth();
  207.         // bar
  208.         $writer->imagefilledrectangle(
  209.             $img,
  210.             $xpos,
  211.             0,
  212.             $xpos $this->getBarcodeWidth(- 1,
  213.             $barcodelongheight,
  214.             $black
  215.         );
  216.         
  217.         $xpos += $this->getBarcodeWidth();
  218.         
  219.         
  220.         // Draw middle $text contents
  221.         $checkdigit substr($text71);
  222.         for ($idx = 1; $idx < 7; $idx ++{
  223.             $value substr($text$idx1);
  224.  
  225.             if ($this->showText{
  226.                 $writer->imagestring(
  227.                     $img,
  228.                     $fontsize,
  229.                     $xpos + 1,
  230.                     $this->getBarcodeHeight(),
  231.                     $value
  232.                     $black
  233.                 );
  234.             }
  235.  
  236.             if ($this->_paritypattern[$checkdigit][$idx-1== 1{
  237.                 foreach ($this->_codingmap[$value]['E'as $bar{
  238.                     if ($bar{
  239.                         $writer->imagefilledrectangle(
  240.                             $img,
  241.                             $xpos,
  242.                             0,
  243.                             $xpos $this->getBarcodeWidth(- 1,
  244.                             $this->getBarcodeHeight(),
  245.                             $black
  246.                         );
  247.                     }
  248.                     $xpos += $this->getBarcodeWidth();
  249.                 }
  250.             else {
  251.                 foreach ($this->_codingmap[$value]['O'as $bar{
  252.                     if ($bar{
  253.                         $writer->imagefilledrectangle(
  254.                             $img,
  255.                             $xpos,
  256.                             0,
  257.                             $xpos $this->getBarcodeWidth(- 1,
  258.                             $this->getBarcodeHeight(),
  259.                             $black
  260.                         );
  261.                     }
  262.                     $xpos += $this->getBarcodeWidth();
  263.                 }
  264.             }
  265.         }
  266.         
  267.         // space
  268.         $xpos += $this->getBarcodeWidth();
  269.         
  270.         // Draws the right guard pattern (bar-space-bar-space-bar)
  271.         // bar
  272.         $writer->imagefilledrectangle(
  273.             $img,
  274.             $xpos,
  275.             0,
  276.             $xpos $this->getBarcodeWidth(- 1,
  277.             $barcodelongheight,
  278.             $black
  279.         );
  280.         
  281.         $xpos += $this->getBarcodeWidth();
  282.         // space
  283.         $xpos += $this->getBarcodeWidth();
  284.         // bar
  285.         $writer->imagefilledrectangle(
  286.             $img,
  287.             $xpos,
  288.             0,
  289.             $xpos $this->getBarcodeWidth(- 1,
  290.             $barcodelongheight,
  291.             $black
  292.         );
  293.         
  294.         $xpos += $this->getBarcodeWidth();
  295.         // space
  296.         $xpos += $this->getBarcodeWidth();
  297.         // bar
  298.         $writer->imagefilledrectangle(
  299.             $img,
  300.             $xpos,
  301.             0,
  302.             $xpos $this->getBarcodeWidth(- 1,
  303.             $barcodelongheight,
  304.             $black
  305.         );
  306.         
  307.         $xpos += $this->getBarcodeWidth();
  308.         
  309.         
  310.         // Print Check Digit
  311.         if ($this->showText{
  312.             $writer->imagestring(
  313.                 $img,
  314.                 $fontsize,
  315.                 $xpos + 1,
  316.                 $this->getBarcodeHeight(),
  317.                 $checkdigit,
  318.                 $black
  319.             );
  320.         }
  321.         
  322.         return $img;
  323.     }
  324.  
  325. // class

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