Generate QR code using codeigniter

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';
        }  
    }
}
  1. Call Qrlib inside your constructor
  2. Call the function generate() and load view page
  3. After submiting the form the function qrcodeGenerator() is called and inside that;
    1. Post the text to be converted to QR code
    2. Set the path to store QR code png. FCPATH or $_SERVER[‘DOCUMENT_ROOT’] need to specify because this library won’t allow https url
    3. 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>
  1. Set the form action url as QrcodeController/qrcodeGenerator
  2. Submit the form

Related Posts

send mail in codeigniter

Send mail in CodeIgniter

How to send mail in CodeIgniter using the normal mail() email library. We can send mail in codeigniter using the mail() and SMTP methods. To know how…

smtp mail in codeigniter

Sending SMTP mail in CodeIgniter

How to send SMTP mail in CodeIgniter ? We have normal mail() function in CodeIgniter but in some servers we may have to send emails using SMTP…

generate barcode in codeigniter

Generate barcode in codeigniter using zend library

How to generate barcode in codeigniter? Create display barcode using codeigniter with zend barcode library In this article we’ll discuss how to generate barcode in codeigniter using…

upload image from url

Upload image from URL in codeigniter

How to upload image from url in codeigniter? Rather than selecting a file from drive and uploading into folder we can upload image from URL. For that…

codeigniter pagination

Codeigniter Pagination

How to do codeigniter pagination with database? Codeigniter pagination is a very simple method to show large amount of data with a limit. Displaying large amount of…

Create PDF in Codeigniter using Dompdf – flexible library

How to generate a PDF in Codeigniter using Dompdf ? In this tutorial we will create pdf in codeigniter using Dompdf. Here I used image, inline css,…

This Post Has 2 Comments

Leave a Reply

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