How to Generate QR Codes in CodeIgniter

QR stands for quick response it’s another form of bar code but some significant difference between them while barcodes hold full details only in the horizontal direction, QR codes stores full details in both direction horizontally as well as vertically. In QR code we can store full details like web url, email, phone number, Address or any types of the set strings etc.

Full details of Qr code is decoded using dedicated QR code scanner or Qr code decoding Application.
 

jQuery Ajax QR Code Generator PHP script Download
If you are looking for a How To Create BarCode generator using PHP then you must go through with this step by step tutorial.

So let’s starts the Codeigniter coding sections.

Library

i use PHPQRCODE library for generating the QR code.So you can download the library using this link PHP QR CODE Library Download

View

qrcodetext.php
And then i Make the view file for enter the text and generate the QR code from this text

<!DOCTYPE html>
<html>
<head>
<title>php qrcode generator - www.pakainfo.com</title>
</head>
<body>
<h2>How to generate QR Code using Codeigniter - www.pakainfo.com</h2>
<form action="<?php echo base_url();?>QrController/qrcodeGenerator" method="post">
<input type="text" name="phpqrcode_input">
<button>Submit</button>
</form>
</body>
</html>


Some brief description of the Qrcontroller.php file

i can Load the Php QR Code library in the constructor of the controller

Qrcontroller.php
 

function __construct ()
{
parent::__construct();
$this->load->library('phpqrcode/qrlib');
$this->load->helper('url');
}

Next i get the entered text and generate the QR Code

public function qrcodeGenerator ( )
{

$phpqrcode_data = $this->input->post('phpqrcode_input');
if(isset($phpqrcode_data))
{

$SERVERFILEPATH = $_SERVER['DOCUMENT_ROOT'].'/qrcode-generation-in-codeigniter/uploads/';
$text = $phpqrcode_data;
$string_qr= substr($text, 0,9);
$directory = $SERVERFILEPATH;
$phpqrcodefilenm1 = $string_qr."-Qrcode" . rand(2,200) . ".png";
$phpqrcodefilenm = $directory.$phpqrcodefilenm1;
QRcode::png($text,$phpqrcodefilenm);
echo"<center><img src=".'http://www.your-domain-name.com/qrcode-generation-in-codeigniter/uploads/'.$phpqrcodefilenm1."></center";
}
else
{
echo 'No Text Entered';
}
}

 

  • $phpqrcode_data – Inputted text that can be encoded in QR Code
  • $SERVERFILEPATH – Path of server where QR Images to be stored
  • $phpqrcodefilenm1 – Name of QR uploads file
  • QRcode::png($text,$phpqrcodefilenm) – Using QRcode::png() function we generate QR code it takes two parameter first one is entered text and 2nd is the name of the QR image file.

Controller

QrController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{

function __construct ()
{
parent::__construct();
$this->load->library('phpqrcode/qrlib');
$this->load->helper('url');
}

public function index()
{
$this->load->view('qrcodetext.php');
}

public function qrcodeGenerator ( )
{


$phpqrcode_data = $this->input->post('phpqrcode_input');

if(isset($phpqrcode_data))
{

//file path for store uploads
$SERVERFILEPATH = $_SERVER['DOCUMENT_ROOT'].'/qrcodeci/uploads/';
$text = $phpqrcode_data;
$string_qr= substr($text, 0,9);

$directory = $SERVERFILEPATH;
$phpqrcodefilenm1 = $string_qr."-Qrcode" . rand(2,200) . ".png";
$phpqrcodefilenm = $directory.$phpqrcodefilenm1;
QRcode::png($text,$phpqrcodefilenm);

echo"<center><img src=".base_url().'uploads/'.$phpqrcodefilenm1."></center";
}
else
{
echo 'No Text Entered';
}
}
}


Dynamic QR codes Generator in Laravel 5.7

 

You can generate QR codes using CodeIgniter by using a third-party library. Here’s an example of how to do it using the phpqrcode library:

Download the phpqrcode library from https://sourceforge.net/projects/phpqrcode/.
Extract the contents of the downloaded archive to the application/third_party directory of your CodeIgniter project.
Create a helper function that loads the library and generates the QR code. Place the following code in a new file named qrcode_helper.php in the application/helpers directory:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH.'third_party/phpqrcode/qrlib.php';

function generate_qr_code($data, $filename, $size=10, $level='L') {
QRcode::png($data, $filename, $level, $size, 2);
}

In your controller or model, load the qrcode_helper and call the generate_qr_code() function to generate the QR code. For example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyController extends CI_Controller {

public function generate_qr() {
$this->load->helper('qrcode');
$data = 'Hello, World!';
$filename = 'qrcode.png';
$size = 10;
$level = 'L';
generate_qr_code($data, $filename, $size, $level);
echo 'QR code generated at ' . $filename;
}
}

In this example, we first load the qrcode_helper using the load->helper() function. We then call the generate_qr_code() function, passing in the data to encode as the first argument, the filename to save the QR code as the second argument, and the size and error correction level as the third and fourth arguments, respectively.

After the QR code is generated, we output a message indicating the filename where the QR code was saved.

Note that this example uses the default settings for the QR code size and error correction level. You can adjust these settings as needed by changing the values passed to the generate_qr_code() function.


#codeigniter 

How to Generate QR Codes in CodeIgniter
3.00 GEEK