PHP code example of attestra / qr-code

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

    

attestra / qr-code example snippets


use Attestra\QrCode\Builder\Builder;
use Attestra\QrCode\Encoding\Encoding;
use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Attestra\QrCode\Label\Alignment\LabelAlignmentCenter;
use Attestra\QrCode\Label\Font\NotoSans;
use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Attestra\QrCode\Writer\PngWriter;

$result = Builder::create()
    ->writer(new PngWriter())
    ->writerOptions([])
    ->data('Custom QR code contents')
    ->encoding(new Encoding('UTF-8'))
    ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
    ->size(300)
    ->margin(10)
    ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->logoPath(__DIR__.'/assets/symfony.png')
    ->labelText('This is the label')
    ->labelFont(new NotoSans(20))
    ->labelAlignment(new LabelAlignmentCenter())
    ->build();

use Attestra\QrCode\Color\Color;
use Attestra\QrCode\Encoding\Encoding;
use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Attestra\QrCode\QrCode;
use Attestra\QrCode\Label\Label;
use Attestra\QrCode\Logo\Logo;
use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Attestra\QrCode\Writer\PngWriter;

$writer = new PngWriter();

// Create QR code
$qrCode = QrCode::create('Data')
    ->setEncoding(new Encoding('UTF-8'))
    ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
    ->setSize(300)
    ->setMargin(10)
    ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->setForegroundColor(new Color(0, 0, 0))
    ->setBackgroundColor(new Color(255, 255, 255));

// Create generic logo
$logo = Logo::create(__DIR__.'/assets/symfony.png')
    ->setResizeToWidth(50);

// Create generic label
$label = Label::create('Label')
    ->setTextColor(new Color(255, 0, 0));

$result = $writer->write($qrCode, $logo, $label);


// Directly output the QR code
header('Content-Type: '.$result->getMimeType());
echo $result->getString();

// Save it to a file
$result->saveToFile(__DIR__.'/qrcode.png');

// Generate a data URI to 

use Attestra\QrCode\Writer\SvgWriter;

$builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);