Community Documentation

QR Block For Current Page using Google.

Last updated November 14, 2012. Created by Engale on April 21, 2011.
Edited by irp-socotec-nuc.... Log in to edit this page.

I was interested in giving user of the site a way to promote their material and other material with QR codes on my Drupal site. There where a few modules out there that could create the desired results but all of them seemed to be way to over bearing for my desired goal. I just wanted an image that would provide a QR code of the current URL (http://www.example.com).

I did some digging and eventually found that Google Charts could, with relative ease provide a QR Code image.
Now with google charts they have a wizard found here: http://code.google.com/apis/chart/docs/chart_wizard.html
With that wizard I learned that the image was based on the url it called.
An example QR image url from them would be http://chart.apis.google.com/chart?chs=200x100&cht=qr&chl=http://www.dru...
Now that's nice, That QR code would send a user to http://www.drupal.com and you can see that if we would replace the url for drupal with another url, say the url of the current page of our site, it would be that url.

With drupal I created a new block and set it so that it could be seen on certain types of pages with php. The code I used for the block was also done in php so that I could use the current url and send that to the google api and get my qr image for the block, the code follows.

<?php
function selfURL(){
   
$query = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:$_SERVER['PHP_SELF'];
   
$protocol = (isset($_SERVER["HTTPS"])&&$_SERVER["HTTPS"] == "on")?"https://":"http://";
   
$port = ":".$_SERVER["SERVER_PORT"];
   
$name = $_SERVER['SERVER_NAME'];
    return
urlencode("$protocol$name$port$query"); 
}
function
strleft($s1, $s2) {
    return
substr($s1, 0, strpos($s1, $s2));
}
print
"<img src=\"http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=" . selfURL() . "\" width=\"200\" height=\"200\" alt=\"\" />";
?>

set the prepositions so the block is viewed properly to the way you want it but this code will update the QR code for the current page.

Comments

Php input filter

You must setup a input filter that lets you evaluate php as well for this to work, else it will not function right.

QR Code is really helpful in

QR Code is really helpful in daily life and business life. I can't image how this small square to encode so such information until I use it by chance. That' so cool! Maybe u can find more information on http://www.keepautomation.com/.

Tutorials and site recipes

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here