我从Zend条形码对象生成了GD图像资源。如何在树枝模板中呈现此资源?
答案 0 :(得分:1)
您好,这段代码可以解决问题:
require 'vendor/autoload.php';
use Zend\Barcode\Barcode;
// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
// No required options
$rendererOptions = array();
$image = Barcode::draw(
'code39',
'image',
$barcodeOptions,
$rendererOptions
);
// By default, imagepng will print on the output file your image.
// Here we play around with ob_* to catch this output and store it on $contents variable.
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
// Save as test.png your current barcode.
file_put_contents('test.png', $contents);
// Display img tag with base64 encoded barcode.
echo '<img src="data:image/png;base64,'.base64_encode($contents).'">';
说明:
imagejpeg()
或imagepng()
接收gd image ressource作为参数并打印。在这里,我们使用ob_*
函数来捕获此输出而不是打印输出。然后,您可以使用此数据执行任何操作。在我的代码上,我完成了两种可能性: