PHP code example of aicrion / qrcode

1. Go to this page and download the library: Download aicrion/qrcode 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/ */

    

aicrion / qrcode example snippets


use Aicrion\QRCode\QRCode;

$qr = QRCode::make();

// Save directly to a file
$qr->generateToFile('https://github.com/aicrion/qrcode-php', __DIR__ . '/qrcode.png');

// Get raw bytes
$bytes = $qr->generate('Hello World');

// Get a data URI (ideal for <img src="...">)
$uri = $qr->generateDataUri('Hello World');

use Aicrion\QRCode\QRCode;

$qr = QRCode::make();

$result = $qr->readFromPath(__DIR__ . '/qrcode.png');

echo $result->content; // decoded text

$qr->read(__DIR__ . '/qrcode.png');      // file path
$qr->read('https://example.com/qr.png'); // URL
$qr->read($base64DataUri);               // Base64 data URI
$qr->read($binaryImageString);           // raw binary

use Aicrion\QRCode\Enums\{OutputFormat, ErrorCorrectionLevel};
use Aicrion\QRCode\ValueObjects\{QRCodeOptions, Color};

$options = (new QRCodeOptions())
    ->withSize(600)
    ->withFormat(OutputFormat::SVG)
    ->withColors(Color::fromHex('#0f172a'), Color::white())
    ->withErrorCorrection(ErrorCorrectionLevel::HIGH)
    ->withLogo(__DIR__ . '/assets/logo.png', sizeRatio: 22);

$svg = QRCode::make()->generate('https://aicrion.dev', $options);

$qr->generateToFile('سلام دنیا! این یک کد کیوآر فارسی است.', __DIR__ . '/farsi-qr.png');

$result = $qr->readFromPath(__DIR__ . '/farsi-qr.png');
echo $result->content; // سلام دنیا! این یک کد کیوآر فارسی است.

use Aicrion\QRCode\Enums\WifiEncryption;

$qr->generateWifi(ssid: 'MyNetwork', password: 'secret123', encryption: WifiEncryption::WPA);
$qr->generateContact(firstName: 'Hadi', lastName: 'Akbarzadeh', phone: '+989120000000', email: '[email protected]');
$qr->generatePhone('+989120000000');
$qr->generateSms('+989120000000', 'Hello!');
$qr->generateEmail('[email protected]', 'Subject', 'Body');
$qr->generateGeo(35.6892, 51.3890);
$qr->generateUrl('https://github.com/aicrion/qrcode-php');

$result = $qr->readFromPath(__DIR__ . '/wifi.png');

echo $result->type->value; // "wifi"
print_r($result->parsed);  // ['ssid' => 'MyNetwork', 'password' => 'secret123', ...]

use Aicrion\QRCode\ValueObjects\{QRCodeOptions, Color};

$options = (new QRCodeOptions())->withColors(Color::fromHex('#0f172a'), Color::fromHex('#f8fafc'));

$qr->generateToFile('https://aicrion.dev', __DIR__ . '/branded-qr.png', $options);

// Reading colored QR codes works exactly like black & white ones
$result = $qr->readFromPath(__DIR__ . '/branded-qr.png');
echo $result->content;

use Aicrion\QRCode\Exceptions\QRCodeException;

try {
    $result = QRCode::make()->readFromPath('/missing/file.png');
} catch (QRCodeException $e) {
    // Handles InvalidSourceException, DecodingException, GenerationException, etc.
    logger()->error($e->getMessage());
}