PHP类库生成二维码

测试OK!先下载类库:

composer require endroid/qr-code

基础使用,生成二维码

$link = 'https://www.baidu.com';
header('Content-Type: image/png');
$qrCode = new QrCode($link);
echo $qrCode->writeString();

保存为文件(注意file_name的路径)

$qrCode->writeFile($file_name);

在基础使用上可以加入很多参数来美化二维码,如色彩、LOGO、大小等等:

$qrCode->setSize(300);

// Set advanced options
$qrCode->setWriterByName('png');
$qrCode->setMargin(10);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER);
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
$qrCode->setLogoSize(150, 200);
$qrCode->setRoundBlockSize(true);
$qrCode->setValidateResult(false);
$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

自己可去尝试使用。

猜你喜欢

转载自blog.csdn.net/myd0512/article/details/88169876
今日推荐