QR Code is a machine-readable code that comprises of black squares on a white background. QR code classically used for the storage of information, which is only readable by the camera. Sometimes there is a need to generate dynamic QR code for members, products, or other items in your application. You’ll find a lot of QR code generator libraries to make QR code. But Google Chart API will give you an easy way to generate QR code in PHP without any libraries or plugin.
With the following tutorial, we’ll explain to you how you can generate QR code with PHP with the help of Google Chart API and cURL. Our PHP QR code generator script permits you to create dynamic QR code for URL, text, phone, email, SMS, contact details, and other content.
We’ve created a PHP QR code generator class called QR_BarCode, which will help you to make a QR code image (PNG) or save QR code image as a PNG file.
<?php /** * QR_BarCode - Barcode QR Code Image Generator * @author Allsweb * @url http://www.allsweb.com * @license http://www.allsweb.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 a QR code PNG image, you have to use the 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('Allsweb'); // display QR code image $qr->qrCode();
SEE ALSO: Mobile Number Verification via OTP SMS using PHP
The above example code will generate and show a QR code like the below.
If you want to save QR code image so, use the QR_BarCode class like the following.
<?php // save QR code image $qr->qrCode(350,'images/cw-qr.png');
QR_BarCode class generates different types of QR codes 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');
SEE ALSO: Send Email via SMTP Server in PHP using PHPMailer
Conclusion
Firstly, the QR_BarCode class makes QR code creation very much simple and more accessible. Secondly, there is only a need to attach one file (QR_BarCode.php) to generate different types of QR codes. At last, you can use the cURL in QR_BarCode class; make sure to enable cURL in PHP.
Also, read our previous blog- File Type (extension) Validation with JavaScript