xsuchy09 / php-qrcode
QR码生成器。PHP 7.2+
Requires
- php: ^7.2
- ext-gd: *
- ext-json: *
- ext-mbstring: *
- chillerlan/php-settings-container: ^1.1
Requires (Dev)
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2024-08-29 05:49:43 UTC
README
从 chillerlan/php-qrcode 分支。
基于 Kazuhiko Arase 的 实现 的 PHP7.2+ QR码库,已命名空间、整理、改进及其他内容。
文档
要求
- PHP 7.2+
ext-gd
,ext-json
,ext-mbstring
- 可选
ext-imagick
安装
需要 composer
只需运行
composer require xsuchy09/php-qrcode
或编辑您的 composer.json (注意:将 dev-master
替换为 版本范围)
{ "require": { "php": "^7.2", "xsuchy09/php-qrcode": "dev-master" } }
手动安装
从 master 或 发布 下载所需版本的包,并将其内容解压到您的项目文件夹中。之后
- 运行
composer install
以安装所需的依赖项并生成/vendor/autoload.php
。 - 如果您使用自定义自动加载器,将命名空间
xsuchy09\QRCode
指向包的src
文件夹
成功了!
用法
我们想要将这个移动验证器的 URI 编码为 QR码图像
$data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net'; //quick and simple: echo '<img src="'.(new QRCode)->render($data).'" alt="QR Code" />';
等等,那是啥?请再说一遍,慢一点!
高级用法
好的,一步一步来。首先你需要一个 QRCode
实例,它可以用一个 QROptions
(或相应的 SettingsContainerInterface
对象)作为唯一参数进行可选调用。
$options = new QROptions([ 'version' => 5, 'outputType' => QRCode::OUTPUT_MARKUP_SVG, 'eccLevel' => QRCode::ECC_L, ]); // invoke a fresh QRCode instance $qrcode = new QRCode($options); // and dump the output $qrcode->render($data); // ...with additional cache file $qrcode->render($data, '/path/to/file.svg');
如果你只想获取原始 QR码矩阵,调用 QRCode::getMatrix()
- 此方法也由 QRCode::render()
内部调用。另请参阅 自定义输出模块。
$matrix = $qrcode->getMatrix($data); foreach($matrix->matrix() as $y => $row){ foreach($row as $x => $module){ // get a module's value $value = $module; $value = $matrix->get($x, $y); // boolean check a module if($matrix->check($x, $y)){ // if($module >> 8 > 0) // do stuff, the module is dark } else{ // do other stuff, the module is light } } }
自定义模块值
之前版本的 QRCode
只包含布尔矩阵值,只能确定模块是暗还是亮。现在你可以区分矩阵的不同部分,即 QR码规范中所需的不同模式,并使用它们以不同的方式。
暗值是模块(亮)值左移 8 位:$value = $M_TYPE << ($bool ? 8 : 0);
,其中 $M_TYPE
是 QRMatrix::M_*
常量之一。你可以显式检查类型的值...
// for true (dark) $value >> 8 === $M_TYPE; //for false (light) $value === $M_TYPE;
...或者你可以进行松散检查,忽略模块值
// for true $value >> 8 > 0; // for false $value >> 8 === 0
另请参阅 QRMatrix::set()
、QRMatrix::check()
和 QRMatrix
常量。
为了将值映射并正确渲染给定 QROutputInterface
的模块,需要覆盖默认值
$options = new QROptions; // for HTML, SVG and ImageMagick $options->moduleValues = [ // finder 1536 => '#A71111', // dark (true) 6 => '#FFBFBF', // light (false) // alignment 2560 => '#A70364', 10 => '#FFC9C9', // timing 3072 => '#98005D', 12 => '#FFB8E9', // format 3584 => '#003804', 14 => '#00FB12', // version 4096 => '#650098', 16 => '#E0B8FF', // data 1024 => '#4A6000', 4 => '#ECF9BE', // darkmodule 512 => '#080063', // separator 8 => '#AFBFBF', // quietzone 18 => '#FFFFFF', ]; // for the image output types $options->moduleValues = [ 512 => [0, 0, 0], // ... ]; // for string/text output $options->moduleValues = [ 512 => '#', // ... ];
自定义 QROutputInterface
与其让您的代码膨胀,您可以通过扩展 QROutputAbstract
简单地创建自己的输出接口。看看内置的输出模块:内置输出模块。
class MyCustomOutput extends QROutputAbstract{ // inherited from QROutputAbstract protected $matrix; // QRMatrix protected $moduleCount; // modules QRMatrix::size() protected $options; // MyCustomOptions or QROptions protected $scale; // scale factor from options protected $length; // length of the matrix ($moduleCount * $scale) // ...check/set default module values (abstract method, called by the constructor) protected function setModuleValues():void{ // $this->moduleValues = ... } // QROutputInterface::dump() public function dump(string $file = null):string{ $output = ''; for($row = 0; $row < $this->moduleCount; $row++){ for($col = 0; $col < $this->moduleCount; $col++){ $output .= (int)$this->matrix->check($col, $row); } } return $output; } }
如果您的输出模块需要额外的设置,只需扩展 QROptions
...
class MyCustomOptions extends QROptions{
protected $myParam = 'defaultValue';
// ...
}
...或者使用更灵活的方法 SettingsContainerInterface
。
trait MyCustomOptionsTrait{ protected $myParam = 'defaultValue'; // ... }
设置选项
$myOptions = [ 'version' => 5, 'eccLevel' => QRCode::ECC_L, 'outputType' => QRCode::OUTPUT_CUSTOM, 'outputInterface' => MyCustomOutput::class, // your custom settings 'myParam' => 'whatever value', ]; // extends QROptions $myCustomOptions = new MyCustomOptions($myOptions); // using the SettingsContainerInterface $myCustomOptions = new class($myOptions) extends SettingsContainerAbstract{ use QROptionsTrait, MyCustomOptionsTrait; };
然后您可以使用自定义模块调用 QRCode
...
(new QRCode($myCustomOptions))->render($data);
...或者手动调用 QROutputInterface
。
$qrOutputInterface = new MyCustomOutput($myCustomOptions, (new QRCode($myCustomOptions))->getMatrix($data)); //dump the output, which is equivalent to QRCode::render() $qrOutputInterface->dump();
API
QRCode
方法
QRCode
常量
QROptions
属性
QRMatrix
方法
QRMatrix
常量
注意
二维码编码器,特别是掩码模式测试的子程序,在矩阵尺寸增大时可能会造成高CPU负载。您可以通过选择快速输出模块(如 OUTPUT_IMAGE_*
)并手动设置掩码模式(这可能导致二维码不可读)来避免部分负载。哦,别忘了对用户输入进行清理!
免责声明!
我对熔化的CPU、误导的应用程序、失败的登录等不承担责任。自行承担风险!
商标声明
“QR Code”一词是 DENSO WAVE INCORPORATED 的注册商标。
http://www.denso-wave.com/qrcode/faqpatent-e.html