Source for file EAN13.php

Documentation is available at EAN13.php

  1. <?
  2. /**
  3.  * Barcode Generator
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE: This source file is subject to LGPL license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html
  10.  *
  11.  * @author            Eli Fox-Epstein
  12.  * @copyright        Trinity Humanitarian-FOSS Project - http://www.cs.trincoll.edu/hfoss
  13.  * @package        sahana
  14.  * @subpackage    vcs
  15.  * @tutorial
  16.  * @license        http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  17.  */
  18.  
  19. /**
  20.  *    Provides a class that outputs EAN-13 barcodes
  21.  */
  22. class EAN13 {
  23.  
  24.     private $numbers;
  25.     private $encoded;
  26.     private $height;
  27.  
  28.     /**
  29.      * Constructor.
  30.      * 
  31.      * @param $numbers the 12-digit or 13-digit number to turn into a barcode.
  32.      *          If 12 digits are provided, the checksum is automatically generated.
  33.      *          If 13 are provided, the first digit is considered to be the checksum and must be valid.
  34.      * @param $barHeight the height of the bars in pixels
  35.      */
  36.     function EAN13($numbers$barHeight 35{
  37.         $this->height $barHeight
  38.         $this->numbers $numbers;
  39.         if(strlen($this->numbers== 12)
  40.             $this->numbers .= $this->checksum();
  41.         if(strlen($this->numbers!= 13)
  42.             die('Invalid numbers length');
  43.         $this->encoded $this->encode();
  44.     }
  45.  
  46.     /**
  47.      * Generates the checksum of an EAN-13 barcode
  48.      * 
  49.      * @return the check digit
  50.      */
  51.     private function checksum({
  52.         $odd true;
  53.         $checksum 0;
  54.         for($i 12$i>0;$i--){
  55.             if($odd){
  56.                 $odd false;
  57.                 $multiplier 3;
  58.             }else{
  59.                 $odd true;
  60.                 $multiplier 1;
  61.             }
  62.             $checksum += $this->numbers[$i-1$multiplier;
  63.         }
  64.         $checksum 10 $checksum 10;
  65.         $checksum ($checksum == 10$checksum;
  66.         return $checksum;
  67.     }
  68.     
  69.     /**
  70.      *    Returns the binary encoding a digit in L, G, or R format
  71.      * 
  72.      * @param $digit the digit to encode
  73.      * @param $type the type of encoding
  74.      */
  75.     private function getEncoding($digit$type){
  76.         $encoding array(
  77.             => '1110010',
  78.             => '1100110',
  79.             => '1101100',
  80.             => '1000010',
  81.             => '1011100',
  82.             => '1001110',
  83.             => '1010000',
  84.             => '1000100',
  85.             => '1001000',
  86.             => '1110100'
  87.         );
  88.         
  89.         if($type == 'R'return $encoding[$digit];
  90.         if($type == 'L'return strtr($encoding[$digit],'10','01');
  91.         if($type == 'G'return strrev($encoding[$digit]);
  92.         return false;
  93.     }
  94.  
  95.     /**
  96.      * Converts the numbers into a binary sequence.
  97.      * 
  98.      * @return the barcode pattern
  99.      */
  100.     private function encode({
  101.     
  102.         $o $this->numbers{0};
  103.         
  104.         /*
  105.          * The L-G type pattern for the first six digits, based on the check digit
  106.          */
  107.         $pattern array(
  108.             => 'LLLLLL',
  109.             => 'LLGLGG',
  110.             => 'LLGGLG',
  111.             => 'LLGGGL',
  112.             => 'LGLLGG',
  113.             => 'LGGLLG',
  114.             => 'LGGGLL',
  115.             => 'LGLGLG',
  116.             => 'LGLGGL',
  117.             => 'LGGLGL'
  118.         );
  119.         $usePattern $pattern[$o];
  120.         
  121.         /*
  122.          * Start marker
  123.          */
  124.         $o '101';
  125.         
  126.         /*
  127.          * First six
  128.          */
  129.         foreach(range(1,6as $i){
  130.             $o .= $this->getEncoding($this->numbers{$i},$usePattern{$i-1});
  131.         }
  132.  
  133.         /*
  134.          * Middle divider
  135.          */
  136.         $o .= '01010';
  137.         
  138.         /*
  139.          * Last six
  140.          */
  141.         foreach(range(7,12as $i){
  142.             $o .= $this->getEncoding($this->numbers{$i},'R');
  143.         }
  144.         
  145.         /*
  146.          * End marker
  147.          */
  148.         $o .= '101';
  149.         
  150.         
  151.         return $o;
  152.     }
  153.     
  154.     /**
  155.      * Outputs a PNG image of the barcode
  156.      * 
  157.      * @return void 
  158.      */
  159.        function output({
  160.            header('Content-type: image/png');
  161.         header("Cache-Control: no-cache, must-revalidate")// HTTP/1.1
  162.         $img imageCreate(105,$this->height 12);
  163.         $white imageColorAllocate($img255255255);
  164.         $black imageColorAllocate($img000);
  165.  
  166.         $t true;
  167.         $u true;
  168.         foreach(str_split($this->encodedas $key => $digit){
  169.             if($digit){
  170.                 $exp 0;
  171.                 if($key <= || ($key >= 45 && $key <= 49|| $key >= 92)
  172.                     $exp 5;
  173.                 imageFilledRectangle($img$key+100$key+10$this->height+$exp$black);    
  174.             }
  175.         }
  176.         global $global;
  177.         putenv("GDFONTPATH={$global['approot']}/3rd/barcode/");
  178.  
  179.         $fontSize 8;
  180.         $firstSixOffset 15;
  181.         $secondSixOffset 60;
  182.         $fontVertOffset 11;
  183.  
  184.         imagettftext($img$fontSize0.00$this->height+$fontVertOffset$black"VeraMono.ttf"$this->numbers{0});
  185.         imagettftext($img$fontSize0.0$firstSixOffset$this->height+$fontVertOffset$black"VeraMono.ttf"substr($this->numbers,1,6));
  186.         imagettftext($img$fontSize0.0$secondSixOffset$this->height+$fontVertOffset$black"VeraMono.ttf"substr($this->numbers,7,6));
  187.         imagePNG($img);
  188.         imageDestroy($img);
  189.     }
  190. }
  191. ?>

Documentation generated on Fri, 18 Jul 2008 11:07:43 -0400 by phpDocumentor 1.4.1