acelaya/zf2-acqrcode

此包已被弃用且不再维护。没有建议的替代包。

基于Endroid QR code的Zend Framework 2 QR码生成模块

v1.0.0 2017-02-21 19:08 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:37:42 UTC


README

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

这个Zend Framework模块允许您通过使用Endroid QR Code库轻松生成QR码,该库由Endroid提供。

它基于EndroidQrCodeBundle Symfony包。

安装

首选的安装方法是使用composer。只需使用composer要求此包

composer require acelaya/zf2-acqrcode

并使用composer更新您的依赖项php composer.phar update

之后,只需将模块添加到您application.config.php文件中启用的模块列表即可

'modules' => array(
    'Application',
    'ZfcUser',
    'ZfcBase',
    'Acelaya\QrCode' // <-- This line will do the job
)

使用

模块可以用多种方式使用。它包括一个路由,该路由指向一个控制器,该控制器以响应的形式返回QR码图像。

您可以使用该路由并通过传递四个简单的参数来创建自己的QR码。要编码的消息、图像扩展名(jpeg、png或gif)、图像大小和填充。

在您的视图模板中,您可以这样做。

<img src="<?php echo $this->url('acelaya-qrcode', ['message' => 'This is a QR code example']) ?>">
<img src="<?php echo $this->url('acelaya-qrcode', ['message' => 'Another QR code', 'extension' => 'gif']) ?>">
<img src="<?php echo $this->url('acelaya-qrcode', ['message' => 'Something bigger', 'extension' => 'png', 'size' => '600']) ?>">
<img src="<?php echo $this->url('acelaya-qrcode', ['message' => 'Custom padding code', 'extension' => 'png', 'size' => '500', 'padding' => '20']) ?>">

默认情况下,扩展名为jpeg,大小为200,填充为10。必须提供消息。

可以通过将文件vendor/acelaya/zf2-acqrcode/config/qr_code.global.php.dist复制到config/autoload/qr_code.global.php并替换那里定义的任何参数来自定义默认值。

视图助手

为了简化这个任务,提供了一个视图助手。使用它,您可以直接渲染指向该QR码的img标签或直接获取像使用url视图助手一样使用的组装路由。

此视图助手的所有方法都按相同的顺序获取4个参数;消息、扩展名、大小和最后是填充。

<?php echo $this->qrCode()->renderImg('The message', 'png', 300, 20); ?>

这将生成此图像

<img src="/qr-code/generate/The%20message/png/300/20">

如果您需要在img标签中添加更多属性,如果最后一个参数是数组,它将被视为属性及其值。

<?php echo $this->qrCode()->renderImg('The message', 'png', ['title' => 'This is a cool QR code', 'class' => 'img-thumbnail']); ?>

这将生成此图像

<img src="/qr-code/generate/The%20message/png" title="This is a cool QR code" class="img-thumbnail">

您还可以渲染一个base64编码的图像,而不是使用内部路由。这在需要渲染URL时非常有用,这是最常见的用例之一。

<?php echo $this->qrCode()->renderBase64Img('http://www.alejandrocelaya.com/skills', 'gif', 350, 5, ['title' => 'This is a cool QR code', 'class' => 'img-thumbnail']) ?>

这将生成此图像

<img src="data:image/gif;base64,12345...AaBbCcDd...YyZz" title="This is a cool QR code" class="img-thumbnail">

在两种情况下,如果您的应用程序是XHTML,图像标签将自动使用/>关闭。如果是HTML5,它将只使用>关闭

如果您只需要获取路由,此视图助手类似于这样使用时是url视图助手的快捷方式。

<div>
    <h2>This is a nice title</h2>
    <div style="background: url(<?php echo $this->qrCode('The message', 'png') ?>);"></div>
</div>

如果您需要使用其他路由选项,可以这样做。

<div>
    <h2>This is a nice title</h2>
    <div style="background: url(<?php echo $this->qrCode()->setRouteOptions(['force_canonical' => true])->assembleRoute('The message', 'png') ?>);"></div>
</div>

服务

如果该视图助手不适合您的需求,或者您需要使用QR码执行其他操作,例如将它们保存到某些存储系统中,您可以直接使用QrCodeService

它已经使用键 Acelaya\QrCode\Service\QrCodeService 进行注册,以便您将其注入到自己的任何服务中。

返回的对象是实现了 Acelaya\QrCode\Service\QrCodeServiceInterface 接口的 Acelaya\QrCode\Service\QrCodeService

/** @var \Zend\ServiceManager\ServiceLocatorInterface $sm */
$service = $sm->get('Acelaya\QrCode\Service\QrCodeService');
$content = $service->getQrCodeContent('http://www.alejandrocelaya.com/contact', 'png');

// Save the image to disk
file_put_contents('/path/to/file.png', $content);

getQrCodeContent 方法接受四个参数,顺序与 ($message$extension$size$padding) 相同,其中第一个参数是唯一必填的。

此外,您可以将 Zend\Controller\Plugin\Params 对象作为第一个参数传递,以便从路由参数中获取信息,忽略其他传递的参数。

// In a controller...

/** @var \Zend\ServiceManager\ServiceLocatorInterface $sm */
$service = $sm->get('Acelaya\QrCode\Service\QrCodeService');
$content = $service->getQrCodeContent($this->params());

// Save the image to disk
file_put_contents('/path/to/file.png', $content);

测试

此模块包含所有单元测试,并遵循依赖注入和抽象,以便您能够测试依赖其类的任何组件。