The most face rockingest project log on the entire fucking web!
The mobile QR code
Published: October 14, 2011
Close up of QR

If you look closely, you can tell this has been photoshopped; because of the pixels.

Smart phones are fast becoming the predominant tool for media consumption in the restroom. However, finding or getting the right story to your smartphone can sometimes be a pain. I’ve created one possible solution. I started with the premise that everyone in the world loves QR codes with a passion. I’m pretty sure the following link to a web comic clearly illustrates my point?

http://bradcolbow.com/archive/view/the_brads_qr_codes/

What I ended up with was a dynamic, branded QR code creator that uses the google chart API, php and GD; and can be inserted into the story tools widget of a news site or blog.

The setup is pretty easy, your server will need to have php and the GD library installed. A php file will accept four query arguments that will determine what your image will look like. When called with the arguments, the php file will return image data so you can directly include it in an image tag inside a “story tools” widget, if you like.

QR Code arguments:

  • URL – ‘url’ This field should be the URL that the phone will browse to once scanned, presumably the page that you’re on. If you’re in wordpress, you can use the function get_permalink(); which will return the full URL for the page that you’re on.
  • Text – ‘text’ This is an optional field and will place text below the QR Code. If you use this feature, you’ll need the file ARIAL.TTF in the same directory as the php file.
  • Logo image – ‘overlay’ This field is also optional and will overlay a logo on top of your QR code. It can be either a fully qualified URL or a relative path to an image, but it must be a png. The height and width of this image must be small enough to not cover more than 30% of the QR code. If the code won’t scan, make your image smaller.
  • Hash code – ‘hash’ This field can be used to append a ‘tracking code’ to your url using a hash tag. The hash tag will be automatically entered for you. If you use google analytics, follow this tutorial to enable hash tag tracking.

Example:

<img src="http://falldeaf.com/extra/fall_qr.php?overlay=falldeaf_qrcode_overlay.png&text=Scan%20this%20code%20to%20read%20on%20your%20mobile%20device&url=<?=get_permalink();?>" />

I hope this helps someone, enjoy! Alternatively, if I’ve inadvertently created a new, annoying, web fad that bugs the hell out of people, please accept my sincere apology! :)

Here’s the code for the php file, save it to a file called dynamic_qr_png.php for best results, or flibbertygibbit.php if you prefer to be obtuse.

$url = $_REQUEST['url']; // URL for code to
$text = $_REQUEST['text']; // message to display below barcode
$overlay = $_REQUEST['overlay'];
$hash_track = $_REQUEST['hash'];
//Add the Hash symbol to the beginning of the hash code
if(isset($hash_track)) $hash_track = "#" . $hash_track;
//google chart API URL
$image = "http://chart.apis.google.com/chart?cht=qr&chs=190x190&chl=" . $url . $hash_track . "&chld=h";
$font = 'ARIAL.TTF';
//Get the QR code image from google
$background = imagecreatefrompng($image);
$swidth = imagesx($background);
$sheight = imagesy($background);
//if a logo is defined, overlay it on top of the QR code
//Important: overlay image needs to be a png
if(isset($overlay))
{
	$overlay = imagecreatefrompng($overlay); //get image
	imagealphablending($background, true); //needed if logo has transparent layer
	$owidth = imagesx($overlay); //get the heigh and width of the overlay image
	$oheight = imagesy($overlay);
	imagecopy($background, $overlay, ($swidth/2) - ($owidth/2), ($sheight/2) - ($oheight/2), 0, 0, $owidth, $oheight); //paste it on top
}
//If text field isn't blank, draw it in below the code
if(isset($text))
{
	$font_color = imagecolorallocate($background, 0, 0, 0); // set font color
	if(strlen($text) >= 15)
	{
		//split string in two, down the middle
		$half = (int) ( (strlen($text) / 2) ); // cast to int incase str length is odd
		$left = trim(substr($text, 0, $half));
		$right = trim(substr($text, $half));
		// Write Text
		imagettftext($background, 9, 0, 35, 176, $font_color, $font, $left);
		imagettftext($background, 9, 0, 35, 186, $font_color, $font, $right);
	} else {
		// Write Text
		imagettftext($background, 9, 0, 35, 172, $font_color, $font, $text);
	}
}
// Output header and final image
header("Content-type: image/png");
header("Content-Disposition: filename=" . $image);
imagepng($background);
// Destroy the images
imagedestroy($background);
imagedestroy($overlay);

References

--

Read More!
Speak up!
falldeaf logo
This entry was posted in Software and tagged , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. 0x13
    Posted Oct 17 at 12:22 pm | Permalink

    Thanks, but I can use this how? I dont see an example on your site. I’m not versed in php at all…so I have no clue what to do with the code and dont see it used on this page either > <

    • Posted Oct 17 at 1:33 pm | Permalink

      Step 1: Save the code section into a file called dynamic_qr_png.php on your webserver.

      Step 2: Put an html image tag on your webpage that looks like < img src=”[LOCATION OF PHP FILE HERE]?overlay=[LOCATION OF OVERLAY IMAGE HERE]&text=[CAPTION TEXT HERE]&url= [URL TO POINT IT TO HERE] ” />

      Step 3: It’s a fast paced world, these days. Don’t forget to take some time out of your day for you. Take a nice bubble bath and get a massage; pamper yourself.

  2. Posted Oct 20 at 8:45 am | Permalink

    Here is a QR Code generator, which works directly in your browser (JavaScript):

    http://qrlogo.kaarposoft.dk

    It allows you to embed a picture, and it will verify the quality of the QR code afterwards!

  3. Posted Oct 26 at 3:09 pm | Permalink

    I am a big fan and user of QR codes. Glad to see another story about them. I like the leaf idea. Will there be a snowflake one for winter?

    Here’s my article about QR codes being used in jewelry – http://www.theweddingringblog.com/qr-codes/.

    • Posted Oct 26 at 5:01 pm | Permalink

      Holy shit. I’ve never seen a site with more ads than yours, sir!

      Regarding your question, it’s an API of sorts that will allow you to put any image you choose.

6 Trackbacks

  1. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

  2. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

  3. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

  4. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

  5. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

  6. [...] thinks they’re pretty great, Hackaday reader [falldeaf] thought it would be cool to put together an automatic QR code generator to be used on web [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>