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:
How to put text and barcodes into png file

 



kwatts59
Novice

Feb 8, 2018, 12:50 PM

Post #1 of 2 (2384 views)
How to put text and barcodes into png file Can't Post

Hello everybody.
I am trying to generate a label with some text and a barcode.
I can generate a PNG file with the text but when I try to add the barcode, it does not appear. Below is my code:

Code
use strict; 
use warnings;
use GD;
use Barcode::Code128;
use diagnostics;

my $image = GD::Image->new(700, 700) || die;
my $white = $image->colorAllocate(230,230,230); # background color - Lt Gray
my $black = $image->colorAllocate(0,0,0); # text color - Black

my $x = 20;
my $y = 100;

$image->string(gdMediumBoldFont, $x, $y, "This is a test", $black);
$x = $x + 20;
$y = $y + 20;
$image->string(gdMediumBoldFont, $x, $y, "Below is a barcode", $black);
$x = $x + 20;
$y = $y + 20;


my $barcode = "0243-3061";
my $object = Barcode::Code128->new();
$object->code('A'); # Enforce 128A?


my $png_data = $image->png;
my $barcode_data = $object->png($barcode);
open OUTFILE, ">", "text_to_image.png" || die;
binmode OUTFILE;
print OUTFILE $png_data;
print OUTFILE $barcode_data;
close OUTFILE;

Any help would be appreciated.
Thanks in advance.

P.S. If you comment out the "print OUTFILE $png_data;" the barcode gets written to the output.


(This post was edited by kwatts59 on Feb 8, 2018, 12:52 PM)


BillKSmith
Veteran

Feb 9, 2018, 2:00 PM

Post #2 of 2 (2367 views)
Re: [kwatts59] How to put text and barcodes into png file [In reply to] Can't Post

You are creating two png images and printing them to the same file. You have already discovered that if you comment out the print of the text data, the barcode prints correctly. I suspect that if you comment out the barcode data instead, your text would be printed correctly.

You must create a single GD object which contains the barcode and the text. You then convert that to png and print it.

I do not know exactly how to do that. It appears to me that you should do the barcode first. Create a gd_image rather than a png. Use that instead of $image. Your $png_data would then be the reguired image.

Save the barcode as a GD image. Create the text as a GD image. Copy the barcode image into the text image. Convert the combined image to png and print. (Resulting file attached)


UPDATE: Added code



Code
use strict;  
use warnings;
use GD;
use Barcode::Code128;
use diagnostics;

my $object = Barcode::Code128->new();
$object->code('A'); # Enforce 128A?
my $bar_image = $object->gd_image('0243-3061');
my $image = GD::Image->new(700,700) || die;

my $white = $image->colorAllocate(230,230,230); # background color - Lt Gray
my $black = $image->colorAllocate(0,0,0); # text color - Black
$image->copy($bar_image,60,140,0,0,294,64);

my $x = 20;
my $y = 100;

$image->string(gdMediumBoldFont, $x, $y, "This is a test", $black);
$x = $x + 20;
$y = $y + 20;
$image->string(gdMediumBoldFont, $x, $y, "Below is a barcode", $black);
$x = $x + 20;
$y = $y + 20;

my $png_data = $image->png;
open OUTFILE, ">", 'text_to_image.png' || die;
binmode OUTFILE;
print OUTFILE $png_data;
close OUTFILE;

Good Luck,
Bill

(This post was edited by BillKSmith on Feb 11, 2018, 6:38 AM)
Attachments: text_to_image.png (0.71 KB)

 
 


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

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