QR Code generation using Google Chart API

These days QR Code has become more and more popular as it capture all the information using a Smart Phone. QR codes can be used in Google’s Android operating system and iOS devices (iPhone/iPod/iPad), as well as by using 3rd party barcode scanners.

So here I am going to tell how to build QR Code generator using Google Chart API.

First of all you need to create two file
index.php: in the file you create a form where you collects the input from the user like size, encoding, content and correction. And make the action of the this form to generate.php.

generate.php: in this file collect the input data from the above form and process it with Google Chart API and create the dynamic QR Code.

File index.php

font-family: Arial, serif;
margin: auto;
padding: 0px;
margin: 0px;
form textarea{
font-family: Arial, serif;
overflow: hidden;
border:1px solid #DDD;
width: 300px;
#generator fieldset{
border:1px solid #DDD;
width: 350px;
<div id="container">
<h1>QR Code Generator</h1>
<div id="generator">
<form target="qrcode-iframe" action="generate.php" method="post">
<input type="radio" name="size" value="150×150" checked>150×150<br>
<input type="radio" name="size" value="200×200">200×200<br>
<input type="radio" name="size" value="250×250">250×250<br>
<input type="radio" name="size" value="300×300">300×300<br>
<input type="radio" name="encoding" value="UTF-8" checked>UTF-8<br>
<input type="radio" name="encoding" value="Shift_JIS">Shift_JIS<br>
<input type="radio" name="encoding" value="ISO-8859-1">ISO-8859-1<br>
<textarea name="content"></textarea>
<legend>Error correction:</legend>
<select name="correction">
<option value="L" selected>L</option>
<option value="M">M</option>
<option value="Q">Q</option>
<option value="H">H</option>
<input type="submit" value="Generate QR Code"></input>
<div id="result">
<iframe name="qrcode-iframe" frameborder="0" id="qrcode" src="generate.php" height="315px;" width="350px"></iframe>

In the above file collects the data from the user and sends that data to generate.php and create the QR Code using Google Chart API in the iFrame.

File generate.php

//capture from the form
$qr_size = $_REQUEST[‘size’];
$qr_content = $_REQUEST[‘content’];
$qr_correction = strtoupper($_REQUEST[‘correction’]);
$qr_encoding = $_REQUEST[‘encoding’];

//form google chart api link
$qrImageUrl = "https://chart.googleapis.com/chart?cht=qr&chs=$qr_size&chl=$qr_content&choe=$qr_encoding&chld=$qr_correction";

//print out the image
echo ‘<img src="’.$qrImageUrl.’">’;

In the above file (generate.php) collects the data from the form using REQUEST method and append the data into the Google Chart API URL and generate the QR Code image.

Email With Attachment

Generally we use to send text/html mails in PHP by mail() function, this function is not providing to sent attachment file by default, so here i write a function using mail() function to send the email with attachment.

Below is that I wrote and modified to send email with attachment and how to use this function.


function qt_mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "–".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; // use mail format here
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "–".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "–".$uid."–";
if (mail($mailto, $subject, "", $header)) {
echo "mail send … OK";
} else {
echo "mail send … ERROR!";

// function uses below

$qt_recipient = ""; // recipient mail address goes here
$qt_file = "somefile.zip"; // name of the file goes here
$qt_path = $_SERVER[‘DOCUMENT_ROOT’]."/your_path_here/"; // path where the file is goes here
$qt_name = "Quality Tuts"; // sender name goes here
$qt_mail = "my@domain.com"; // sender email goes here
$qt_replyto = "qt_reply_to@domain.com"; // reply to email goes here
$qt_subject = "This is a mail with attachment."; // mail subject goes here
$qt_message = "Hello, Your message goes here, QT"; // mail message goes here
qt_mail_attachment($qt_file, $qt_path, $qt_recipient, $qt_mail, $qt_name, $qt_replyto, $qt_subject, $qt_message); // call to function