picqer / php-barcode-generator

Barcode generator in PHP that is easy to use, non-bloated and framework independent.
GNU Lesser General Public License v3.0
1.67k stars 402 forks source link

Text at the bottom of the barcode #162

Closed YunigeCDilip closed 2 years ago

YunigeCDilip commented 2 years ago

Hello,

Is there any way I can show the actual code of the generated barcode or a custom text at the bottom part of the generated barcode image?

casperbakker commented 2 years ago

https://github.com/picqer/php-barcode-generator/issues/130, https://github.com/picqer/php-barcode-generator/issues/125, https://github.com/picqer/php-barcode-generator/issues/111 all cover this, in essence, it's not supported currently.

yakhyomus commented 1 year ago
$generator = new BarcodeGeneratorPNG();
return Html::img('data:image/jpeg;base64, '. base64_encode($generator->getBarcode('012345678911121'), $generator::TYPE_EAN_13)), ['width' => '120', 'height' => '50']).
'<br /> 012 345678 911121';

test

KamranBiglari commented 1 month ago
$barcode = '1000002';
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$image = $generator->getBarcode($barcode, $generator::TYPE_CODE_128, height: 60, foregroundColor: [0, 0, 0]);

// use imagick 
$im = new \Imagick();

$im->readImageBlob($image);
$im->setImageFormat("png");

// Get the original image dimensions
$width = $im->getImageWidth();
$height = $im->getImageHeight();

// Create a new ImagickDraw object
$textHeight = 40; // For example, 40 pixels for the text area
$extendWidth = 10;
$extendedHeight = $height + $textHeight;
$extendedWidth = $width + $extendWidth;

$extendedImage = new \Imagick();
$extendedImage->newImage($extendedWidth, $extendedHeight, new \ImagickPixel('white'));
$extendedImage->setImageFormat("png");

// Composite the original image onto the new image
$extendedImage->compositeImage($im, \Imagick::COMPOSITE_OVER, ($extendWidth? ($extendWidth/2): 0), 0);

// Create a new ImagickDraw object
$draw = new \ImagickDraw();
$draw->setFillColor('black');
$draw->setFont('Helvetica');
$draw->setFontSize(20);

$draw->setGravity(\Imagick::GRAVITY_SOUTH); // Position the text at the bottom

// Add the text to the image
$extendedImage->annotateImage($draw, 0, 0, 0, $barcode);

// Save the image
$extendedImage->setImageFormat('png');