привет, этот код поможет:
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_*
, чтобы зафиксировать этот вывод в переменной, а не распечатать его. Тогда вы можете делать с этими данными все, что захотите. В моем коде я сделал обе возможности: