Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

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

Parent Feed: 
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)

Author: 
RSS Tags: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web