January 24, 2024 5:05 am

How to generate QR code in PHP

QR (Quick Response) code used to read your data in your smart devices for ease. If you have a visiting card and want to add name, email and mobile number in you mobile its hart to type if there is a QR code of contact added on that card then you can easily add that in you contact list by scanning that code. Today i am going to show you how to generate a QR code in PHP.

make-qr-code-with-google

[wpdm_file id=35]DEMO

We have used Google Chart Tools to generate code.

Create file QRGenerator.php

<?php
class QRGenerator { 

    protected $size; 
    protected $data; 
    protected $encoding; 
    protected $errorCorrectionLevel; 
    protected $marginInRows; 
    protected $debug; 

    public function __construct($data='https://www.phpgang.com',$size='300',$encoding='UTF-8',$errorCorrectionLevel='L',$marginInRows=4,$debug=false) { 

        $this->data=urlencode($data); 
        $this->size=($size>100 && $size<800)? $size : 300; 
        $this->encoding=($encoding == 'Shift_JIS' || $encoding == 'ISO-8859-1' || $encoding == 'UTF-8') ? $encoding : 'UTF-8'; 
        $this->errorCorrectionLevel=($errorCorrectionLevel == 'L' || $errorCorrectionLevel == 'M' || $errorCorrectionLevel == 'Q' || $errorCorrectionLevel == 'H') ?  $errorCorrectionLevel : 'L';
        $this->marginInRows=($marginInRows>0 && $marginInRows<10) ? $marginInRows:4; 
        $this->debug = ($debug==true)? true:false;     
    }
public function generate(){ 

        $QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=".$this->size."x".$this->size.                            "&chl=" . $this->data .  
                   "&choe=" . $this->encoding . 
                   "&chld=" . $this->errorCorrectionLevel . "|" . $this->marginInRows; 
        if ($this->debug) echo   $QRLink;          
        return $QRLink; 
    }
?>

In a above class we have make many  options image size, data etc now showing you examples of it to generate images.

Example 1: Without data it will generate QR code of https://www.phpgang.com/ link.

<?php
$ex1 = new QRGenerator(); 
echo "<img src=".$ex1->generate().">";
?>

Example 2: This will generate different url with different image size.

<?php
$ex2 = new QRGenerator('http://google.com',100); 
echo "<img src=".$ex2->generate().">";
?>

Example 3: This will Generate simple text QR code with different encoding.

<?php
$ex3 = new QRGenerator('THIS IS JUST A TEXT',100,'ISO-8859-1'); 
echo "<img src=".$ex3->generate().">";
?>

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

4 responses to “How to generate QR code in PHP”

  1. Azhar Sayyad says:

    hello sir but how to implement it in program can you explain it properly my email:[email protected]

  2. Neelima Bharti says:

    Hello Huzoor Bux, It was of great help to my project. I was facing broken QR code with qrcode master library of PHP. Although your code rescued me in perfect time.
    Thanks a lot,
    Neelima Bharti Sharma
    from India 🙂

  3. Zaid Butt says:

    Good Work

  4. Lance Parker says:

    This save my life! perfect and compact!

Leave a Reply

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