QrCode view helper

You see QrCodes popping up every now and again on sites, in publications and the like. I think they can be a very handy way for people with cameras on their phones to get a url or other content on to their phone very easily. (I’m thinking more about those people without iPhones or full keyboards, of course!)

If you’ve never seen a QrCode before, it looks something like this:

QR Code image

Now how cool would it be to be able to generate that automatically for each page on your site and allow people to be able bookmark that site on their phone? Well, I think it’d be pretty cool! So I came up with a very simple ZF view helper to do it for me.

This is the view helper code:

[code lang=”php”]<?php

* Output a QR code block.
* Currently, only via Google Chart API is supported, but it has
* room to add other sources of qrcode generation.
* @category Amnuts
* @package Amnuts_View
* @subpackage Amnuts_View_Helper
class Amnuts_View_Helper_QrCode extends Zend_View_Helper_Abstract
protected $template = ‘
<div class="qrcode">
<p>Bookmark this page on your mobile</p>
<img src="%s" alt="QR Code image" />
<p class="hint"><a href="http://en.wikipedia.org/wiki/QR_Code">What is this?</a></p>

* Constructor.
* @return Amnuts_View_Helper_QrCode
public function qrCode($template = null)
if (null !== $template) {
$this->template = $template;
return $this;

* Generate the QR code image via Google’s Chart API.
* @param array $params
* @return string
public function google($params = array())
$default = array(
‘text’ => $_SERVER[‘SCRIPT_URI’],
‘size’ => ‘100×100’,
‘correction’ => ‘M’,
‘margin’ => 0
$params = array_merge($default, $params);

$params[‘text’] = urlencode($params[‘text’]);
$params[‘margin’] = (int)$params[‘margin’];
if (!in_array($params[‘correction’], array(‘L’, ‘M’, ‘Q’, ‘H’))) {
$params[‘correction’] = ‘M’;
if (!preg_match(‘/^\d+x\d+$/’, $params[‘size’])) {
$params[‘size’] = ‘100×100’;

$url = "http://chart.apis.google.com/chart?cht=qr&chl={$params[‘text’]}"
. "&chld={$params[‘correction’]}|{$params[‘margin’]}"
. "&chs={$params[‘size’]}";
return sprintf($this->template, $url);

As it’s a view helper, it’s very simple to use within your view. Just do something like:

[code lang=”php”]<?php echo $this->qrCode()->google(); ?>[/code]

If you want to change the default template for one instance, you can do that such as:

[code lang=”php”]<?php echo $this->qrCode(‘<li><img src="%s" /></li>’)->google(); ?>[/code]

And, of course, you can change the parameters that the QrCode uses, too. By default, if you don’t supply the ‘text’ parameter then it will use the script uri. But you could change that with something like:

[code lang=”php”]<?php echo $this->qrCode()->google(array(‘text’ => ‘Visit my site at: http://blog.amnuts.com/’)); ?>[/code]

There are other options as view helper code shows. But this should get you started on easily using QrCodes on your own site.


1 Response

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.