Zend - The PHP Company




Graphics

Add Code


Barcode creator  

Type: application
Added by: ritt9322
Entered: 25/02/2004
Last modified: 02/12/2003
Rating: - (fewer than 3 votes)
Views: 7331
When accessed via the tag: Where $barcode is the string with your information to be barcoded, it returns a pnd image of the barcode. It creates a barcode39 type barcode.


<?php
#Mike Ritthaler 
#mritthaler@upress.unm.edu
#02/25/2004
# University of New Mexico Press
# www.unmpress.com
# Barcoder
# Version 1

if (isset($barcode39))
{
    unset(
$barcode39);
}
$barcode39['0']="nnnwwnwnn";
$barcode39['1']="wnnwnnnnw";
$barcode39['2']="nnwwnnnnw";
$barcode39['3']="wnwwnnnnn";
$barcode39['4']="nnnwwnnnw";
$barcode39['5']="wnnwwnnnn";
$barcode39['6']="nnwwwnnnn";
$barcode39['7']="nnnwnnwnw";
$barcode39['8']="wnnwnnwnn";
$barcode39['9']="nnwwnnwnn";
$barcode39['A']="wnnnnwnnw";
$barcode39['B']="nnwnnwnnw";
$barcode39['C']="wnwnnwnnn";
$barcode39['D']="nnnnwwnnw";
$barcode39['E']="wnnnwwnnn";
$barcode39['F']="nnnwwnwnn";
$barcode39['G']="nnnnnwwnw";
$barcode39['H']="wnnnnwwnn";
$barcode39['I']="nnwnnwwnn";
$barcode39['J']="nnnnwwwnn";
$barcode39['K']="wnnnnnnww";
$barcode39['L']="nnwnnnnww";
$barcode39['M']="wnwnnnnwn";
$barcode39['N']="nnnnwnnww";
$barcode39['O']="wnnnwnnwn";
$barcode39['P']="nnwnwnnwn";
$barcode39['Q']="nnnnnnwww";
$barcode39['R']="wnnnnnwwn";
$barcode39['S']="nnwnnnwwn";
$barcode39['T']="nnnnwnwwn";
$barcode39['U']="wwnnnnnnw";
$barcode39['V']="nwwnnnnnw";
$barcode39['W']="wwwnnnnnn";
$barcode39['X']="nwnnwnnnw";
$barcode39['Y']="wwnnwnnnn";
$barcode39['Z']="nwwnwnnnn";
$barcode39['-']="nwnnnnwnw";
$barcode39['.']="wwnnnnwnn";
$barcode39[' ']="nwwnnnwnn";
$barcode39['*']="nwnnwnwnn";
$barcode39['$']="nwnwnwnnn";
$barcode39['/']="nwnwnnnwn";
$barcode39['+']="nwnnnwnwn";
$barcode39['%']="nnnwnwnwn";


#given a string of information, returns the information as a png image
#which is a barcode.   Only symbols which exist in the $barcode 39 array
#can be rendered.  Barcodes are read as a series of narrow and wide black and
#white bars, with a quite zone (ALL white) on each end.  The most important thing
#is the relative size between the wide and narrow bars.
#This barcode can take up a lot of space!

function generate_barcode($info)
{
    global 
$barcode39;
    
$im;
    
//quiet zone=10x
    //$L=($C+2)*(3*$N+6)*$X+($C+1)*$I;
    //$L = length of symbol, not counting quiet zone in mils.
    //$C = Number of data characters
    //$X = $X dimension, width of the smallest unit in mills.
    //$N = Wide to narrow multiple (Use 3.0 if your code had a 3 to 1 ratio)
    //$I= intercharacter gap.  max I=5.3 X for X<10  if X>10 I=(>3X,53)

    
$info="*".$info."*";
    
$X=1;
    
$N=3;
    
$I=5.3*$X;
    
$C=strlen($info);
    
$L=($C+2)*(3*$N+6)*$X+($C+1)*$I+20*$X;  //last bit for the quiet zones
    
$infostrtoupper ($info);

    
$im = @ImageCreate ($L,50)
    or die (
"Cannot Initialize new GD image stream");
    
$white=ImageColorAllocate ($im255255255);//back ground color
    
$black=ImageColorAllocate ($im000);//bar colors
    //after info has been uppercased, procedure moves through each letter
    //matches it to the corresponding 'wnwnwn' entry,
    //then draws a series of wide and narrow bands 
    //which are the barcode.
    
$position=10*$X;
    for(
$i=0;$i<$C;$i++) //loop through the string
    
{
        
$ic=$info[$i];
        
$bs=$barcode39[$ic];
        
$bar=true;
        for(
$j=0;$j<strlen($bs);$j++) //loop through the matching entry to draw
        
{
            if(
$bar)//drawing a bar
            
{
                if(
$bs[$j]=='w')
                {
                    
imagefilledrectangle ($im$position0$position+$N*$X-150$black);
                    
$position+=$N*$X;
                }
                if(
$bs[$j]=='n')
                {
                    
imagefilledrectangle ($im$position0$position+$X-150$black);
                    
$position+=$X;
                }
            }

            else
//not drawing a bar
            
{
                if(
$bs[$j]=='w'$position+=$N*$X;
                if(
$bs[$j]=='n'$position+=$X;
            }
            if(
$bar$bar=false;  //turn bars off and on
            
else $bar=true;
        }
//end of barcode characters
    
        
$position+=$X;
    }
//end of info
    
return $im;
}

#grab the security
$barcode="NONE";


#only on of the following sections should
#be enabled


#section for PHP 4.1 and beyond
/*if (isset($_POST['info']))
{
    $barcode=$_POST['info'];
}
if (isset($_GET['info']) )
{
    $barcode=$_GET['info'];
}*/

#section for PHP 3->php 4.1
if (isset($HTTP_POST_VARS['info']))
{
    
$barcode=$HTTP_POST_VARS['info'];
}
if (isset(
$HTTP_GET_VARS['info']) )
{
    
$barcode=$HTTP_GET_VARS['info'];
}


$im=generate_barcode($barcode);
Header("Content-type: image/png"); 
ImagePng($im)
?>


Usage Example




Rate This Script





Search



This Category All Categories