我使用了一个扩展在 yii2 PHP 中生成二维码 (https://github.com/tecnickcom/tc-lib-barcode)
通过文档,我遵循了这一点。
// instantiate the barcode class
$barcode = new \Com\Tecnick\Barcode\Barcode();
// generate a barcode
$bobj = $barcode->getBarcodeObj(
'QRCODE,H', // barcode type and additional comma-separated parameters
'https://tecnick.com', // data string to encode
-4, // bar width (use absolute or negative value as multiplication factor)
-4, // bar height (use absolute or negative value as multiplication factor)
'black', // foreground color
array(-2, -2, -2, -2) // padding (use absolute or negative values as multiplication factors)
)->setBackgroundColor('white'); // background color
// output the barcode as HTML div (see other output formats in the documentation and examples)
echo $bobj->getHtmlDiv();
我有一个生成二维码的要求,并且我们必须通过电子邮件发送该二维码。
电子邮件代码已准备就绪,因为我们已映射 图像标签来显示图像。如果我们传递任何存储的图像路径,则图像将显示并且电子邮件代码工作正常。
但我在电子邮件中显示二维码时遇到了挑战。我已经通过 Postman 执行了上述代码,并且显示了二维码,但电子邮件上没有显示。
$bobj->getHtmlDiv();
除了这个功能我也尝试过。
$bobj->getPng();
$bobj->getSvg();
但不工作
谁能告诉我们如何渲染它?
或者有其他易于使用和实现的扩展吗?
尝试过功能
$result = $bobj->getHtmlDiv();
$result = $bobj->getPng();
$result = $bobj->getSvg();
但无法通过电子邮件发送。
还尝试在文件对象中传递以下代码的结果。静态图像仅出现在邮递员中,而不是文件对象中。
file_get_contents()
也尝试过,但无法解决。
getPngData()
方法而不是getPng()
。这是因为getPng()
不只是简单地返回图像数据,而是设置标题并直接输出结果。
使用
getPngData()
获取图像数据后,您可以使用 yii\mail\MessageInterface::embedContent()
将图像嵌入到邮件中,而无需将其保存在任何地方。
假设您已经在
$bobj
属性中拥有条形码对象,那么在邮件模板中您可以执行以下操作:
<img src="<?= $message->embedContent(
$bobj->getPngData(),
[
'fileName' => 'barcode.png',
'contentType' => 'image/png',
]
); ?>" alt="barcode">