How To Generate a QR Code using Codeigniter

Read Time1 Minute, 38 Seconds

Hello, Welcome back to KDJ Guru official blog. From this post, you will learn how to generate a QR code using the Codeigniter framework. As a modern framework, it is very flexible and easy to learn. But, most of the times, you have to write most of the functions from scratch by spending more time. Anyways, you can use some useful libraries to save time. This is not a library. But if you need to use this code snippet, you must have knowledge of Views and  Controllers. OK, let’s roll.

View (qrcode.php)

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>How to generate QR Code using Codeigniter</h1>
<form action="<?php echo base_url();?>QrController/qrcodeGenerator" method="post">
<input type="text" name="qrcode_text">
<button>Submit</button>
</form>
</body>
</html>

 

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('qrcode.php');
}
public function qrcodeGenerator ( )
{
$qrtext = $this->input->post('qrcode_text');
if(isset($qrtext))
{
//file path for store images
$SERVERFILEPATH = $_SERVER['DOCUMENT_ROOT'].'/qrcodeci/images/';
$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().'images/'.$file_name1."></center";
}
else
{
echo 'No Text Entered';
}
}
}

I hope, you could do it and make it work by yourself. If you have any issue with this code snippet, please send me a message or leave a comment. Thank You for being with us. Have a nice day!

2 0

About Post Author

Kavidu Jayakody

Kavidu Dilshan Jayakody is the founder and the author of KDJ Blog. He is a skilled web developer and graphic designer. In addition, he has earned his degree BSc (Hons) Computing, UK. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.
100 %
Happy
0 %
Sad
0 %
Excited
0 %
Angry
0 %
Surprise

Leave a Comment

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

KDJ Singlish 1.1.0 for PC/Android/Google Chrome
Download Now

Scroll to Top