How to generate QR code using codeigniter ?
In this tutorial we’ll discuss about generate QR code using codeigniter. QR code is an another form of barcode and the QR stands for Quick response. The difference between QR code and barcode is barcode stores data in horizontal direction where qr code can hold data in both horizontal and vertical direction.
Click on this link to check how to generate barcodes in codeigniter.
To generate QR code using codeigniter we have to include a library called PHPQRCODE. Download from phpqrcode link
Controller : QrcodeController.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class QrcodeController extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->library('phpqrcode/Qrlib');
}
public function generate()
{
$data[] = array();
$this->load->view('qrcode/generate', $data);
}
public function qrcodeGenerator()
{
$qrtext = $this->input->post('qrcode_text');
if(isset($qrtext))
{
$SERVERFILEPATH = FCPATH."data/qrcode/";
$text = $qrtext;
$text1= substr($text, 0,9);
$folder = $SERVERFILEPATH;
$file_name1 = $text1."-Qrcode" . rand(2,200) . ".png";
$file_name = $folder.$file_name1;
QRcode::png($text,$file_name);
echo"<center><img src=".base_url().'data/qrcode/'.$file_name1."></center";
}
else
{
echo 'No Text Entered';
}
}
}
- Call Qrlib inside your constructor
- Call the function generate() and load view page
- After submiting the form the function qrcodeGenerator() is called and inside that;
- Post the text to be converted to QR code
- Set the path to store QR code png. FCPATH or $_SERVER[‘DOCUMENT_ROOT’] need to specify because this library won’t allow https url
- QRcode::png() will generate QR code with parameters text and name of image file.
View : generate.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Generate QR Code using Codeigniter</h1>
<form action="<?php echo base_url();?>QrcodeController/qrcodeGenerator" method="post">
<input type="text" name="qrcode_text">
<button>Submit</button>
</form>
</body>
</html>
- Set the form action url as QrcodeController/qrcodeGenerator
- Submit the form