CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Alternative Working Concept of Using Bar codes

 



ogit2
Novice

Mar 27, 2018, 4:31 AM

Post #1 of 1 (8015 views)
Alternative Working Concept of Using Bar codes Can't Post

Hi

Not an expert by any standards, but some code we use using alternative method. The experts will easily be able to improve so please take this as working concept.


Code
#!C:\Perl\bin\perl.exe 
use strict;
use warnings;
use File::Copy;
use GD;
use GD::Text;
use GD::Text::Align;

my $fontp="FREE3OF9.TTF"; # This is a free barcode font.
my $tempfil = "$$\_file\.png";
my $string = "*BARCODE_STRING*";
my $file = "OriginalGraphicsFile.png";

#START: Create Barcode, note you can use text also with a different font as well as barcode
my $image = new GD::Image( $x1, 160 );
my $black = $image->colorAllocate( 255, 255, 255 );
my $white = $image->colorAllocate( 0, 0, 0 );
my $text = new GD::Text::Align( $image, font => gdLargeFont, text => "$string", color => $white, valign => "top", halign => "center",);
$text->set_font($fontp, 50) or die $text->error;

#START: Open up existing image file, get dimensions and do calculations for positioning/size of bar code
my $im1 = GD::Image->new( $file ) or die;
my( $x1, $y1 ) = $im1->getBounds();
my $y2 = $y1 + 160;
my $td = $x1 / 2;
#END: Open up existing image file, get dimensions and do calculations for positioning/size of bar code

# the order of these two lines is important
$image->filledRectangle( 15, 15, 150, 150, $black );
$text->draw( $td , 50 );
#END : Create Barcode

#START: Create Temporary Barcode File
open( IMAGE, ">$tempfil") || die "Couldn't open file: $!\n";
binmode( IMAGE );
print IMAGE $image->png();
close IMAGE;
#END : Create Temporary Barcode File

#START: Setup final output file and temporary barcode file in memory
my $im3 = GD::Image->new( $x1, $y2, 1 );
my $im2 = GD::Image->new( $tempfil ) or die;
#END: Setup final output file and temporary barcode file in memory

#START: Join images them together (bar code and actual)
$im3->copy( $im1, 0, 160, 0, 0, $x1, $y1 );
$im3->copy( $im2, 0, 0, 0, 0, $x1, 160 );
#END : Join images them together (bar code and actual)

#START: Delete temporary and old image files
unlink($tempfil);
unlink($file);
#END : Delete temporary and old image files

#START: Create new image file, call it the same as original image file
open( IMAGE, ">$file") || die "Couldn't open file: $!\n";
binmode( IMAGE );
print IMAGE $im3->png;
close IMAGE;
#END : Create new image file, call it the same as original image file


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives