Using barcodes in your web application


Posted in: data, libraries, php | Save to del.icio.us | Twit This! 2 Sep 2009

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




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts

  • No Related Post


8 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.

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More


  • Users Online

    • 10 Users Online
    • 9 Guests, 1 Bot
  • My status