About me
Find out who's in charge of all this happy horse shit:
Advertisement
Twitter feed
- @devbisme Right, of course. 06:18:17 PM April 06, 2012 in reply to devbisme ReplyRetweetFavorite
- @devbisme Good question, I'll bring that up at the next meeting. 06:13:22 PM April 06, 2012 in reply to devbisme ReplyRetweetFavorite
- @devbisme Close! We decided that my family will not be allowed to look at people protesting online about cats run over by steamrollers. 06:09:07 PM April 06, 2012 in reply to devbisme ReplyRetweetFavorite
- Another meeting, another doodle... http://t.co/5aJMxhfa 05:57:48 PM April 06, 2012 ReplyRetweetFavorite
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
- HackaDay tutorial for creating logo’d QR codes : http://hackaday.com/2011/08/11/how-to-put-your-logo-in-a-qr-code/
- Google Chart API : http://code.google.com/apis/chart/
- Campaign Tracking using _setAllowAnchor tags : http://www.lunametrics.com/blog/2009/02/04/setallowanchor/
- Making a watermark in GD : http://www.codingforums.com/showthread.php?t=72317
--
This entry was posted in Software and tagged gdlibrary, php, qrcode, webapp. 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
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 > <
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.
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!
Hey that’s a nice find! Thanks for posting :)
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/.
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.
Hey – thanks.
6 Trackbacks
[...] 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 [...]
[...] 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 [...]
[...] 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 [...]
[...] 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 [...]
[...] 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 [...]
[...] 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 [...]