Generating dynamic QR codes in CodeIgniter 4 can be achieved using various libraries. One of the most popular options is the PHP QR Code Library. Here's a step-by-step guide on how to generate a dynamic QR code in CodeIgniter 4, including storing QR code images on the server. You can add text content, Website URL( or any URL ), email, phone number, SMS, Business Card, and other info to the QR code and generate QR barcode images with Codeigniter Application.
Table Of Content
1 Prerequisites
1.) PHP version of 8.2
2.) PHP GD Extension
2 Introduction
A QR code (Quick Response code) is a type of 2D barcode used to store information. In this guide, we will demonstrate how to generate QR codes in CodeIgniter 4 dynamically, as well as provide examples for storing QR code images on the server.
3 Create / Install a Codeigniter 4 Project
3.1 Install Codeigniter 4 Project
Ensure your computer has Composer installed. Use the following command to set up a new project:
When we install CodeIgniter 4, we will have env file at root. To use the environment variables, we need to rename env to .env
Also we can do this in command Line.
Open project in Command Line
sudo cp env .env
Above command will create a copy of env file to .env file. Now we are ready to set environment variables.
Configure Development Mode
CodeIgniter starts up in production mode by default. You need to make it in development mode to debug or check any error if you are working with application.
Open .env file from root.
# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development
Now application is in development mode.
4 Install the QR Code Library
To Install Qr Code library in CodeIgniter 4, Create a class file inside "/app/Libraries" directory.
Create a library file with name Ciqrcode.php class into /app/Libraries folder
Open Ciqrcode.php file and write this code into it.
<?php
declare(strict_types=1);
namespace App\Libraries;
use QRcode;
use QRimage;
use function array_filter;
use function count;
use function define;
use function defined;
use function in_array;
use function is_array;
use function max;
use function min;
class Ciqrcode
{
var $cacheable = true;
var $cachedir = WRITEPATH . 'cache/';
var $errorlog = WRITEPATH . 'logs/';
var $quality = true;
var $size = 1024;
function __construct($config = [])
{
include APPPATH . '/ThirdParty/qrcode/qrconst.php';
include APPPATH . '/ThirdParty/qrcode/qrtools.php';
include APPPATH . '/ThirdParty/qrcode/qrspec.php';
include APPPATH . '/ThirdParty/qrcode/qrimage.php';
include APPPATH . '/ThirdParty/qrcode/qrinput.php';
include APPPATH . '/ThirdParty/qrcode/qrbitstream.php';
include APPPATH . '/ThirdParty/qrcode/qrsplit.php';
include APPPATH . '/ThirdParty/qrcode/qrrscode.php';
include APPPATH . '/ThirdParty/qrcode/qrmask.php';
include APPPATH . '/ThirdParty/qrcode/qrencode.php';
$this->initialize($config);
}
public function initialize($config = []): void
{
$this->cacheable = $config['cacheable'] ?? $this->cacheable;
$this->cachedir = $config['cachedir'] ?? $this->cachedir;
$this->errorlog = $config['errorlog'] ?? $this->errorlog;
$this->quality = $config['quality'] ?? $this->quality;
$this->size = $config['size'] ?? $this->size;
// use cache - more disk reads but less CPU power, masks and format templates are stored there
if (! defined('QR_CACHEABLE')) {
define('QR_CACHEABLE', $this->cacheable);
}
// used when QR_CACHEABLE === true
if (! defined('QR_CACHE_DIR')) {
define('QR_CACHE_DIR', $this->cachedir);
}
// default error logs dir
if (! defined('QR_LOG_DIR')) {
define('QR_LOG_DIR', $this->errorlog);
}
// if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
if ($this->quality) {
if (! defined('QR_FIND_BEST_MASK')) {
define('QR_FIND_BEST_MASK', true);
}
} else {
if (! defined('QR_FIND_BEST_MASK')) {
define('QR_FIND_BEST_MASK', false);
}
if (! defined('QR_DEFAULT_MASK')) {
define('QR_DEFAULT_MASK', $this->quality);
}
}
// if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
if (! defined('QR_FIND_FROM_RANDOM')) {
define('QR_FIND_FROM_RANDOM', false);
}
// maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images
if (defined('QR_PNG_MAXIMUM_SIZE')) {
return;
}
define('QR_PNG_MAXIMUM_SIZE', $this->size);
}
public function generate($params = [])
{
if (
isset($params['black'])
&& is_array($params['black'])
&& count($params['black']) == 3
&& array_filter($params['black'], 'is_int') === $params['black']
) {
QRimage::$black = $params['black'];
}
if (
isset($params['white'])
&& is_array($params['white'])
&& count($params['white']) == 3
&& array_filter($params['white'], 'is_int') === $params['white']
) {
QRimage::$white = $params['white'];
}
$params['data'] = $params['data'] ?? 'QR Code Library';
if (isset($params['savename'])) {
$level = 'L';
if (
isset($params['level']) && in_array(
$params['level'],
['L', 'M', 'Q', 'H']
)
) {
$level = $params['level'];
}
$size = 4;
if (isset($params['size'])) {
$size = min(max((int) $params['size'], 1), 10);
}
QRcode::png($params['data'], $params['savename'], $level, $size, 2);
return $params['savename'];
} else {
$level = 'L';
if (
isset($params['level']) && in_array(
$params['level'],
['L', 'M', 'Q', 'H']
)
) {
$level = $params['level'];
}
$size = 4;
if (isset($params['size'])) {
$size = min(max((int) $params['size'], 1), 10);
}
QRcode::png($params['data'], null, $level, $size, 2);
}
}
}
?>
Here are some examples to generate Various types of QR code images using PHP.
1.URL QR Code: Specify the website URL, including the protocol:
Specify the website URL including the protocol (HTTP or HTTPS) to recognize the QR code as a URL.
$qrContent = 'https://getsamplecode.com/';
2.Text QR Code: Specify the text to generate the QR code:
Specify the Text to Generate QR Code.
$qrContent = 'QR Code Generated by GetSampleCode';
3.Phone Number
Specify the Phone Number including Country Code to Generate QR Code.
$qrContent = 'tel:+16471234567';
4.SMS
Specify the Phone Number including Country Code and pre filled message to Generate QR Code.
$qrContent = 'sms:+16471234567:Samplemessage';
5.EMAIL
Specify the Email Address to Generate QR Code.
$qrContent = ''mailto:getsamplecode@gmail.com';';
6 Create New Controller - QrcodeController
Now create a controller "QrcodeController"
Create the controller using the following command:
php spark make:controller QrcodeController
Inside the controller, define index and generate methods for handling QR code types such as URLs, phone numbers, SMS, and business cards. For instance, here’s how you can handle different QR code types dynamically:
app/Controllers/QrcodeController.php
Define routes for the QrcodeController in the Routes.php file app/Config/Routes.php
use CodeIgniter\Router\RouteCollection;
$routes->get('/', 'Home::index');
$routes->get('qrcode', 'QrcodeController::index');
$routes->post('store', 'QrcodeController::store');
9 Folder Structure
10 Run Web Server to Test the App
Use the following command to Test the App.
php spark serve
Visit the URL
http://localhost:8080/index.php/qrcode
11 Conclusion
This guide showed you how to dynamically generate a QR code in CodeIgniter 4 and provided an example of storing QR code images on the server. With the flexibility of the PHP QR Code Library, you can easily create different types of QR codes for your business or personal use.