Recently, I was asked to help out with a project that requires working with QRCodes. QRCode (short for Quick Response Code) is a specific two-dimentionsl code, also refered to as a matrix barcode, which is readable by specially designed QR barcode readers. With the advent of the cellphone-camera, it is easier than ever to have a 2D barcode scanner in your pocket. QRCodes consist of black-squared modules arranged in a larger square pattern on a white background. These codes are used for all kinds of things from encoding website URLs to encrypting private messages.
The first piece of the project that I have been asked to work on is an application that will run on the Blackberry, iPhone, Android, and other mobile devices soon to come. However, before I could begin writing a QRCode reader for cellphones, I first needed a way to create some QRCodes to test with. Since the second piece of this project will require QRCodes to be generated and displayed on a website, I thought I would start by writing a QRCode generator in PHP. So, I began writing my own QRCode library. But, what if I only wanted some codes and didn’t want to go thru the hassle of writing an entire barcode library? Well, that’s what I’m going to show you how to do now. It’s extremely easy to do. So, let’s get to it.
If you do not want to create your own barcode library, our good friend Google can help. With the use of the Google Chart API, we can quickly generate charts of all kinds, including QR Codes. To do this, we will basically call the Google Chart API and pass it a few parameters such as size, chart type, and the text we want encoded. So, open the PHP page that you intend on including QR Codes in and add a new function called “generateQrCode”. You can call this function whatever you want. For the parameters, you will need to add a variable for the input text, the chart size, the chart level, and a margin.
function generateQrCode($input, $size = “150″, $level = “L”, $margin = “0″)
As you can see, I set defaults for the last 3 parameters as those won’t change much, but can be overloaded if necessary. Before continuing, I should probably take a moment to explain a few things. First off, like other forms of data encoding, QR Codes are limited to how much data they can hold. With that said, QR Codes are still capable of holding a fairly significant amount of data. Here are the maximum size limits for QR Codes.
- Numeric Only: 7,089 characters
- Alphanumeric: 4,296 characters
- Binary (8 bits): 2,953 bytes
There are also things like the Micro QR Code which is simply a smaller version of the standard QR Code. The Micro QR Code is only capable of storing up to 35 numeric characters. But, they’re still useful as certain times.
In the function above, you can see that I have included a parameter called “level”. The level variable tells the QR Code generator what error correction percentage to use. This is very helpful when the code becomes damaged, isn’t scanned at the perfect angle, or the QR Code has something on it making it difficult to read. Here are the different error correction levels.
- Level L: 7% of codewords can be restored
- Level M: 15% of codewords can be restored
- Level Q: 25% of codewords can be restored
- Level H: 30% of codewords can be restored
Since QR Codes are expected to be read as a perfect square, you’ll notice that I only include 1 size parameter which I’ve defaulted to 150. This size parameter will be used for both the width and the height. The margin parameter is simply a way to tell the QR Code generator how much extra space to place around the edge of the code. I like to default it to zero so that the code fills up the entire 150 pixels. I can add extra space in my PHP code if I need a margin.
Alrighty, now that we have a little more understanding of QR Codes and the parameters needed to make one, let’s add the rest of the code to our “generateQrCode” function. To begin with, if you’re planning on passing in URLs as the input parameter, it’s always a good idea to call the built-in “urlencode” function from PHP. This will make our incoming URL a little more friendly for passing to the Google Chart API. If you plan on using this same function for encoding more than URLs, you should leave out this line and put it only in the places that are actually passing URLs. But, it’s still good to leave it here in some cases so that all incoming text gets encoded properly.
$url = urlencode($input);
The next thing you need to do is tell the function to echo, print, or return the actual image. For the src attribute of your image, you will include a specially designed URL which points to the Google Chart API and includes all of the required parameters including the parameters you’ve passed into the function. Here is what my version looks like:
echo “<img src=\”http://chart.apis.google.com/chart?chs=\”" . $size . “x” . $size . “&cht=qr&chld=” . $level . “|” . $margin . “&chl=” . $chl . “\” widht=\”" . $size . “\” height=\”" . $size . “\”/>”;
That’s it! You’re now ready to put your QR Code to work. To do that, call the “generateQrCode” function from somewhere within your PHP page and pass it the text you want encoded. Here’s an example:
generateQrCode(“http://www.prodigyproductionsllc.com”);
If everything went according to plan, you should see the following QR Code image.
If you have a QR Code reader installed on your mobile device, go ahead and scan the image above. It should redirect you to my home page.
Here is the complete code for this example:
<?php
function generateQrCode($input, $size = "150", $level = "L", $margin = "0") {
$url = urlencode($input);
echo "<img src=\"http://chart.apis.google.com/chart?chs=\"" . $size . "x" . $size . "&cht=qr&chld=" . $level . "|" . $margin . "&chl=" . $chl . "\" widht=\"" . $size . "\" height=\"" . $size . "\"/>";
}
?>
<html>
<head>
<title>QR Code Test</title>
<body>
<?php generateQrCode("http://www.prodigyproductionsllc.com"); ?>
</body>
</html>
