php-barcode-generator
php-barcode-generator copied to clipboard
Barcode Generated looks weird JPG,PNG
Hey guys, does anyone ever meet this problem?
the barcode generated looks weird
this is the code to generate it
$generator = new \Picqer\Barcode\BarcodeGeneratorJPG();
file_put_contents('media/barcode.jpg', $generator->getBarcode($invoice->getIncrementId(), $generator::TYPE_CODE_39, 1, 50, [0, 0, 0]));
It works normal on local but works weird on cloud env. Please assist
I encountered this issue as well. Just keeping the width factor >=2 works.
What is the value that you put into the barcode? In the example, what is coming out of $invoice->getIncrementId()?
@casperbakker A string of random numbers like "0123456789" should technically work for reproducing this issue. The issue really was that the thinner lines did not render for some reason, only the thicker ones, although on my local environment I have been unable to reproduce that. And as mentioned previously, just using a width factor that is >= 2 worked for me.
The issue was on Magento Cloud in my case.
Weird. I think it can only be something in the way GD or Imagick was build on that machine. Maybe the quality of the JPG rendering was overruled to be lower quality?
This is on my machine. On the top rendered with 1 as width, the bottom one with 2.
If the JPG gives you problems, you can try to use the PNG renderer. As PNG has no way to compress the image and loose small lines to make the file smaller.
Surprisingly, this happened both with png and jpg renderers during my testing.
AFAIK it was with the gd renderer there, didn’t test adding imagick. Are there any tests that could help here?
@edvardsr Could you dump the data from the $barcodeData in https://github.com/picqer/php-barcode-generator/blob/v2.4.2/src/BarcodeGeneratorPNG.php#L57 and post it here? var_dump($barcodeData);die(); on line 58 for example. Then I can check if there is something wrong with the barcode data being fed into the renderer.
I also just released version 3 of this library with some optimisations. You could also try to use that and see if the results are different.
I’ll give the new version a try later today, will report back with findings roughly before this time tomorrow.
Hi @casperbakker,
This is what is happening with the new version (3.1.0) on the cloud environment.
This is the code I used:
<div>
<p>Barcode test</p>
<p>
<?php
$barcode = (new \Picqer\Barcode\Types\TypeCode39())->getBarcode('0123456789');
$renderer = new \Picqer\Barcode\Renderers\PngRenderer();
echo '<img src="data:image/png;base64,' . base64_encode($renderer->render($barcode, 1, 50)) . '">';
echo '<br>';
echo '<img style="margin-top: 10px;" src="data:image/png;base64,' . base64_encode($renderer->render($barcode)) . '">';
echo '<br>';
echo '<img style="margin-top: 10px;" src="data:image/png;base64,' . base64_encode($renderer->render($barcode, 2, 100)) . '">';
echo '<br>';
echo '<img style="margin-top: 10px;" src="data:image/png;base64,' . base64_encode($renderer->render($barcode, 3, 150)) . '">';
?>
</p>
</div>
To compare, this is what I have on my local setup:
Here is a var_dump of the $barcode variable on the cloud environment. For the record, it seems almost identical to the one on my local setup except for the # numbers, so the issue has to be somewhere else probably.
https://gist.github.com/edvardsr/eb8a5a1b2745db66eb785f4822d0e9e1
The GD version on the cloud instance is "bundled (2.1.0 compatible)", so potentially it might be possible to reproduce the issue with this version of GD. Imagick isn't installed, and the PHP version is 8.3.7.
The version of GD on my local setup is 2.3.3, and the version of imagick is 3.7.0. I have been unable to reproduce the issue with those versions.