Image 如何在细枝模板中显示图像GD资源

Image 如何在细枝模板中显示图像GD资源,image,symfony,twig,gd,Image,Symfony,Twig,Gd,我已经从Zend Barcode对象生成了一个GD图像资源。如何在细枝模板中呈现此资源?您好,此代码执行以下操作: require 'vendor/autoload.php'; use Zend\Barcode\Barcode; // Only the text to draw is required $barcodeOptions = array('text' => 'ZEND-FRAMEWORK'); // No required options $rendererOptions

我已经从Zend Barcode对象生成了一个GD图像资源。如何在细枝模板中呈现此资源?

您好,此代码执行以下操作:

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).'">';
需要“vendor/autoload.php”;
使用Zend\Barcode\Barcode;
//只需要绘制文本
$barcodeOptions=array('text'=>'ZEND-FRAMEWORK');
//没有必要的选择
$renderoptions=array();
$image=条形码::绘图(
‘代码39’,
“图像”,
$barcodeOptions,
$renderoptions
);
//默认情况下,imagepng将在输出文件上打印您的图像。
//在这里,我们使用ob_*捕获此输出并将其存储在$contents变量中。
ob_start();
imagepng($image);
$contents=ob_get_contents();
ob_end_clean();
//将当前条形码另存为test.png。
文件内容('test.png',$contents);
//显示带有base64编码条形码的img标签。
回声';
解释:

imagejpeg()
imagepng()
作为参数gd image resource接收并打印它。这里我们使用
ob.*
函数来捕获变量中的输出,而不是打印它。然后你可以用这些数据做任何你想做的事情。在我的代码中,我已经完成了两种可能性:

  • 保存在静态文件中
  • 在我的html中直接将其打印为base64图像