bitandblack/qr-code

生成二维码

资助包维护!
支持我

0.4.0 2023-05-19 20:33 UTC

This package is auto-updated.

Last update: 2024-09-24 22:17:51 UTC


README

PHP from Packagist Codacy Badge Latest Stable Version Total Downloads License

二维码

这个库可以生成二维码。它基于 Jeroen van den Enden 的 endroid/qr-code,但不同之处在于使用了 bitandblack/colors 来允许不同的颜色空间。

安装

这个库是为与 Composer 一起使用而制作的。通过运行 $ composer require bitandblack/qr-code 将其添加到您的项目中。

使用方法

<?php 

use BitAndBlack\QrCode\QrCode;

$qrCode = new QrCode('Hello World!');

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

高级使用

<?php 

use Color\Value\RGB;
use BitAndBlack\QrCode\ErrorCorrectionLevel;
use BitAndBlack\QrCode\LabelAlignment;
use BitAndBlack\QrCode\QrCode;
use BitAndBlack\QrCode\Response\QrCodeResponse;

$qrCode = new QrCode('Hello World!');
$qrCode
    ->setSize(300)    
    ->setWriterByName('png')    
    ->setMargin(10)    
    ->setEncoding('UTF-8')    
    ->setErrorCorrectionLevel(
        new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH())
    )    
    ->setForegroundColor(
        new RGB(0, 0, 0)
    )    
    ->setBackgroundColor(
        new RGB(255, 255, 255)
    )    
    ->setLabel('Scan the code', 16, 'SomeFont.otf', LabelAlignment::CENTER)    
    ->setLogoPath('SomeLogo.png')    
    ->setLogoSize(150, 200)    
    ->setRoundBlockSize(true)    
    ->setValidateResult(false)    
    ->setWriterOptions([
        'exclude_xml_declaration' => true
    ])
;

// Directly output the QR code
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

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

// Create a response object
$response = new QrCodeResponse($qrCode);

帮助

如果您有任何问题,请通过 hello@bitandblack.com 联系我们。

有关 Bit&Black 的更多信息可以在 www.bitandblack.com 找到。