Using barcodes in your web application


Brought into the mainstream by supermarket checkout systems, bar codes have become a ubiquitous element in our daily lives. Rarely will one come across any product that doesn’t have a barcode. The idea of a barcode originated in 1932 from the thesis of Wallace Flint at Harvard.

Varieties of barcodes

Barcodes bascially come in two main type: linear (1-dimensional) and Matrix (2-dimensional) with each offering a variety of formats, depending on your application purpose. The barcodes we usually see on books and other standard products are of the linear type. These only encode data in one dimension, from left to right. The following shows a typical UPC barcode.

bar2

Matrix or 2-dimensional barcode encode information in both directions (x-y axis). So they are able to store more information in the same space then linear barcodes. There are many type of matrix codes, the one shown below is that of QR-Code or Quick Response code. QR-Codes are mostly used for access with cell phones and have become a de-facto standard for Japanese cell phones. Other type of matrix codes include ShotCode, Semacode. The QR-code below shows the string ‘codediesel.com’ encoded.

qr_code

In this post we will see how to generate linear barcodes using a pear library.

Installation

We will be using the ‘Image_Barcode’ Pear library to generate linear barcodes. You can download the library manually from here, or use the pear installer.

pear install Image_Barcode-1.1.0

Sample Code

A sample code for generating a CODE128 barcode is shown below.

<?php
 
   require_once 'Barcode.php';
   /* Data that will be encoded in the bar code */
   $bar_code_data = "TRSD5656";
 
   /* The third parameter can accept any from the following,
    * jpg, png and gif.
    */
   Image_Barcode::draw($bar_code_data, 'code128', 'png');
 
 ?>

By default, the image generated by the barcode is directly output to the browser. If you do not want this, you can pass ‘false’ as the fourth parameter and it will return the GD image resource object; allowing you to do further things with it.

Image_Barcode::draw($bar_code_data, 'code128', 'png', false);

Supported Types

The ‘Image_Barcode’ library supports the following barcode types:

  • Code 39
  • Code 128
  • EAN 13
  • INT 25
  • PostNet
  • UPCA

Some sample generated barcodes

Some barcodes generated with the above library are shown below. The last one is the United States Postal Service format.

barcode_example

Google charts and QR-Codes

Before closing this post we will take a look at generating QR-Codes using Google charts. The following line for example generates the QR-Code shown below. The ‘chl’ parameter specifies the data that will be encoded (‘www.codediesel.com’ here).

http://chart.apis.google.com/chart
?cht=qr&chs=150x150&chl=www.codediesel.com

Which you can embed in your html as below:

<img src="http://chart.apis.google.com/chart
?cht=qr&chs=150x150&chl=www.codediesel.com" />

google_chart_qrcode

Testing bar codes

Ok, now you have created all the barcodes you want, but the problem is you do not have a barcode reader handy; no sweat, Qualitysoft.de offers a free barcode testing application for checking a plethora of barcode formats.


Additional Resources

1. http://qrcode.kaywa.com/
2. http://www.denso-wave.com/qrcode/index-e.html
3. http://en.wikipedia.org/wiki/Barcode

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

17 Responses

1

Sameer Borate’s Blog: Using barcodes in your web application | Webs Developer

September 7th, 2009 at 3:00 pm

[...] his Code Diesel blog Sameer takes a look at creating and using barcodes in your PHP applications: Brought into the mainstream by supermarket checkout systems, bar codes [...]

2

io

September 8th, 2009 at 12:48 am

What is the use with php ?

sameer

September 8th, 2009 at 3:30 am

Well if you selling your own products, you can use it to print UPC bar codes or if you are selling some products from a e-commerce application, you can generate a barcode for each delivery from your end, print it and attach it to the delivery package for tracking. The main point is that being a standard the barcode information you create can be read by any scanner.

4

deep

October 26th, 2009 at 11:50 pm

Thanks for the tutorial.I have install the pear Image_Barcode-1.1.0 package.But i got an error “Warning: require_once(Barcode.php) [function.require-once]: failed to open stream”

sameer

October 27th, 2009 at 12:27 am

Try the following path:

require_once ‘Image/Barcode.php’;

6

Das

November 4th, 2009 at 10:46 pm

Nice tutorial Sameer. Keep going

7

Divyesh Karelia

February 9th, 2010 at 3:21 am

nice tutorial for generating a barcode.

i want to create high resolution image of that barcode. is it possible using this library or other trick for ….

sameer

February 9th, 2010 at 3:44 am

You will need to make some changes in the Barcode class files for the appropriate barcode type. So if you are using the ‘code128′ barcode type, you can change the height and width in the ‘code128.php’ file. But note that the sizes in the code are calculated, manually changing the sizes can render the barcode unreadable.

9

Tom

March 18th, 2010 at 10:55 am

Nifty tutorial, thanks! I have the Image_Barcode Pear library installed, but I have no idea where to find the ” require_once ‘Barcode.php’; ” referenced in your sample.

Did I miss something?

10

Tom

March 18th, 2010 at 11:37 am

Ok I found my problem related to path in both the Pear Library location on the server and the include reference in the CODE128 file in the Barcode sub-directory.

That’s all working. I’ve verified that I have an updated GD library installed and working.

I’m getting the following ASCII garbage where the code128 barcode should be…

‰PNG  ��� IHDR���7���<���øÒ°Æ���PLTE���ÿÿÿ¥ÙŸÝ���IDAT•cøºr²Åò £4i0 9 �~7…m “����IEND®B`‚

Any suggestions?

sameer

March 18th, 2010 at 9:01 pm

The barcode class accepts the following 3 formats: png, gif, jpg. Try using the gif format in the draw function.

Image_Barcode::draw($bar_code_data, ‘code128′, ‘gif’);

The ASCII garbage is actually the png graphic, which for some reason your browser is not able to understand. The php barcode sends the image generated with the ‘Content-Type: image/png’ headers, so that the browser can recognize it as a png document and render it as such. If the browser is unable to recognize the format then it will display the data in the raw format as above.

12

NG

August 15th, 2012 at 6:09 am

hi sameer, interesting tutorial.

how would i go about having the barcode / qr code displayed in an external document like a pdf document?

sameer

August 15th, 2012 at 6:43 am

You can copy the generated PNG image to the external document.

14

andrew

October 26th, 2012 at 4:09 am

Thank you for the post. I’d like to incorporate multiple barcodes into my php page but this seems to generate a single barcode and then stops. Any guidance?

15

Md. Anisur Rahman

May 14th, 2013 at 2:53 am

Hi Sameer,

Thank you very much for your post. But I want to generate multiple barcode on my php page under a html table row/columns etc. Please guide me. I am looking forward for your earliest reply.

Thanking you
Anis

16

Md. Anisur Rahman

May 14th, 2013 at 7:12 am

Sameer,

Thank you very much for ur post. Please tell me how can I print multiple barcodes using this library. Pls reply any one asap.

Thanking you
Anis

sameer

May 14th, 2013 at 8:51 pm

Rather than sending the output to the browser directly, pass ‘false’ as the fourth parameter to the draw function, which will then return a GD object which you can then save as an image. For printing multiple barcodes iterate the ‘draw’ function in a loop and save the respective images.

Your thoughts