Generating dynamic QR code using Google API

Google have a QR code API this is an easy way to generate QR code without any programming knowledge. Simply pass your request to api URL that will generate your QR code.

for example, if I want to generate QR code for my web address http://webdevelopertuts.com/ I’ll simply call this url

http://chart.googleapis.com/chart?cht=qr&chs=200×200&chl=http://webdevelopertuts.com/

This URL have three parameters

cht : Specifies a QR code

chs: Image size width and height

chl: Date to encode as QR code

We can add this QR code as an image in HTML


QR code of URL: http://www.webdevelopertuts.com/

<img src="http://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http://webdevelopertuts.com/" />

QR code on print page

if you want to add QR code only on printed pages you can use this tricks

Create style sheet like this


#qr {display:none; }
@media print {
 #qr {display:block; }
}

put you QR image into div


<div id="qr"><img src="http://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http://webdevelopertuts.com/" /></div>

this will show on print only

Generating dynamic QR code with POST request using Php

Using post request for generate QR code will reduce the load time and you can encode more than 2K bytes data.

save this code as qr.php


<?php

header('content-type: image/png');
 $url = 'http://chart.googleapis.com/chart';

// Add image type, image size, and data to params.
 $qrcode = array(
 'cht' => 'qr',
 'chs' => '200x300',
 'chl' => $_REQUEST['q']);

// Send the request, and print out the returned bytes.
 $context = stream_context_create(
 array('http' => array(
 'method' => 'POST',
 'content' => http_build_query($qrcode))));
 fpassthru(fopen($url, 'r', false, $context));

?>

By calling this file with parameter q this will generate your QR code using post request. Will not show the API URL.

for example:


qr.php?q=http://webdevelopertuts.com/

qr.php?q=hello world

<img src="qr.php?q=http://webdevelopertuts.com/" />

Incoming search terms:

  • google qr code api (11)
  • google api qr code (10)
  • qr code google api (7)
  • how to generate qr code for dynamic datas using javascript (6)
  • google apis qr (4)
  • api google qr code (4)
  • chart googleapis com qr code IN HTML USING javascript (3)
  • google api qr codes (3)
  • how to create a pdf with dynamic qr code (3)
  • how to generate qr code in javascript using googleapi (3)

Register your email address to receive articles via email.


Delivered by FeedBurner

Leave a Reply