For a project I am working on right now, I needed to generate barcodes and QR codes. Looking around for solutions for the barcodes I quickly found Zend_Barcode (thanks to someone pointing me to it, by the way. I had not expected a barcode generator in Zend). For QR, it was slightly harder to find a good solution. A quick question on Twitter helped a lot. In the end, it boiled down to two options: PHP QR Code and using the Google Chart API.
First, let's have a look at barcodes. For this project, I will be working on some shipping labels. Postal services can be very strict in their demands on the labels (and rightfully so), so it was a nice surprise to see that Zend_Barcode - aside from supporting the regular standards - has support for several of the postal service standards, like Identcode (used by German postal service), Postnet and Royal Mail. This will surely be really important for my project.
Using Zend_Barcode is really easy. I used my autoloader to autoload the Zend library in my project. Obviously, I had to also include the dependencies of Zend_Barcode (Zend_Loader, Zend_Validate, Zend_Exception and Zend_Registry). After that, it's just setting up the right options based on the request, and outputting the barcode:
As you can see, it's just a matter of fetching the right options from the request, setting the response content type, and calling the Zend_Barcode factory to create the barcode, then render it. It doesn't get any easier.
Now for QR I still had two options left. Usually, when I have two options left, and one is Google, I go for the Google solution. Why? Well, Google usually makes it really easy to use their API, is solid, and is always available. Oh, and *fast*. However, for this project, I decided against this. The main reason for this is that my application may end up running inside a hugely firewalled corporate network with very limited connectivity to the outside world. So I needed something stand-alone for this. The PHP QR Library seemed like a very decent solution and came recommended by people whom I trust a lot on this.
Using PHP QR is possible even simpler than using the Barcode, because it's a single standard so doesn't really have rendering options. So it's just passing along the information that needs to be in the QR code to the library and having it generate the graphic.
I'm loading the qrlib file here (which includes all the library files), setting the content type and then simply calling QRcode::png() to have the library output a PNG for the passed text. That's it!