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

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.

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" /> |

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
11 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²Åò£4i0 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.