#native_company# #native_desc#
#native_cta#

The Dynamic Duo: barcode.php & barcode.cgi

By Dave – flyingmonkeysoftware.com
on June 29, 2001

Version: 0.01

Type: Full Script

Category: Graphics

License: GNU General Public License

Description: initial version for dynamic UPC code image generation using php, perl, a flat file, and GD::Barcode

# ----------------------- barcode.php -----------------------#

<?

/*
    barcode.php
    
Copyright 2001 Dave Turner <[email protected]>, All Rights Reserved.
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.

*/

$script = '/barcode.php';
$imagescript = '/cgi-bin/barcode.cgi';
$flatfile = './barcode.txt';

$fp = fopen ($flatfile, "r");
while (!feof ($fp)) {
    $line = fgets($fp, 4096);
    if(preg_match("/(S*) (S.*)/",$line,$match)) {
        $history .= '<a href="' . $script . '?upc=' . $match[1] . '">' . $match[2] . "</a><br>n";
        if(preg_match("/$match[1]/",$describe)) {
            $done = 1;
        }
    }
}
fclose ($fp);

if(preg_match("/(d+)/",$describe,$m)) {
    
    $upc = $m[1];
    $code = join ('',file ("http://www.upcdatabase.com/item.pl?upc=".$upc));

    if(!$code) {
        $description = "Request failed.";
    } else {

        // wipe out html tags
        $search = array (
                 "'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                 "'<[/!]*?[^<>]*?>'si",  // Strip out html tags
                 "'([rn])[s]+'",  // Strip out white space
                 "'&(quot|#34);'i",  // Replace html entities
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e"  // evaluate as php
                 );
        $replace = array ("",
                  "",
                  "1",
                  """,
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(1)"
                  );
        $code = preg_replace ($search, $replace, $code);

        if(preg_match ("/Description(.*n)/",$code,$match)) {
            $filedescription = $match[1];
            $description = $filedescription . "<br>n";
        } else {
            $description = "no description<br>n";
        }
        if(preg_match ("/Manufacturer(.*n)/",$code,$match)) {
            $description .= $match[1] . "<br>n";
        } else {
            $description .= "no manufacturer<br>n";
        }
        if(preg_match ("/Size/Weight(.*n)/",$code,$match)) {
            $description .= $match[1] . "<br>n";
        } else {
            $description .= "no size/weight<br>n";
        }

        if(preg_match ("/w+/",$filedescription) && !$done) {
            // save it for later!
            $fp = fopen ($flatfile, "ab");
            fwrite ($fp, "$upc $filedescription");
            fclose ($fp);
        }

    }
} else {
    $description = "<a href='$script?describe=$upc'>What the hell is it?</a>";
}

?>
<HTML>
<HEAD>
<TITLE>UPC Type A, E Barcodes</TITLE>
<style type="text/css">
<!--
body {font-face:arial,helvetica,sans-serif;color:#FFFFFF;background-color:#666666;}
td {font-face:arial,helvetica,sans-serif;color:black;}
li {font-size:11pt;font-face:arial,helvetica,sans-serif;color:#000099;}
-->
</style>
</HEAD>
<BODY
  BGCOLOR="#666666"
  TEXT="#000000"
  LINK="#0000FF"
  VLINK="#000080"
  ALINK="#FF0000"><center><h3>This is <?

 echo get_current_user().' on '.getenv(SERVER_NAME) . " with PHP" . phpversion(); 

?></h3><br><?

    if (ImageTypes()) {
        if (IMG_PNG) {
            echo "IMG_PNG Support is enabled<br>";
        }
        if (IMG_GIF) {
            echo "IMG_GIF Support is enabled<br>";
        }
        if (IMG_JPG) {
            echo "IMG_JPG Support is enabled<br>";
        }
        if (IMG_WBMP) {
            echo "IMG_WBMP Support is enabled<br>";
        }
    }


?><hr width=50% size=1 noshade><br><? echo php_logo_guid (); ?><table 
  cellspacing=1 
  cellpadding=0 
  bgcolor=#000000 
  border=0 
  width=350><?
  
    if(!preg_match("/d/",$upc)) {
        $upc = '037900991053';
    }
    if(preg_match("/off/",$text)) {
        $textoff = ' checked';
    } else {
        $texton = ' checked';
    }

?><tr
  bgcolor=#E0E0E0><td 
  align=center
  valign=middle><img src='<? echo $imagescript; ?>?upc=<? echo $upc; ?>&text=<? echo $text; ?>&size=<? echo $size; ?>' 
  vspace=10></td><td 
  align=center 
  valign=middle><? echo $description; ?></td></tr><tr
  bgcolor=#ffffff><td colspan=2><table 
  cellspacing=0 
  cellpadding=10 
  border=0 
  width=100%><tr><td align=center><form>UPC: <input 
  type=text 
  width=12 
  name=upc 
  value=<?

    echo "$upc";

    if($size == 100) {
        $hundredselect = ' selected';
        $eightyselect = '';
        $sixtyselect = '';
        $fortyselect = '';
    } elseif($size == 80) {
        $hundredselect = '';
        $eightyselect = ' selected';
        $sixtyselect = '';
        $fortyselect = '';
    } elseif($size == 40) {
        $hundredselect = '';
        $eightyselect = '';
        $sixtyselect = '';
        $fortyselect = ' selected';
    } else {
        $hundredselect = '';
        $eightyselect = '';
        $sixtyselect = ' selected';
        $fortyselect = '';
    }

?>> <input 
  type=submit 
  value=' Draw BarCode '><br>Text: <input 
  type='radio' 
  name='text'
  value='on'<? echo $texton; ?>>ON <input 
  type='radio' 
  name='text'
  value='off'<? echo $textoff; ?>>OFF &nbsp;&nbsp; Height: <select
  name='size'><option 
  value='40'<? echo $fortyselect; ?>>  40 </option><option 
  value='60'<? echo $sixtyselect; ?>>  60 </option><option
  value='80'<? echo $eightyselect; ?>>  80 </option><option
  value='100'<? echo $hundredselect; ?>> 100 </option></select></form> <?
            
    echo $history;

?></td></tr></table></td></tr></table>
  </center>
<br>
</BODY>
</HTML>


# ----------------------- barcode.cgi -----------------------#

#!d:/perl/bin/perl.exe

=head1

barcode.cgi

 image output script support for UPCA and UPCB bar codes

 12 digits for a normal Type A UPC code
   (typical US retail items)
 8 digits for a Type-E (zero-supressed) UPC code
   (a pack of smokes)

Usage:

 /cgi-bin/barcode.cgi?upc=080808&text=on&size=60

=cut

use CGI 'param';


my($oGdBar,$oGdErr);
my $upc = param('upc');
my $text = param('text') eq 'off' ? 1 : 0;
my $size = param('size') =~ /(100|80|40)/ ? $1 : 60;

binmode STDOUT;
print "Content-Type: image/pngnn";


if(&validupcAcode($upc)) {

    require GD::Barcode::UPCA;
    $oGdBar = GD::Barcode::UPCA->new($upc);
    $oGdErr = $GD::Barcode::UPCA::errStr unless($oGdBar);
    
} else {

    require GD::Barcode::UPCE;
    $oGdBar = GD::Barcode::UPCE->new($upc);
    $oGdErr = $GD::Barcode::UPCE::errStr unless($oGdBar);

}
if($oGdBar) {
    
    print $oGdBar->plot(Height => $size, NoText => $text)->png;

} else {

    my $im = new GD::Image(100,$size);
    my $white = $im->colorAllocate(255,255,255);
    my $black = $im->colorAllocate(66,66,66);
    my $red = $im->colorAllocate(255,0,0);
    $im->rectangle(0,0,99,$size-1,$black);
    $im->fill(75,50,$white);
    if($^O =~ /win/i) {
        my @bounds = GD::Image->stringTTF($red,"c:windowsfontsarial.ttf",12,0,0,0,$oGdErr)
            or die $@;
        my($x,$y) = ((100 - $bounds[2]) / 2,($size - $bounds[3]) / 2);
        $im->stringTTF($red,"c:windowsfontsarial.ttf",12,0,$x,$y,$oGdErr)
            or die $@;
    } else {
        my $y = $size / 2 + 5;
        $im->string(gdSmallFont,10,$y,'Invalid Code',$red);
    }
    print $im->png;

}

sub validupcAcode {
    my($upc_code) = @_;
    my($numsys,$mfrcode,$prodcode,$checkdigit) = $upc_code =~ /(d)(d{5})(d{5})(d)/ ?
        ($1,$2,$3,$4) : return undef;
    $mfrcode =~ /(d)(d)(d)(d)(d)/;
    my($a,$b) = ($2+$4,$1+$3+$5);
    $prodcode =~ /(d)(d)(d)(d)(d)/;
    $a += $1+$3+$5;
    $b += $2+$4;
    my $checkmatch = $a*3+$b;
    $checkmatch =~ s/d+(d)/$1/;
    return 1 if $checkdigit == abs($checkmatch - 10);# match!
    return undef;# sucks!
}

=head1

COPYRIGHT

Copyright 2001 Dave Turner <[email protected]>, All Rights Reserved.

You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.

=cut