привет, этот код делает свое дело:
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 изображение ресурса и распечатайте его. Здесь мы поэкспериментируем с функцией ob_*
, чтобы записать этот вывод в переменную вместо того, чтобы печатать его. Затем вы можете делать все, что вы хотите с этими данными. В моем коде я сделал обе возможности:
- Сохраните его в статическом файле.
- Напечатайте его как изображение base64 внутри моего html.