Image_Barcode
[ class tree: Image_Barcode ] [ index: Image_Barcode ] [ 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. // | PHP version 4.0                                                      |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2001 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 3.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/3_0.txt.                                  |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Author: Didier FOURNOUT <didier.fournout@nyc.fr>                     |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: $
  21. //
  22. // +----------------------------------------------------------+ 
  23. //   Jeffrey K. Brown  5/22/2005
  24. //   Slightly Modified ean13.php to get upca.php
  25. //   I needed a way to print UPC-A bar codes on a
  26. //   PHP page.  The Image_Barcode class seemed like
  27. //   the best way to do it, so I modified ean13 to
  28. //   print in the UPC-A format.  Checked the bar code
  29. //   tables against some documentation below (no errors)
  30. //   and validated the changes with my trusty cue-cat.
  31. //
  32. //   http://www.indiana.edu/~atmat/units/barcodes/bar_t4.htm
  33. // +----------------------------------------------------------+
  34.  
  35. require_once "PEAR.php";
  36. require_once "Image/Barcode.php";
  37.  
  38. class Image_Barcode_upca extends Image_Barcode
  39. {
  40.     /**
  41.      * Barcode type
  42.      * @var string 
  43.      */
  44.     var $_type 'upca';
  45.  
  46.     /**
  47.      * Barcode height
  48.      *
  49.      * @var integer 
  50.      */
  51.     var $_barcodeheight = 50;
  52.  
  53.     /**
  54.      * Font use to display text
  55.      *
  56.      * @var integer 
  57.      */
  58.     var $_font = 2;  // gd internal small font
  59.  
  60.     /**
  61.      * Bar width
  62.      *
  63.      * @var integer 
  64.      */
  65.     var $_barwidth = 1;
  66.  
  67.  
  68.     /**
  69.      * Number set
  70.      * @var array 
  71.      */
  72.     var $_number_set = array(
  73.            '0' => array(
  74.                     'A' => array(0,0,0,1,1,0,1),
  75.                     'B' => array(0,1,0,0,1,1,1),
  76.                     'C' => array(1,1,1,0,0,1,0)
  77.                         ),
  78.            '1' => array(
  79.                     'A' => array(0,0,1,1,0,0,1),
  80.                     'B' => array(0,1,1,0,0,1,1),
  81.                     'C' => array(1,1,0,0,1,1,0)
  82.                         ),
  83.            '2' => array(
  84.                     'A' => array(0,0,1,0,0,1,1),
  85.                     'B' => array(0,0,1,1,0,1,1),
  86.                     'C' => array(1,1,0,1,1,0,0)
  87.                         ),
  88.            '3' => array(
  89.                     'A' => array(0,1,1,1,1,0,1),
  90.                     'B' => array(0,1,0,0,0,0,1),
  91.                     'C' => array(1,0,0,0,0,1,0)
  92.                         ),
  93.            '4' => array(
  94.                     'A' => array(0,1,0,0,0,1,1),
  95.                     'B' => array(0,0,1,1,1,0,1),
  96.                     'C' => array(1,0,1,1,1,0,0)
  97.                         ),
  98.            '5' => array(
  99.                     'A' => array(0,1,1,0,0,0,1),
  100.                     'B' => array(0,1,1,1,0,0,1),
  101.                     'C' => array(1,0,0,1,1,1,0)
  102.                         ),
  103.            '6' => array(
  104.                     'A' => array(0,1,0,1,1,1,1),
  105.                     'B' => array(0,0,0,0,1,0,1),
  106.                     'C' => array(1,0,1,0,0,0,0)
  107.                         ),
  108.            '7' => array(
  109.                     'A' => array(0,1,1,1,0,1,1),
  110.                     'B' => array(0,0,1,0,0,0,1),
  111.                     'C' => array(1,0,0,0,1,0,0)
  112.                         ),
  113.            '8' => array(
  114.                     'A' => array(0,1,1,0,1,1,1),
  115.                     'B' => array(0,0,0,1,0,0,1),
  116.                     'C' => array(1,0,0,1,0,0,0)
  117.                         ),
  118.            '9' => array(
  119.                     'A' => array(0,0,0,1,0,1,1),
  120.                     'B' => array(0,0,1,0,1,1,1),
  121.                     'C' => array(1,1,1,0,1,0,0)
  122.                         )
  123.         );
  124.  
  125.  
  126.     var $_number_set_left_coding = array(
  127.            '0' => array('A','A','A','A','A','A'),
  128.            '1' => array('A','A','B','A','B','B'),
  129.            '2' => array('A','A','B','B','A','B'),
  130.            '3' => array('A','A','B','B','B','A'),
  131.            '4' => array('A','B','A','A','B','B'),
  132.            '5' => array('A','B','B','A','A','B'),
  133.            '6' => array('A','B','B','B','A','A'),
  134.            '7' => array('A','B','A','B','A','B'),
  135.            '8' => array('A','B','A','B','B','A'),
  136.            '9' => array('A','B','B','A','B','A')
  137.         );
  138.  
  139.  
  140.  
  141.  
  142. //==========================================================================================
  143.  
  144. function draw($text$imgtype 'png')  {
  145.  
  146.         if ( (is_numeric($text)==false|| (strlen($text)!=12) )  {
  147.             $barcodewidth(12 * 7 * $this->_barwidth+ 3 + 5 + 3 + 2 * (imagefontwidth($this->_font)+1);
  148.             $error = 1;
  149.         }
  150.         else {
  151.             // Calculate the barcode width
  152.             $barcodewidth (strlen($text)) (7 * $this->_barwidth)
  153.                 + 3 // left
  154.                 + 5 // center
  155.                 + 3 // right
  156.                 + imagefontwidth($this->_font)+1
  157.                 + imagefontwidth($this->_font)+1   // check digit's padding
  158.                 ;
  159.         
  160.  
  161.         $barcodelongheight = (int) (imagefontheight($this->_font)/2)+$this->_barcodeheight;
  162.  
  163.         // Create the image
  164.         $img = ImageCreate($barcodewidth$barcodelongheightimagefontheight($this->_font)+1);
  165.  
  166.         // Alocate the black and white colors
  167.         $black = ImageColorAllocate($img000);
  168.         $white = ImageColorAllocate($img255255255);
  169.  
  170.         // Fill image with white color
  171.         imagefill($img00$white);
  172.  
  173.         if ($error==1{
  174.             $imgerror = ImageCreate($barcodewidth$barcodelongheight+imagefontheight($this->_font)+1);
  175.             $red = ImageColorAllocate($imgerror25500);
  176.             $black = ImageColorAllocate($imgerror0,0,0);
  177.             imagefill($imgerror,0,0,$red);
  178.  
  179.             imagestring($imgerror$this->_font$barcodewidth/2-(10/2* imagefontwidth($this->_font))$this->_barcodeheight/2"Code Error"$black);
  180.         }
  181.  
  182.         // get the first digit which is the key for creating the first 6 bars
  183.         $key substr($text,0,1);
  184.  
  185.         // Initiate x position
  186.         $xpos = 0;
  187.  
  188.         // print first digit
  189.         imagestring($img$this->_font$xpos$this->_barcodeheight$key$black);
  190.         $xposimagefontwidth($this->_font+ 1;
  191.  
  192.  
  193. //===========================================================================================
  194.  
  195.  
  196.         // Draws the left guard pattern (bar-space-bar)
  197.         // bar
  198.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  199.         $xpos += $this->_barwidth;
  200.         // space
  201.         $xpos += $this->_barwidth;
  202.         // bar
  203.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  204.         $xpos += $this->_barwidth;
  205.  
  206.         $set_array $this->_number_set_left_coding[$key];
  207.  
  208.  
  209. //=========================================================================================
  210.  
  211.  
  212.         foreach ($this->_number_set['0'][$set_array[0]] as $bar{
  213.             if ($bar{
  214.                 imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  215.             }
  216.             $xpos += $this->_barwidth;
  217.         }
  218.  
  219.  
  220. //=========================================================================================
  221.  
  222.         // Draw left $text contents
  223.         for ($idx = 1; $idx < 6; $idx ++{
  224.             $value=substr($text,$idx,1);
  225.             imagestring ($img$this->_font$xpos+1$this->_barcodeheight$value$black);
  226.  
  227.             //foreach ($this->_number_set[$value][$set_array[$idx-1]] as $bar) {
  228.  
  229.             foreach ($this->_number_set[$value][$set_array[$idx]] as $bar{
  230.                 if ($bar{
  231.                     imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$this->_barcodeheight$black);
  232.                 }
  233.                 $xpos += $this->_barwidth;
  234.             }
  235.         }
  236.  
  237. //=========================================================================================
  238.  
  239.         // Draws the center pattern (space-bar-space-bar-space)
  240.         // space
  241.         $xpos += $this->_barwidth;
  242.         // bar
  243.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  244.         $xpos += $this->_barwidth;
  245.         // space
  246.         $xpos += $this->_barwidth;
  247.         // bar
  248.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  249.         $xpos += $this->_barwidth;
  250.         // space
  251.         $xpos += $this->_barwidth;
  252.  
  253. //====================================================================================
  254.  
  255.         // Draw right $text contents
  256.         for ($idx = 6; $idx < 11; $idx ++{
  257.             $value=substr($text,$idx,1);
  258.             imagestring ($img$this->_font$xpos+1$this->_barcodeheight$value$black);
  259.             foreach ($this->_number_set[$value]['C'as $bar{
  260.                 if ($bar{
  261.                     imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$this->_barcodeheight$black);
  262.                 }
  263.                 $xpos += $this->_barwidth;
  264.             }
  265.         }
  266.  
  267.  
  268. //======================================================================================
  269.  
  270.         $value substr($text,11,1);
  271.         foreach ($this->_number_set[$value]['C'as $bar{
  272.             if ($bar{
  273.                 imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  274.             }
  275.             $xpos += $this->_barwidth;
  276.         }
  277.  
  278.  
  279. // ===================================================================================
  280.  
  281.         // Draws the right guard pattern (bar-space-bar)
  282.         // bar
  283.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  284.         $xpos += $this->_barwidth;
  285.         // space
  286.         $xpos += $this->_barwidth;
  287.         // bar
  288.         imagefilledrectangle($img$xpos0$xpos $this->_barwidth - 1$barcodelongheight$black);
  289.         $xpos += $this->_barwidth;
  290.  
  291. // ====================================================================================
  292.  
  293.         // Print Check Digit
  294.         imagestring($img$this->_font$xpos+1$this->_barcodeheight$value$black);
  295.  
  296.  
  297. // ======================================================================================
  298.  
  299.         // Send image to browser
  300.         switch($imgtype{
  301.  
  302.             case 'gif':
  303.                 header("Content-type: image/gif");
  304.  
  305.                 if ($error==1{
  306.                     imagegif($imgerror);
  307.                     imagedestroy($imgerror);
  308.                 }
  309.                 else {
  310.                     imagegif($img);
  311.                     imagedestroy($img);
  312.                 }
  313.             break;
  314.  
  315.             case 'jpg':
  316.                 header("Content-type: image/jpg");
  317.                 if ($error==1{
  318.                     imageijpeg($imgerror);
  319.                     imagedestroy($imgerror);
  320.                 }
  321.                 else {
  322.                     imagejpeg($img);
  323.                     imagedestroy($img);
  324.                 }
  325.             break;
  326.  
  327.             default:
  328.                 header("Content-type: image/png");
  329.                 if ($error==1{
  330.                     imagepng($imgerror);
  331.                     imagedestroy($imgerror);
  332.                 }
  333.                 else {
  334.                     imagepng($img);
  335.                     imagedestroy($img);
  336.                 }
  337.             break;
  338.  
  339.         }
  340.  
  341.         return;
  342.  
  343.     // function create
  344.  
  345. // class

Documentation generated on Mon, 11 Mar 2019 14:21:06 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.