Welcome to freeonlinetest tutorials, here you will learn how to generate QR code for URL using PHP library.
PHP provides several library for generating QR code like phpqrcode library and tc-lib-barcode library.
QR Code stands for Quick Response code. By using this, we can replace a large amount of information, such as smart card data, consumer advertising, website urls, telephone numbers, articles, and much more, into just a two-dimensional matrix Barcode.
It is capable of storing up to 4,296 alphanumeric characters of arbitrary text.
QR Code Generation Using PHP
In this section, you can do this QR code generation by two methods
Generating QR Code using tc-lib-barcode library
Download composer, if you dont have composer in your machine.
Click here to download Composer
Run this code on your working folder, and make sure it will be in your root folder. See the example below screenshot.
1 2 | composer require tecnickcom/tc-lib-barcode |
Put this below code in index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php // Include the library in your project require ('vendor/autoload.php'); // Instantiate the library class $barcode = new \Com\Tecnick\Barcode\Barcode(); $dir = "qr-code/"; // Directory to store barcode if (! is_dir($dir)) { mkdir($dir, 0777, true); } // data string to encode $source = "https://www.freeonlinetest.in/"; // ser properties $qrcodeObj = $barcode->getBarcodeObj('QRCODE,H', $source, - 16, - 16, 'black', array( - 2, - 2, - 2, - 2 ))->setBackgroundColor('#f5f5f5'); // generate qrcode $imageData = $qrcodeObj->getPngData(); $timestamp = time(); //store in the directory file_put_contents($dir . $timestamp . '.png', $imageData); $png ='.png'; //Output image to the browser ?> <div class="center"> <p><?php echo '<img src="'.$dir . $timestamp.$png.'" width="200px" height="200px">';?></p> </div> <style type="text/css"> .center { width: 100px; height: 100px; position: absolute; top:0; bottom: 0; left: 0; right: 0; margin: auto; } </style> |
Generating QR Code using phpqrcode library in PHP
Download the qrcode library from GitHub or download it from below link
Now paste this below code in your index.php, then it will work fine.
1 2 3 4 5 | <?php include "phpqrcode/qrlib.php" ; $content = "http://www.freeonlinetest.in/" ; QRcode::png($content) ; ?> |