Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate Barcode using PHP and Display it as an Image on the same page [duplicate]

I want to Generate Bar code (any type) using PHP

I am having a variable where i store a code

<?php
    $code= 'f5c9b918c5';
?>

so just i want to generate a barcode of this and echo the barcode image where i want ..... please help

like image 815
sudo Avatar asked Apr 15 '13 18:04

sudo


People also ask

Can anyone generate a barcode?

Step #1: Know If You Really Need To Create A Barcode System Meanwhile, your startup may have 2, 3 or just 10 employees but still, you may need a system that can automate tracking of items in stock. For internal operations, you can start cheaply with just a barcode generator, printer, and scanner.


1 Answers

There is a library for this BarCode PHP. You just need to include a few files:

require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

You can generate many types of barcodes, namely 1D or 2D. Add the required library:

require_once('class/BCGcode39.barcode.php');

Generate the colours:

// The arguments are R, G, and B for color.
$colorFront = new BCGColor(0, 0, 0);
$colorBack = new BCGColor(255, 255, 255);

After you have added all the codes, you will get this way:


(source: barcodebakery.com)

Example

Since several have asked for an example here is what I was able to do to get it done

require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

require_once('class/BCGcode128.barcode.php');

header('Content-Type: image/png');

$color_white = new BCGColor(255, 255, 255);

$code = new BCGcode128();
$code->parse('HELLO');

$drawing = new BCGDrawing('', $color_white);
$drawing->setBarcode($code);

$drawing->draw();
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

If you want to actually create the image file so you can save it then change

$drawing = new BCGDrawing('', $color_white);

to

$drawing = new BCGDrawing('image.png', $color_white);
like image 144
Praveen Kumar Purushothaman Avatar answered Oct 17 '22 05:10

Praveen Kumar Purushothaman