How to Generate QR code in PHP using tc-lib-barcode

  6719 views   1 year ago PHP

In this article i will share with you how to generate QR code in php using tc-lib-barcode library with example. as you knonw in some application we want to generate dynamic QR code with some dynamic data in PHP and the tc-lib-barcode provide you that type of functionality with very easy way.

Download the tc-lib-barcode library from here and implement in your PHP application. here i share a very simple QR code generate script help of this code you can implement dynamic QR code generation functioonality in your PHP application.


<form method="post" name="frmQRGenerator" id="frm-qr"
    onSubmit="return validate();">
    <div class="form-row">
        Email: <input type="text" name="email_field" id="email_field"
            class="input-field" />

        <input type="submit" name="generate" class="submit-button"
            value="Generate QR Code" />

<div id="validation-info"></div>
<script src="jquery-3.2.1.min.js"></script>
	function validate() {
		var valid = true;
		var emailRegexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i

		if ($("#email_field").val() == "") {
			$("#validation-info").html("Email is required.");
			valid = false;
		} else if (!(emailRegexp.test($("#email_field").val()))) {
			$("#validation-info").html("Invalid Email.");
			valid = false;
		return valid;


if (! empty($_POST["email_field"])) {
    require ('tc-lib-barcode/vendor/autoload.php');
    $barcode = new \Com\Tecnick\Barcode\Barcode();
    $targetPath = "qr-code/";
    if (! is_dir($targetPath)) {
        mkdir($targetPath, 0777, true);
    $bobj = $barcode->getBarcodeObj('QRCODE,H', $_POST["email_field"], - 16, - 16, 'black', array(
        - 2,
        - 2,
        - 2,
        - 2
    $imageData = $bobj->getPngData();
    $timestamp = time();
    file_put_contents($targetPath . $timestamp . '.png', $imageData);
<div class="result-heading">Output:</div>
<img src="<?php echo $targetPath . $timestamp ; ?>.png" width="150px"

i  hope it will be help to you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]