Generate QR Code Using PHP

QR code is a type of two dimensional barcode which is abbreviated from Quick Response Code. It is an optical label which contains information about the item. It is simply attached to the item. It contains black squares which are arranged in a squares grid with white background.

In this blog we will learn how to generate QR code using PHP step by step. With the help of simple PHP script you can generate QR code for Text, Phone, URL, Email and SMS.

In this project you need to create two files:

  1. qrcode.php
  2. index.php

STEP 1: Define class QrCode in qrcode.php:

1. Create a class QrCode. In this class, we will define the URL for GOOGLE CHART API and functions to generate the QR code for Text, Phone, URL, Email and SMS.

class QrCode {

}

STEP 2: Define URL of Google chart API in QrCode Class:

1. We create variables to store the URL of Google chart API and data which is used to create the  QR code.

class QrCode {
//URL OF GOOGLE CHART API
private $apiUrl = 'http://chart.apis.google.com/chart';
// DATA TO CREATE QR CODE
private $data;
}

STEP 3: Define Functions to generate QR code of different types in QrCode Class:

We have the following types and functions for same in which we can generate the QR Code:

1. URL

// Function which is used to generate the URL type of QR Code.
public function URL($url = null) {
$this->data = preg_match("#^https?\:\/\/#", $url) ? $url :    "http://{$url}";
}

2. TEXT

// Function which is used to generate the TEXT type of QR Code.
public function TEXT($text) {
$this->data = $text;
}

3. EMAIL

// Function which is used to generate the EMAIL type of QR Code.
public function EMAIL($email = null, $subject = null, $message = null) {
$this->data =   "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";
}

4. PHONE

// Function which is used to generate the PHONE type of QR Code.
public function PHONE($phone) {
$this->data = "TEL:{$phone}";
}

5. SMS

// Function which is used to generate the SMS type of QR Code.
public function SMS($phone = null, $msg = null) {
$this->data = "SMSTO:{$phone}:{$msg}";
}

6. CONTACT

// Function which is used to generate the CONTACT 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};;";
}

STEP 4: Define Functions to save the QR Code image in QrCode Class:

//Function which is used to save the qrcode image file.
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;
}

STEP 5: Create a new file index.php.

1. In this file we call the function to generate the QR Code of different types which we defined in qrcode.php

2. To generate the Text QR code you need to call the TEXT function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create Text Code
$qc->TEXT("Knowband");
// Save QR Code
$qc->QRCODE(400,"Knowband_text.png");
?>

3. To generate the URL QR code you need to call the URL function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create URL Code
$qc->URL("Knowband.com");
// Save QR Code
$qc->QRCODE(400,"Knowband_url.png");
?>

4. To generate the EMAIL QR code you need to call the EMAIL function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create EMAIL Code
$qc->EMAIL("Knowband@test.com","Subject","Message");
// Save QR Code
$qc->QRCODE(400,"Knowband_email.png");
?>

5. To generate the PHONE QR code you need to call the PHONE function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create PHONE Code
$qc->PHONE("1234567890");
// Save QR Code
$qc->QRCODE(400,"Knowband_phone.png");
?>

6. To generate the SMS QR code you need to call the SMS function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create SMS Code
$qc->SMS("1234567890");
// Save QR Code
$qc->QRCODE(400,"Knowband_sms.png");
?>

7. To generate the CONTACT QR code you need to call the CONTACT function in your index.php file.

<?php
// Include qrcode.php file.
include "qrcode.php";
// Create object
$qc = new QRCODE();
// Create CONTACT Code
$qc->CONTACT("knowband","Address","1234567890","knowband@test.com");
// Save QR Code
$qc->QRCODE(400,"Knowband_contact.png");
?>

STEP 6: Run script file (index.php) in your environment:

If all goes well then the filename will be named “knowband_text.png” and other which you want downloaded and saved in your computer.

NOTE: You can generate only 1 type of QR Code at a time.

Click here to download the complete code.

Leave a Reply

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