Add barcodes to your Web apps using PEAR and PHP

One way to tie hardcopy items to backend systems is by providing barcodes through Web applications. Phillip Perkins demonstrates how easy it is to use the PEAR::Image_Barcode class in PHP to dynamically create barcodes for Web applications.

Keep your developer skills sharp by automatically signing up for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.

Barcodes provide a great solution for tying real-world items to associated computer data. An item with a barcode not only helps link associated data, but it also eliminates the need for a data entry operator to key in identifying information and possibly make mistakes.

Providing barcodes through Web applications is one way to tie hardcopy items to backend systems. I'll demonstrate how easy it is to use the PEAR::Image_Barcode class in PHP to dynamically create barcodes for your Web applications.

The backbone to the PEAR::Image_Barcode's ability to produce barcode images is in the GD library, an open source code library for the dynamic creation of images. GD creates PNG, JPEG, and GIF images, among other formats. Since PHP 4.3, the GD library has been available as a bundled version. (For this article, I use PHP 4.3.10 on Windows 2000 with IIS 5.0.) If you haven't already installed PEAR, you will need to do so in order to get this application to work. (For more information on installing PEAR, visit the PEAR Web site.)

First, you need to make sure that you have GD support installed. For my environment, I had to extract the php_gd2.dll library from the zipped Windows binary library to my extensions directory. (The extensions directory is specified in my php.ini file located in my WINNT folder.) Then, I uncommented the GD extension line in my php.ini file: extension=php_gd2.dll.

Then, using the PEAR package installation tool, you need to install the Image_Barcode package. Once you've completed these steps, you should be able to create barcode images.

Now, open the Barcode.php file located in the /Image/Barcode directory of your PHP install, and you will see that this class has only one method: draw. This method accepts the following parameters: text, type, and imgtype.

  • Text is the string that will be represented in your barcode.
  • Type is the barcode type.
  • Imgtype is the type of image you would like returned, e.g., png, jpg, gif.

Type can be one of three values: Code39, int25, or ean13. For my demonstration, I'll use Code39 as the barcode type and png as the image type.

The code is very simple. The Image_Barcode class returns image data as a stream to the browser and even sets the header. All you have to do is create an instance of the Image_Barcode class and invoke the draw method. You can either hardcode the text to be represented or send it in as a GET. As you can see below, I prefer to use GET:

$bc = new Image_Barcode;
$bc->draw($_GET['bctext'], "Code39", "png");

When you run this code, you will get a barcode image back with the bctext information displayed under the barcode.

If you want further control over the created barcode, it's available, but it's a bit more complicated. Take a moment to examine the code in the Code39.php file in the PEAR\Image\Barcode directory of your PHP install; you'll see that you have control over the bar thin width and the bar thick width in the class constructor. Also, you can control whether the text displays with the barcode and the height of the barcode in the draw method. In order to have this control, however, you need to create an instance of the Image_Barcode_Code39 class directly instead of relying on the Image_Barcode class factory. Here's how your updated code would look:

$bc = new Image_Barcode_Code39('',2,4);
$bc->draw($_GET['bctext'], 'png', true, 120);

I simply create an instance of the Image_Barcode_Code39 class using 2 and 4 as the thin and thick widths, respectively, in the class constructor. Then, I call the draw method and pass the text, the output type, true (to not display text), and 120 as the height of the barcode.

The work of creating barcode images is wrapped up in the barcode classes and the GD library. As you can see, with just a few lines of code, it's possible to add barcodes to your PHP solution.

Editor's Picks

Free Newsletters, In your Inbox