QR Code is a machine-readable code that consists of black squares on a white background. QR code typically used for storing information which readable by the camera. Sometimes you required to generating dynamic QR code for products, members, or other items in your application. You’ll find many QR code generator libraries to create QR code. But Google Chart API provides a simple way to generate QR code in PHP without any libraries or plugin.
In this tutorial, we’ll show how you can generate QR code with PHP using Google Chart API and cURL. Our PHP QR code generator script allows you to generate dynamic QR code for URL, text, email, phone, SMS, contact details, and other content.
We’ve created PHP QR code generator class called QR_BarCode
, that helps to generate QR code image (PNG) or save QR code image as PNG file.
<?php
/**
* QR_BarCode - Barcode QR Code Image Generator
* @author CodexWorld
* @url http://www.codexworld.com
* @license http://www.codexworld.com/license/
*/
class QR_BarCode{
// Google Chart API URL
private $googleChartAPI = 'http://chart.apis.google.com/chart';
// Code data
private $codeData;
/**
* URL QR code
* @param string $url
*/
public function url($url = null){
$this->codeData = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}";
}
/**
* Text QR code
* @param string $text
*/
public function text($text){
$this->codeData = $text;
}
/**
* Email address QR code
*
* @param string $email
* @param string $subject
* @param string $message
*/
public function email($email = null, $subject = null, $message = null) {
$this->codeData = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";
}
/**
* Phone QR code
* @param string $phone
*/
public function phone($phone){
$this->codeData = "TEL:{$phone}";
}
/**
* SMS QR code
*
* @param string $phone
* @param string $text
*/
public function sms($phone = null, $msg = null) {
$this->codeData = "SMSTO:{$phone}:{$msg}";
}
/**
* VCARD QR code
*
* @param string $name
* @param string $address
* @param string $phone
* @param string $email
*/
public function contact($name = null, $address = null, $phone = null, $email = null) {
$this->codeData = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;";
}
/**
* Content (gif, jpg, png, etc.) QR code
*
* @param string $type
* @param string $size
* @param string $content
*/
public function content($type = null, $size = null, $content = null) {
$this->codeData = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;";
}
/**
* Generate QR code image
*
* @param int $size
* @param string $filename
* @return bool
*/
public function qrCode($size = 200, $filename = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->googleChartAPI);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->codeData));
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 QR code PNG image, use QR_BarCode class like the following.
// include QR_BarCode class
include "QR_BarCode.php";
// QR_BarCode object
$qr = new QR_BarCode();
// create text QR code
$qr->text('CodexWorld');
// display QR code image
$qr->qrCode();
The above example code will generate and display QR code like the below.
If you want to save QR code image, use QR_BarCode class like the following.
<?php
// save QR code image
$qr->qrCode(350,'images/cw-qr.png');
QR_BarCode class generates the different type of QR code in PHP.
<?php
// create url QR code
$qr->url('URL');
// create text QR code
$qr->text('textContent');
// create email QR code
$qr->email('emailAddress', 'subject', 'message');
// create phone QR code
$qr->phone('phoneNumber');
// create sms QR code
$qr->sms('phoneNumber', 'message');
// create contact QR code
$qr->contact('name', 'address', 'phone', 'email');
// create content QR code
$qr->content('type', 'size', 'content');
The QR_BarCode class makes QR code creation quicker and easier. Only one file (QR_BarCode.php
) need to be included to generate different types of QR code. The cURL is used in QR_BarCode
class, make sure cURL is enabled in PHP.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
very simple article . thank you
its possible to create one qr code with logo
It worked perfectly, Thanks.
Thank you very much for this article. i appreciate your deeds, it is quite useful to me but the thing is that it is not working on dynamic qr codes. May i know how to work with it. Thank you very much.
very nice article ,Please Provide any solution to calculate no scan of generated qr code,also track user information who scan that qr code