Generating Barcodes in PHP

I’ve discovered some applications which will allow you to create barcodes in PHP but never came across one which had been issued under MIT license (which allows full use for free, including commercial use, with a few restrictions). All the other applications I had found were quite expensive or licensed only for not-for-profit use.

Then, I stumbled upon this post by David Scott Tufts who had developed a PHP script which will generate barcodes “in four barcode formats including Code 128, Code 39, Code 2of5, and Codabar”. I downloaded his code and tried it out and, sure enough, it worked perfectly for the application I was building! Thanks to David Scott Tufts for saving me a significant amount of time, or quite a bit of money! His solution is quick, accurate, and simple.

To use his script, download his PHP file (he uses the .phps extension on the file to allow download, I believe, so I just changed that to .php before using). Upload the script to your server and use it as follows in your HTML:

<img src=”/images/barcode.php” alt=”barcode” />

Available parameters are:


text=string (Default: “0”)

size=int (Default: “20”)

codetype=(Code128|Code39|Code25|Codabar) (Default: “Code128”)

orientation=(horizontal|vertical) (Default: “Horizontal”)

Text: Should be a url-escaped string (e.g. “%20″ for ” “).

Size: 50 worked well for me. Play around to find the best size for your application, especially if printing.

Code Type: I used Code 128 for my application. The code you use may depend on your application or the scanner you expect to read the barcode with.

Orientation: Self-explanatory. Just make sure your IMG tag widths and heights reflect the orientation correctly.

That’s all there is to it! It took a few minutes to play around with the settings and get something that worked for what I needed, but compared to writing my own script it was a breeze.

Notes:

  1. This script requires PHP’s GD Library to be installed and available on the server.

2 thoughts on “Generating Barcodes in PHP”

Leave a Reply