PHP code example of sqrart / qart

1. Go to this page and download the library: Download sqrart/qart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sqrart / qart example snippets


new QArtGenerator(prefix: 'https://sqr.art/', version: 5);   // simple logos
QArtGenerator::suggestVersion('photo.jpg');                  // heuristic 5|10|15

use SqrArt\QArt\Cache\FileMatrixCache;
use SqrArt\QArt\QArtGenerator;
use SqrArt\QArt\RenderProfile;

$generator = new QArtGenerator(
    prefix: 'https://sqr.art/',
    errorBudgetPerBlock: 1,                                   // RS codewords sacrificed per block (capped by version/ECC)
    matrixCache: new FileMatrixCache('/tmp/qart-cache'),      // ~4 s saved per generation
    maxAttempts: 3,                                           // retry with a new serial if the decode fails
);

$result = $generator->generate('photo.jpg', 'qr.png', RenderProfile::screen());

$result->url;       // full encoded URL (271 characters)
$result->suffix;    // store this for the lookup GET /{suffix}
$result->serial;    // first 8 characters of the suffix (40 bits of entropy)
$result->mask;      // chosen QR mask (best fidelity score)
$result->attempts;  // 1 unless a regeneration was needed
$result->warnings;  // upscaled image, low contrast…

use SqrArt\QArt\UrlMode;

$gen = new QArtGenerator(prefix: 'https://sqr.art/', urlMode: UrlMode::Short);
$res = $gen->generate('photo.jpg', 'qr.png');
$res->url;      // https://sqr.art/UKmohnVJ — 24 characters, clean at scan time
$res->suffix;   // the serial alone (8 characters) for the lookup

use SqrArt\QArt\ImportanceMap;

$importance = (new ImportanceMap)->protect(x: 0.38, y: 0.18, w: 0.34, h: 0.45);
$res = $gen->generate('photo.jpg', 'qr.png', importance: $importance);
$res->protectedMismatches;   // 0 = zone guaranteed; otherwise a warning

use SqrArt\QArt\Fit;

$gen->generate('logo-large.png', 'qr.png', fit: Fit::Contain);

$importance = (new ImportanceMap)->paint(file_get_contents('mask.png'));

// free crop: source square as fractions (x, y of the origin,
// size as a fraction of the short side) — default: centred square
$res = $gen->generate('photo.jpg', 'qr.png',
    importance: $importance,
    crop: ['x' => 0.15, 'y' => 0.0, 'size' => 0.8],
);

$result = $generator->generate('photo.jpg', 'qr.png', $profile, 'qr.svg');
$result->svgPath; // 'qr.svg'

use SqrArt\QArt\{DotShape, FinderShape};

$profile = RenderProfile::screen()
    ->withDotShape(DotShape::Round)          // Square | Round | Diamond
    ->withFinderShape(FinderShape::Rounded)  // Square | Rounded
    ->withFinderColor('#1a2b4c');            // dark brand colour

use SqrArt\QArt\Ecc;

new QArtGenerator(prefix: 'https://sqr.art/', ecc: Ecc::H, errorBudgetPerBlock: 6);

use SqrArt\QArt\{Ecc, RenderMode, RenderProfile};

$gen = new QArtGenerator(prefix: 'https://sqr.art/', ecc: Ecc::H, errorBudgetPerBlock: 8);
$gen->generate('photo.jpg', 'qr.png', RenderProfile::screen()->withMode(RenderMode::Module));

use SqrArt\QArt\Dithering;

RenderProfile::screen()->withDithering(Dithering::FloydSteinberg);

use SqrArt\QArt\PdfRenderer;

$gen->generate('photo.jpg', 'qr.png', $profile, 'qr.svg', outPdf: 'qr.pdf');   // 100 mm
PdfRenderer::toFile($img, $spec, $matrix, 'qr.pdf', $profile, sizeMm: 60.0);   // custom size

use SqrArt\QArt\UrlMode;

$gen = new QArtGenerator(
    prefix: 'WIFI:T:WPA;S:Café de la Plage;P:password;;',
    urlMode: UrlMode::Short,
    serialLength: 0,
);
$res = $gen->generate('photo.jpg', 'qr.png', $profile);
// $res->url === the payload, $res->suffix === '' (nothing to resolve server-side)

use SqrArt\QArt\Random\SeededRandom;

new QArtGenerator(prefix: 'https://sqr.art/', random: new SeededRandom(42));
bash
composer build-native   # cargo build --release in native/
php -d ffi.enable=1 ...