Generating a QR Code for the Current URL (Google Chart API/Drupal 6.x)

QR code of this URL
QR code for this URL

Just a few hours ago Google announced the latest feature of their chart API: QR Codes. I always thought that it might be cool to have some QR image of the current URL on my website. So, if someone happens to read one of my articles over at some internet café or at a friend's machine, a QR code might be handy for "bookmarking".

Writing a QR encoder with PHP didn't look like much fun though. And then there is the processing overhead, which can be addressed with caching, but that only means more work. There is also that random string DoS attack vector. Far too much hassle, really. But if Google takes care of all that, it's a piece of cake.

That's why I wrote a little test module for Drupal. It lacks width/height options, but apart from that it's fully functional (as far as I can tell).

<?php
function google_qr_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks = array();
    $blocks[] = array('info' => t('Google QR'),'cache' => BLOCK_CACHE_PER_PAGE);
    return $blocks;
  }
  else if ($op == 'view' && user_access('view Google QR')) {
    $query_string = drupal_query_string_encode($_GET, array('q'));
    if (empty($query_string)) {
      $query_string = NULL;
    }
    $request = trim($_REQUEST['q'], '/');
    $alias = drupal_get_path_alias($request);
    $path = $request;
    if ($alias != $request) {
      $path = $alias;
    }
    $url = urlencode(url($path, array('query' => $query_string, 'absolute' => TRUE)));
    $block = array(
    //'subject' => $url,
    'content' =>
    '<div class="google-qr-block">
    <img src="http://chart.apis.google.com/chart?chs=150x150&amp;cht=qr&amp;chl='.$url.'" alt="QR code for this URL" width="150" height="150"/>
    </div>'
    );
    return $block;
  }
}
?>

Using drupal_urlencode instead of urlencode doesn't work by the way.

If you're wondering why I'm not using that module here yet: Well, the right sidebar is already overcrowded the way it is. The only other sensible region is "Header", but that doesn't work well with the current theme. I'll probably reserve some space for it if I ever get around finishing my custom theme.

Download: Google QR module for Drupal 6.x (1kb)

Comments

So cool...

I was writing it just yesterday with Nokiacodes....
Why you didn't put it on the official site of Drupal??

Huh?

As contributed module? I'm sorry, but I can't be arsed to use CVS. ;)

If they finally switch to SVN (they intend to do that for a couple of years), I'll probably contribute a few things.

yes, you are right...

It's more easier with SVN...
What have you to contribute more??

And do you want that I contribute this module for you?

Feel free to contribute that

Feel free to contribute that module if you feel like maintaining it. Since the code is available under a 0-clause BSD-style license you can do whatever you want with it.

And other things... well, see that highlighted code above? That's done by some simple filter module which offloads the highlighting work to JavaScript (SHJS).

ok, your right..

I try to ask them a CVS account by 2 times, and they refused again...
So ok, it's more hard to contribute than what I thought...

And what have you more in drupal module?

to small for a module?

what kind of features of settings should be added so it's a full google qr code module?

missing permissions ..

the block does not display for anonymous and roles ..
the code checks for user_access('view Google QR') but there is no permission hook ..
add this function to your module:

function google_qr_perm() {
return array('view Google QR');
}

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options