Hi, In this tutorial you will learn How to Generate QR Code using Google Chart API in PHP. Before going to the topic you need to know about
what is QR Code?
QR Code is a
machine-readable code, It consisting of
an array of black and white squares. It is typically used for storing the information like general text, contact information, URL, image and SMS this information is reading by the smartphone cameras and QRcode Scanner Devices.
If you want to generate the QRcode in PHP you shall need to load the lots of libraries to your application.But we use this Google chart API to generate the QRcode. Here there is no need to load the libraries to your application just we use the Google Chart API and CURL.
The Script allows you to create the dynamic QRcodes for the content like
TEXT, URL, EMAIL, CONTACT, SMS and other details.
You can save the generated QR code as a PNG file.
qrcode.php
<?php
class QrCode{
// Google Chart API URL
private $apiUrl = 'http://chart.apis.google.com/chart';
private $data;
// It generates URL type of Qr code
public function URL($url = null){
$this->data = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}";
}
// It generate the Text type of Qr code
public function TEXT($text){
$this->data = $text;
}
// It generates the Email type of Qr code
public function EMAIL($email = null, $subject = null, $message = null) {
$this->data = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";
}
//It generates the Phone numner type of Qr Code
public function PHONE($phone){
$this->data = "TEL:{$phone}";
}
//It generates the Sms type of Qr code
public function SMS($phone = null, $msg = null) {
$this->data = "SMSTO:{$phone}:{$msg}";
}
//It generates the VCARD type of Qr code
public function CONTACT($name = null, $address = null, $phone = null, $email = null) {
$this->data = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;";
}
// It Generates the Image type of Qr Code
public function CONTENT($type = null, $size = null, $content = null) {
$this->data = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;";
}
// Saving the Qr code image
public function QRCODE($size = 400, $filename = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$img = curl_exec($ch);
curl_close($ch);
if($img) {
if($filename) {
if(!preg_match("#\.png$#i", $filename)) {
$filename .= ".png";
}
return file_put_contents($filename, $img);
} else {
header("Content-type: image/png");
print $img;
return true;
}
}
return false;
}
}
?>
To create the QR code you need to include the
qrcode.php file in your
index.php file
<?php
include "qrcode.php";
// Create QRcode object
$qc = new QRCODE();
// create text QR code
$qc->TEXT("TechiesBadi");
// render QR code
$qc->QRCODE(400,"qrcode.png");
?>
Like this you can generate the remainig Qrcodes.
// URL QR code
$qc->URL('www.techiesbadi.in');
// EMAIL QR code
$qc->EMAIL('info@techiesbadi.in', 'SUBJECT', 'MESSAGE');
// PHONE QR code
$qc->PHONE('PHONENUMBER');
// SMS QR code
$qc->SMS('PHONENUMBER', 'MESSAGE');
// CONTACT QR code
$qc->CONTACT('NAME', 'ADDRESS', 'PHONE', 'EMAIL');
Here you can download the
Full Source code and check the
demo.
Download
Demo
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff