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.


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 ‘’ encoded.


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


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.

   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.


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 (‘’ here).

Which you can embed in your html as below:


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, offers a free barcode testing application for checking a plethora of barcode formats.

Additional Resources


19 thoughts to “Using barcodes in your web application”

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

  2. 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”

  3. 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 ….

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

  5. 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?

  6. 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?

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

  8. hi sameer, interesting tutorial.

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

  9. 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?

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

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

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

  13. Not Clear. Please upload all required files and complete bar code inserted php code in a zip format..

Leave a Reply

Your email address will not be published.