bnomei / kirby3-qrcode
生成二维码。内置的面板字段将显示二维码,并在点击时触发下载。
2.2.0
2023-09-27 08:41 UTC
Requires
- php: >=8.0
- endroid/qr-code: ^4.3
- getkirby/composer-installer: ^1.1
Requires (Dev)
- getkirby/cms: ^3.2
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^9.5
Suggests
- bnomei/kirby3-janitor: Provides tools to add a custom Panel button to trigger download of ICS/CSV/QRCode Files
This package is auto-updated.
Last update: 2024-09-23 16:59:53 UTC
README
轻松生成二维码。内置的面板字段将显示二维码,并在点击时触发下载。
商业用途
支持开源!
此插件是免费的,但如果您将其用于商业项目,请考虑赞助我或捐款。
如果我的工作帮助您赚到一些钱,我认为我也许应该得到一点回报,对吗?
友善。分享一点。谢谢。
- Bruno
安装
- 将 master.zip 解压缩到
site/plugins/kirby3-qrcode
文件夹或 git submodule add https://github.com/bnomei/kirby3-qrcode.git site/plugins/kirby3-qrcode
或composer require bnomei/kirby3-qrcode
用例
原始PHP
$qrcodeObject = new \Bnomei\QRCode([ 'Text' => 'https://github.com/bnomei/kirby3-qrcode', ]); echo $qrcodeObject->html('qrcode-plugin.png');
页面方法
site/templates/default.php
// base64 encoded png image tag // with $page->url() as Text echo $page->qrcode()->html( $page->slug() . '.png' // image format detected from extension );
将文件保存为页面对象中的文件
在保存时,您除了文件名外,还可以为文件提供额外的内容数据或使用额外的函数参数强制覆盖(这将更改UUID和媒体哈希)。逻辑将使用当前登录用户或代表 kirby
来创建文件。
site/templates/default.php
$qrcodeFilename = $page->slug() . '.png'; $file = $page->file($qrcodeFilename); if (!$file) { // short version $file = $page->qrcode()->save( $qrcodeFilename ); // with all params $file = $page->qrcode()->save( $qrcodeFilename, 'myfiletemplate', // or null [/* my content data array */], true // force overwrite ); } echo $file; // outputs a img tag
为什么不默认覆盖文件?让我们假设您想通过电子邮件发送二维码。那么您不希望稍后覆盖,因为这会更改媒体哈希,并破坏电子邮件中发送的图像的URL。
我不能使用base-64编码内嵌二维码吗?技术上可以,但几乎没有任何电子邮件客户端会渲染它。
触发下载
site/templates/default.qr.php
$page->qrcode()->download( $page->slug() . '.png' );
对生成的图像进行进一步自定义
高级选项可以设置。
echo $page->qrcode([ 'margin' => 10, 'encoding' => 'UTF-8', 'foregroundColor' => new \Endroid\QrCode\Color\Color(0, 0, 0), 'backgroundColor' => new \Endroid\QrCode\Color\Color(255, 255, 255), 'labelText' => 'Scan the code', 'logoPath' => __DIR__.'/../assets/images/getkirby.png', 'size' => 200, ])->html( $page->slug() . '.png' );
提示:您可以将这些选项数组移动到前端渲染的配置值中或重复使用
面板字段:页面/文件的URL作为二维码
将字段添加到蓝图。这将显示二维码PNG图像,并在点击时触发文件的下载。
site/blueprints/default.yml
fields: # current page qrcode: qrcode qrcode2: type: qrcode title: Text below image # title with query qrcode3: type: qrcode title: "{{ page.title }}" # custom url qrcode4: type: qrcode title: Panel Url of {{ page.title }} url: "{{ page.panel.url }}" # custom url and custom filename with | qrcode4: type: qrcode title: Issue 7 url: "https://github.com/bnomei/kirby3-qrcode/issues/7|Issue 7"
您可以在配置文件中定义二维码字段如何显示图像 在面板内部 的选项。例如
site/config/config.php
<?php return [ // other options... // set values for qrcode inside the panel 'bnomei.qrcode.field' => [ 'foregroundColor' => new \Endroid\QrCode\Color\Color(126, 154, 191), 'backgroundColor' => new \Endroid\QrCode\Color\Color(239, 239, 239), 'size' => 128, 'margin' => 0, ], ];
您也可以这样在前端代码中重复使用
$page->qrcode(option('bnomei.qrcode.field', []))->download( $page->slug() . '.png' );
依赖项
免责声明
此插件提供“原样”且无任何保证。自行承担使用风险,并在在生产环境中使用之前始终自行测试。如果您发现任何问题,请创建新问题。
许可证
不建议在任何推广种族主义、性别歧视、同性恋恐惧症、动物虐待、暴力或任何其他形式仇恨言论的项目中使用此插件。
鸣谢
基于