You'll have seen these printed "QR" codes turning up on printed documents such as railway timetables, brochures and business cards, and they allow you to wave your phone at the pattern and have a copy of the document (or other details) come up there in electronic form.

How do they work?

QR codes are really two dimensional barcodes. Hidden within the code is the text of a URL. QR code reader applications analyse the image, use the common features of the squares at three of the corners to work out its orientation and perspective, and then call up a browser of other application to take the user to the URL specified.

QR codes vary in size depending on the length of the data they're storing, and they hold a number of checking elements (you can choose how many / what strength) to ensure a correct interpretation of fuzzy or slightly damaged images.

How are they set up?

There are quite a few web sites that you can type a URL into and will give you a QR code. There isn't a central registry / look up for the codes, so you don't have to look for a good central supplier who you know you'll be able to trust, nor do you need to pay a fee.

Be aware, though, that QR codes can contain any URL, and you may find sites that return you an impure QR code - i.e. one that points to a URL on their server which they then redirect to you. Such redirects are very commonly used (and to great advantage and openly) on media such as Twitter, where http://bit.ly/z6oqQK is far fewer characters that http://www.wwrug.org.uk/calendar.html. These short codes together with QR mean a simpler and smaller matrix, but if you use them you are relying on the services of the people at bit.ly and the survival of their service for the duration of your printed papers.

Why put QR codes on my web site?

QR codes let you go from Printer Material to an electronic device - so why on earth would you want to have them on a website which you'll already be calling up electronically?

1. People print pages out. I do it myself a lot of the time, and then they hand those printed pages to people. I usually recommend that web site pages that are likely to be printed should include their URL so that people can type them and get back there - and now I'm going to recommend that you add in a QR code too

2. There's nothing to stop someone waving a QR code reader at a web page on your screen during a presentation to quickly bookmark the URL, or to transfer a page from their own computer to their mobile phone ...

Can I set up a QR code myself?

Yes - the algorithm is public, and libraries such as the phpqrcode library are available under Open Source - [here] on sourceforge. I have installed this library on one of our servers, and can now generate QR codes as and when I want them.

Installation issues

PHP QR Code requires PHP version 5, built with the GD Library and zlib. Although GD and zlib are both shipped with current source distributions of PHP, you need to ensure that they are built in to your live server. When I tried the library for the first time on our server, I got a fatal error because the function gzuncompress was missing and had to rebuild adding to my previous build (using the config.nice script). The addition is: --with-zlib

We already have extensive graphics (GD) support on our server: --with-gd
--with-png-dir=/usr
--with-jpeg-dir=/usr
--with-ttf
--with-freetype-dir=/usr/local
--enable-gd-native-ttf
so no extra were added for GD. Documentation suggests that --with-gd should be sufficient.

I've installed the code into a separate directory away from the main document tree (to avoid direct access) and added the path to that separate directory into the example code that I'm using. But we have lots of separate demos on our server that we want to keep separate, and for a more regular use I would suggest you use the central library facilities provided by PHP.

Calls in the library (qrlib.php pulls in other files as needed) and generates a QR code image for http:// www.wellho.net/ course/ index.html in the file /home /wellho /public_htm l/qr /demo.png . The lowest of the four levels of validation bits is used ("L"), each blob is 4 pixels square, and there is a 2 pixel border. The HTML code send out to the browser then calls up the image.

I've had to prepare the way by ensuring that demo.png is writable by the web server ...

Images can be directly generated and output too, and by using a variable to pass in the URL they can be generalised. Here's a snippet of code from such an image generator: include "$_SERVER[DOCUMENT_ROOT]/../qrcode/qrlib.php";
QRcode::png($url,false,"L",4,2);
with the false flag indicating direct output to the browser; this means that the URL of the PHP script needs to be called up in an <img src="***"> format, where *** is the URL of the code generator and any GET parameters needed to tell it the URL.

Performance and security issues

The same parameters in to QRcode::png will generate the same graphic each time ... and there is an element of resource needed in the generation with could be significant on a very busy server. Why do the same work time after time - why not store common codes with a caching mechanism? There is such a mechanism available in the PHP QR code library.

And if you put a dynamic QR code generator on your site, you'll certainly want to add some security tests to ensure that you're not providing a very public service to provide some very naughty links. I've limited ours to providing links only to specified domains, and to validating the page name too. Try running my dynamic generator on a naughty link, and you'll get a very nice picture of a horse that's totally useless for linking!

This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.