This topic is locked

Guide 3 - for PHPRunner newbies - Generate QR

9/18/2020 12:57:14 PM
PHPRunner Tips and Tricks
fhumanes author


With the COVID pandemic, 2D barcodes, the so-called QR codes, have become very fashionable.
Some of the people who have contacted for support have told me that they want to include this type of code in invoices or documents in general.
The libraries to create Word, Excel or PDF documents, allow us to integrate images in them, but not specifically QR codes, it is for this reason and to try to provide a solution to those who wish to incorporate QR codes to their documents that I have made this tutorial very simple and that is the first step, to have the image of the QR.
Objective
Create images of QR codes and store them in the file management that PHPrunner does, exactly as if we had used the application to load it into the system.

Solution
For the creation of this type of images I have used the PHP libraries at https://github.com/endroid/qr-code.
DEMO: https://fhumanes.com/qr/
It is very simple and has an example with many characteristics of the product


In the example, both in the Registration and in the Editing of the record, what it does is create a QR with the content of the "Text" field
The code that makes the image and saves it is:





<?php

// https://github.com/endroid/qr-code, source of Code

/*

Occurs after record was updated or added

*/

// Load the QR library classes

require_once __DIR__ . '/../../ComponentCode/qr-code_3.9.1/autoload.php';

use Endroid\QrCode\ErrorCorrectionLevel;

use Endroid\QrCode\LabelAlignment;

use Endroid\QrCode\QrCode;

use Endroid\QrCode\Response\QrCodeResponse;

// Create a basic QR code

$qrCode = new QrCode($values['Text']);

$qrCode->setSize(200);

$qrCode->setMargin(10);

// Set advanced options

$qrCode->setWriterByName('png');

$qrCode->setEncoding('UTF-8');

$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());

$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);

$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);

// $qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());

// $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');

// $qrCode->setLogoSize(150, 200);

$qrCode->setValidateResult(false);

// Round block sizes to improve readability and make the blocks sharper in pixel based outputs (like png).

// There are three approaches:

$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN); // The size of the qr code is shrinked, if necessary, but the size of the final image remains unchanged due to additional margin being added (default)

$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE); // The size of the qr code and the final image is enlarged, if necessary

$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK); // The size of the qr code and the final image is shrinked, if necessary

// Set additional writer options (SvgWriter example)

$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

// Directly output the QR code

// header('Content-Type: '.$qrCode->getContentType());

// echo $qrCode->writeString();

// Generate a data URI to include image data inline (i.e. inside an <img> tag)

// $dataUri = $qrCode->writeDataUri();

// Save it to a file

$fileName = 'QR_'.$values['idprueba_qr'].'.png';

$file = substr(__DIR__, 0, -6); // root of file

$file = $file.'files/'.$fileName;

$qrCode->writeFile($file);

// Save the new file in Database

$size = filesize($file);

$fileArray = [];

$fileArray[0][name]= 'files\/'.$fileName;

$fileArray[0][usrName]= $fileName;

$fileArray[0][size]= $size;

$fileArray[0][type]= 'image\/png';

$fileArray[0][searchStr] = $fileName.',!:sStrEnd';

$fileFileSystem=my_json_encode($fileArray);



// Update in DB the file

$id_key = $values['idprueba_qr'];

$sql="Update prueba_qr set File = '$fileFileSystem' where idprueba_qr = $id_key";

$res=db_exec($sql,$conn);

?>


As always, any questions you can contact me at [email="fernandohumanes@gmail.com"]fernandohumanes@gmail.com[/email]
I leave you the files that you will need, if you want to reproduce it on your computers, in my portal