In one of my projects I suddenly needed to generate QR code. I tried searching for some free QR Code generator Library, but didn’t find anything suitable and reliable except the PEAR package which I had used many years back. I was not willing to use the package as the last Alpha release was about 9 years ago. But as there were no suitable package so ultimately used that. The PHP version is 7.2.34.
Using the library is simple and pretty straightforward
Installation:
$pear install channel://pear.php.net/Image_QRCode-0.1.3
PHP Code
require_once './Image_QRCode-0.1.3/Image/QRCode.php'; $qrcode = new Image_QRCode(); $options = array( "image_type" => "png", "output_type" => "return" ); $gd_object = $qrcode->makeCode("STRING", $options); $sx = imagesx($gd_object); $sy = imagesy($gd_object); //The returned object is an "image resource". Which can be saved as an image or can be used as a watermark. Below example is of watermarking which will placed at the bottom right on the main image //imagesx($im) - $sx === the x position where the qr code will be placed //imagesy($im) - $sy === the y position where the qr code will be placed //0, 0 == the start point from where the QRCode image will start to be be copied - 0,0 means from the top left or start of the image - if some other value is given then it will be cropped //imagesx($gd_object), imagesy($gd_object), == the end point of the qr code image. The current copies upto the bottom right or end point of the image. Any value lesser will cause a cropped image. //75 == is the quality of the overall image. $im = imagecreatefromstring($data); // imagecreatefrompng($filename) imagecopymerge($im, $gd_object, imagesx($im) - $sx, imagesy($im) - $sy, 0, 0, imagesx($gd_object), imagesy($gd_object), 75); ob_start(); //Turn on output buffering imagepng($im); //Generate your image $outputImage = ob_get_contents(); // get the image as a string in a variable ob_end_clean(); //Turn off output buffering and clean file_put_contents("IMAGE.PNG",$outputImage);