二维码Qrcode Library

壤驷彦
2023-12-01

二维码Qrcode Library

插件官网:https://qrcode-library.readthedocs.io/en/latest/#yii2

composer 安装

php composer.phar require 2amigos/qrcode-library:~1.1

or add

 "2amigos/qrcode-library": "^2.0"

执行 composer update

用法

  1. 在控制器中使用
use Da\QrCode\QrCode;

$qrCode = (new QrCode('This is my text'))
    ->setSize(250)
    ->setMargin(5)
    ->useForegroundColor(51, 153, 255);

// now we can display the qrcode in many ways
// saving the result to a file:

$qrCode->writeFile(__DIR__ . '/code.png'); // writer defaults to PNG when none is specified

// display directly to the browser 
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();
exit(); //不能掉,否则系统报头信息已发送异常
?> 


//第二种显示
<?php 
// or even as data:uri url
echo '<img src="' . $qrCode->writeDataUri() . '">';
exit(); //不能掉,否则系统报头信息已发送异常
?>

或者代码都写在一起

use Da\QrCode\QrCode;

$qrCode = (new QrCode('This is my text'))
    ->setSize(250)
    ->setMargin(5)
    ->useForegroundColor(51, 153, 255);

$imgDisplay = false;
if ($imgDisplay ) {
	echo '<img src="' . $qrCode->writeDataUri() . '">';
} else {
	// now we can display the qrcode in many ways
	// saving the result to a file:
	$qrCode->writeFile(__DIR__ . '/code.png'); // writer defaults to PNG when none is specified
	// display directly to the browser 
	header('Content-Type: '.$qrCode->getContentType());
	echo $qrCode->writeString();
}

exit();
?> 
  1. 设置为插件 QrCodeComponent

配置

// in components config of Yii2 app

'components' => [
// ... 
    'qr' => [
        'class' => '\Da\QrCode\Component\QrCodeComponent',
        'label' => '2amigos consulting group llc',
        'size' => 500 // big and nice :D
        // ... you can configure more properties of the component here
    ]
// ...
]

使用

$qr = Yii::$app->get('qr');

Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', $qr->getContentType());

return $qr
    ->setText('https://2amigos.us')
    ->setLabel('2amigos consulting group llc')
    ->writeString();
  1. 做成独立的动作
/ On our controller  
public function actions()
{
    return [
        'qr' => [
            'class' => QrCodeAction::className(),
            'text' => 'https://2amigos.us',
            'param' => 'v',
            'commponent' => 'qr' // if configured in our app as `qr` 
        ]
    ];
}

在前端页面可以直接调用控制器

<img src="<?= Url::to(['controller/qr', 'v' => 'Hey! This is some content on my QrCode!']) ?>" />

<!-- this will display https://2amigos.us (default text) -->
<img src="<?= Url::to(['controller/qr']) ?>" />

复杂的用法

// A label can be a string OR a Da\Contracts\LabelInterface instance. 
// Using the instance, we will have more control on how do we want the label to be displayed.
// Immutability also applies to this class! 
$label = (new Label('2amigos'))
    ->setFont(__DIR__ . '/../resources/fonts/monsterrat.otf')
    ->setFontSize(12);

$qrCode = (new QrCode('https://2amigos.us'))
    ->setLogo(__DIR__ . '/data/logo.png')
    ->setForegroundColor(51, 153, 255)
    ->setBackgroundColor(200, 220, 210)
    ->setEncoding('UTF-8')
    ->setErrorCorrectionLevel(ErrorCorrectionLevelInterface::HIGH)
    ->setLogoWidth(60)
    ->setSize(300)
    ->setMargin(5)
    ->setLabel($label);

$qrCode->writeFile(__DIR__ . '/codes/my-code.png');
 类似资料: