Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Barcode PNGs from DB

by Mr. Muskrat (Canon)
on Jun 03, 2002 at 19:02 UTC ( [id://171306]=CUFP: print w/replies, xml ) Need Help??

This will create PNG files for all of the UPCA UPCs that are stored in a database. This is a quick hack (maybe five minutes of coding) and in no way finished software. Next I will store the generated PNG file in the database as well. I've left some commented code in place for reference.
#!/usr/bin/perl use strict; use warnings; use DBI; use GD::Barcode; my $db = "database"; my $db_engine = "mysql"; my $host = "localhost"; my $user = "username"; my $password = "password"; my $dbh = DBI->connect("DBI:$db_engine:$db:$host",$user, $password,{ P +rintError => 0}) || die $DBI::errstr; GetUPCs($dbh); my $rc = $dbh->disconnect; exit; sub GetUPCs { my $dbh = shift; my $table = "items"; #my $select = "item_id, status, description, upc"; my $select = + "upc"; # get whatever info you store on the item my $sth; # The length check is for our specific setup. # Our accounting software cannot handle UPCs with more than 10 digits. + (Go figure) # Someone here has stored some with only 4 and others with 5 digits. +(Waiting on confirmation? Dunno) # However, I have stored some in the database with 11 digits. $sth = $dbh->prepare("SELECT $select FROM $table WHERE upc <> '' A +ND length(upc) >= 10 ;"); if (!$sth) { die "Error:" . $dbh->errstr . "\n"; } if (!$sth->execute) { die "Error:" . $sth->errstr . "\n"; } my $row_ref; while($row_ref = $sth->fetchrow_arrayref) { #print "Item #: $row_ref->[0]\n"; #print "Status: $row_ref->[1]\n"; #print "Description: $row_ref->[2]\n"; #print "UPC: $row_ref->[3]\n"; print "UPC: $row_ref->[0]\n"; print "\n"; #CreateBarCodes('UPCA', $row_ref->[3]); CreateBarCodes('UPCA', $row_ref->[0]); } my $rv = $sth->finish; } sub CreateBarCodes { my ($type, $upc) = @_; my $oGdBar; my $sPtr; $upc = "0".$upc if (length($upc) == 10); # if < 11 digits, +add a 0 to the front chop $upc if (length($upc) == 12); # if > 11 digits, remove the + check digit $oGdBar = GD::Barcode->new($type, $upc); die $GD::Barcode::errStr unless($oGdBar); #Invalid Length open(IMG, ">./png/$upc.png") or die $!; binmode(IMG); print IMG $oGdBar->plot->png; close(IMG); #open(IMG, ">./png/small/$upc.png") or die $!; #binmode(IMG); #print IMG $oGdBar->plot(NoText=>1, Height => 20)->png; #close(IMG); *** Thanks jeffa for pointing out my missing '#' *** }

Replies are listed 'Best First'.
Re: Barcode PNGs from DB
by Juerd (Abbot) on Jun 04, 2002 at 06:43 UTC

    (maybe five minutes of coding)

    Either you are a very fast typer (and thinker), or your timing device is broken.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      The secret lies in code reuse. Remember, that is one of the tenets of our faith!
      If you look closely, you will notice that the subroutine is used in my other barcode related snippets. The database code comes from another... This was just a bunch of cut and paste. So I stand behind, "maybe five minutes of coding."

      Who says that programmers can't work in the Marketing Department?
      Or is that who says that Marketing people can't program?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://171306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found