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.
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
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);
The ‘Image_Barcode’ library supports the following barcode types:
- Code 39
- Code 128
- EAN 13
- INT 25
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).
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.