PHP code example of zoujingli / qrcode

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

    

zoujingli / qrcode example snippets


$result = \Endroid\QrCode\Builder\Builder::create()
    ->writer(new \Endroid\QrCode\Writer\PngWriter())
    ->writerOptions([])
    ->data('Custom QR code contents')
    ->encoding(new \Endroid\QrCode\Encoding\Encoding('UTF-8'))
    // ->errorCorrectionLevel(new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh())
    ->size(300)
    ->margin(10)
    // ->roundBlockSizeMode(new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin())
    
    //// 设置二维码 LOGO 图片
    // ->logoPath(__DIR__.'/icon.png')
    
    //// 设置二维码标签字段
    // ->labelText('This is the label')
    // ->labelFont(new \Endroid\QrCode\Label\Font\OpenSans(20))
    // ->labelAlignment(new \Endroid\QrCode\Label\Alignment\LabelAlignmentCenter())
    ->validateResult(false)
    ->build();

// 输出 Base64 图片
echo $result->getDataUri();


$result = \Endroid\QrCode\Builder\Builder::create()
    ->writer(new \Endroid\QrCode\Writer\PngWriter())
    ->writerOptions([])
    ->data('Custom QR code contents')
    ->encoding(new \Endroid\QrCode\Encoding\Encoding('UTF-8'))
    ->errorCorrectionLevel(new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh())
    ->size(300)
    ->margin(10)
    ->roundBlockSizeMode(new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin())
    ->logoPath(__DIR__.'/icon.png')
    ->labelText('This is the label')
     ->labelFont(new \Endroid\QrCode\Label\Font\OpenSans(20))
    ->labelAlignment(new \Endroid\QrCode\Label\Alignment\LabelAlignmentCenter())
    ->validateResult(false)
    ->build();

$writer = new \Endroid\QrCode\Writer\PngWriter();

// Create QR code
$qrCode = \Endroid\QrCode\QrCode::create('Life is too short to be generating QR codes')
    ->setEncoding(new \Endroid\QrCode\Encoding\Encoding('UTF-8'))
    ->setErrorCorrectionLevel(new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow())
    ->setSize(300)
    ->setMargin(10)
    ->setRoundBlockSizeMode(new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin())
    ->setForegroundColor(new \Endroid\QrCode\Color\Color(0, 0, 0))
    ->setBackgroundColor(new \Endroid\QrCode\Color\Color(255, 255, 255));

// Create generic logo
$logo = \Endroid\QrCode\Logo\Logo::create(__DIR__.'/icon.png')->setResizeToWidth(50);

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

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

// Validate the result
$writer->validateResult($result, 'Life is too short to be generating QR codes');


// 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 Endroid\QrCode\Writer\SvgWriter;

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