AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

Generate QR Code with PHP

Lakshika Mathur by Lakshika Mathur
January 1, 2020
Reading Time: 3 mins read
0
How to Generate QR Code with PHP using Google Chart API

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.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

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

Tags: Generate QR CodePHP
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
Send Email via SMTP Server in PHP using PHPMailer
PHP and MySQL

Send Email via SMTP Server in PHP using PHPMailer

December 31, 2019
55
Next Post
How to Create Loader Animation with CSS

How to Create Loader Animation with CSS

Display Loading Image While Page Loads using jQuery and CSS

Display Loading Image While Page Loads using jQuery and CSS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version