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

Source for file Code39.php

Documentation is available at Code39.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Ryan Briones <ryanbriones@webxdesign.org>                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Last Modified: 2003/12/09 - Ryan Briones
  21.  
  22. /**
  23.  *
  24.  * Image_Barcode_Code39 creates Code 3 of 9 ( Code39 ) barcode images. It's
  25.  * implementation borrows heavily for the perl module GD::Barcode::Code39
  26.  *
  27.  * @author Ryan Briones <ryanbriones@webxdesign.org>
  28.  * @todo add a function to print images directly to browser not using the draw() function
  29.  *
  30.  */
  31.  
  32.  
  33. require_once "PEAR.php";
  34. require_once "Image/Barcode.php";
  35.  
  36. /*
  37.  *
  38.  * the function str_split() has been including, not part of the class for
  39.  * it's ability to emulate Perl's split( //, $text ). This function was
  40.  * stolen from the PHP function documentation comments on PHP's str_split()
  41.  * which is to be included in PHP5.
  42.  *
  43.  * @param string $str
  44.  * @param int number of characters you wish to split on
  45.  * @return array|false Returns an array or false when $num is less than 1
  46.  */
  47. function str_split$str$num '1' {
  48.    if($num < 1return FALSE;
  49.    $arr = array();
  50.    for ($j = 0; $j strlen($str)$j$j+$num{
  51.        $arr[substr($str,$j,$num);
  52.    }
  53.    return $arr;
  54. }
  55.  
  56. class Image_Barcode_Code39 extends Image_Barcode {
  57.     /**
  58.      * Barcode type
  59.      * @var string 
  60.      */
  61.     var $_type 'Code39';
  62.     
  63.     /**
  64.      * Barcode height
  65.      *
  66.      * @var integer 
  67.      */
  68.     var $_barcodeheight = 50;
  69.     
  70.     /**
  71.      * Bar thin width
  72.      *
  73.      * @var integer 
  74.      */
  75.     var $_barthinwidth = 1;
  76.     
  77.     /**
  78.      * Bar thick width
  79.      *
  80.      * @var integer 
  81.      */
  82.     var $_barthickwidth = 3;
  83.     
  84.     /**
  85.      * Coding map
  86.      * @var array 
  87.      */
  88.     var $_coding_map = array(
  89.         '0' => '000110100',
  90.         '1' => '100100001',
  91.         '2' => '001100001',
  92.         '3' => '101100000',
  93.         '4' => '000110001',
  94.         '5' => '100110000',
  95.         '6' => '001110000',
  96.         '7' => '000100101',
  97.         '8' => '100100100',
  98.         '9' => '001100100',
  99.         'A' => '100001001',
  100.         'B' => '001001001',
  101.         'C' => '101001000',
  102.         'D' => '000011001',
  103.         'E' => '100011000',
  104.         'F' => '001011000',
  105.         'G' => '000001101',
  106.         'H' => '100001100',
  107.         'I' => '001001100',
  108.         'J' => '000011100',
  109.         'K' => '100000011',
  110.         'L' => '001000011',
  111.         'M' => '101000010',
  112.         'N' => '000010011',
  113.         'O' => '100010010',
  114.         'P' => '001010010',
  115.         'Q' => '000000111',
  116.         'R' => '100000110',
  117.         'S' => '001000110',
  118.         'T' => '000010110',
  119.         'U' => '110000001',
  120.         'V' => '011000001',
  121.         'W' => '111000000',
  122.         'X' => '010010001',
  123.         'Y' => '110010000',
  124.         'Z' => '011010000',
  125.         '-' => '010000101',
  126.         '*' => '010010100',
  127.         '+' => '010001010',
  128.         '$' => '010101000',
  129.         '%' => '000101010',
  130.         '/' => '010100010',
  131.         '.' => '110000100',
  132.         ' ' => '011000100'
  133.     );
  134.         
  135.         /*
  136.          *
  137.          * constructor
  138.          *
  139.          * @param string $text
  140.          * @param int $wThin Width of the thin lines on the barcode
  141.          * @param int $wThick Width of the thick lines on the barcode
  142.          *
  143.          */
  144.     function Image_Barcode_Code39$text ''$wThin = 0$wThick = 0 {
  145.         
  146.         // Check $text for invalid characters
  147.         if $this->checkInvalid$text ) ) {
  148.             return false;
  149.         }
  150.  
  151.         $this->text $text;
  152.         if $wThin > 0 $this->_barthinwidth $wThin;
  153.         if $wThick > 0 $this->_barthickwidth $wThick;
  154.  
  155.         return true;
  156.     }
  157.         
  158.         /*
  159.          *
  160.          * make an image resource using the GD image library
  161.          *
  162.          * @param bool $noText Set to true if you'd like your barcode to be sans text
  163.          * @param int $bHeight height of the barcode image including text
  164.          * @return resource The Barcode Image (TM)
  165.          *
  166.          */
  167.     function plot$noText = false$bHeight = 0 {
  168.        // add start and stop * characters
  169.        $final_text "*" $this->text "*";
  170.              
  171.              if $bHeight > 0 {
  172.                  $this->_barcodeheight $bHeight;
  173.              }
  174.        
  175.        $barcode '';
  176.        foreach str_split$final_text as $character {
  177.            $barcode .= $this->_dumpCode$this->_coding_map[$character'0' );
  178.        }
  179.         
  180.        $barcode_len strlen$barcode );
  181.  
  182.        // Create GD image object
  183.        $img imagecreate$barcode_len$this->_barcodeheight );
  184.        
  185.        // Allocate black and white colors to the image
  186.        $black imagecolorallocate$img00);
  187.        $white imagecolorallocate$img255255255 );
  188.        $font_height $noText ? 0 : imagefontheight"gdFontSmall" ) );
  189.        $font_width imagefontwidth"gdFontSmall" );
  190.  
  191.        // fill background with white color
  192.        imagefill$img00$white );
  193.  
  194.        // Initialize X position
  195.        $xpos = 0;
  196.        
  197.        // draw barcode bars to image
  198.              if $noText {
  199.                  foreach str_split$barcode as $character_code {
  200.                          if $character_code == 0 {
  201.                                  imageline$img$xpos0$xpos$this->_barcodeheight$white );
  202.                          else {
  203.                                  imageline$img$xpos0$xpos$this->_barcodeheight$black );
  204.                          }
  205.                          
  206.                          $xpos++;
  207.                     }
  208.                 else {
  209.                      foreach str_split$barcode as $character_code {
  210.                              if $character_code == 0 {
  211.                                      imageline$img$xpos0$xpos$this->_barcodeheight $font_height - 1$white );
  212.                              else {
  213.                                      imageline$img$xpos0$xpos$this->_barcodeheight $font_height - 1$black );
  214.                              }
  215.                              
  216.                              $xpos++;
  217.                         }
  218.                         
  219.                         // draw text under barcode
  220.                         imagestring$img"gdFontSmall"
  221.                                 $barcode_len $font_width strlen$this->text ) )/2,
  222.                                 $this->_barcodeheight $font_height,
  223.                                 $this->text,
  224.                                 $black );
  225.                 }
  226.  
  227.         return $img;
  228.     }
  229.  
  230.         /*
  231.          *
  232.          * send image to the browser; for Image_Barcode compaitbility
  233.          *
  234.          * @param string $text
  235.          * @param string $imgtype Image type; accepts jpg, png, and gif, but gif only works if you've payed for licensing
  236.          * @param bool $noText Set to true if you'd like your barcode to be sans text
  237.          * @param int $bHeight height of the barcode image including text
  238.          * @return bool true or die
  239.          *
  240.          */
  241.     function draw$text$imgtype 'png'$noText = false$bHeight = 0 {
  242.                 // Check $text for invalid characters
  243.         if $this->checkInvalid$text ) ) {
  244.             return false;
  245.         }
  246.                 
  247.                 $this->text $text;
  248.         $img $this->plot$noText$bHeight );
  249.  
  250.                 // Send image to browser
  251.                 switch($imgtype{
  252.                                 case 'gif':
  253.                             header("Content-type: image/gif");
  254.                         imagegif($img);
  255.                         imagedestroy($img);
  256.                         break;
  257.                 
  258.                     case 'jpg':
  259.                         header("Content-type: image/jpg");
  260.                         imagejpeg($img);
  261.                         imagedestroy($img);
  262.                     break;
  263.                 
  264.                     default:
  265.                         header("Content-type: image/png");
  266.                         imagepng($img);
  267.                         imagedestroy($img);
  268.                     break;
  269.                 }
  270.                 
  271.                 return true;
  272.     }
  273.  
  274.     /*
  275.      *
  276.      * _dumpCode is a PHP implementation of dumpCode from the Perl module
  277.      * GD::Barcode::Code39. I royally screwed up when trying to do the thing
  278.      * my own way the first time. This way works.
  279.          *
  280.          * @param string $code Code39 barcode code
  281.          * @return string $result barcode line code
  282.      *
  283.      * @access private
  284.      *
  285.      */
  286.     function _dumpCode$code {
  287.         $result '';
  288.         $color = 1;    // 1: Black, 0: White
  289.         
  290.         // if $bit is 1, line is wide; if $bit is 0 line is thin
  291.         foreach str_split$code as $bit {
  292.             $result .= ( ( $bit == 1 str_repeat"$color"$this->_barthickwidth str_repeat"$color"$this->_barthinwidth ) );
  293.             $color ( ( $color == 0 ? 1 : 0 );
  294.         }
  295.         
  296.         return $result;
  297.     }
  298.     
  299.     /*
  300.      *
  301.      * check for invalid characters
  302.      *
  303.      * @param string $text
  304.      * @return bool returns true when invalid characters have been found
  305.      *
  306.      */
  307.     function checkInvalid$text {
  308.         return preg_match"/[^0-9A-Z\-*+\$%\/. ]/"$text );
  309.     }
  310. }
  311.  
  312. ?>

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