Installation:
composer require atheon/barcode-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (legacy):
new BG\BarcodeBundle\BarcodeBundle(),
First Use Case: Generate a simple EAN-13 barcode in a controller:
use BG\BarcodeBundle\Util\Base1DBarcode;
$barcode = new Base1DBarcode();
$barcode->savePath = $this->getParameter('kernel.project_dir').'/public/barcodes/';
$imagePath = $barcode->getBarcodePNGPath('501234567890', 'EAN13', 1.75, 45);
return new Response("<img src='/barcodes/{$imagePath}' />");
Where to Look First:
Base1DBarcode and Base2DBarcode classes in Util/ for API details.Usage section in the README for Twig integration patterns.config/packages/barcode.yaml (if auto-generated) or default settings in the bundle.Image-Based Barcodes (GD/ImageMagick):
$barcode = new Base1DBarcode();
$barcode->savePath = $cacheDir; // Set cache directory
$imagePath = $barcode->getBarcodePNGPath($data, $type, $scale, $rotation);
HTML/SVG Output:
$htmlBarcode = $barcode->getBarcodeHTML($data, $type, $scale);
|raw filter to render HTML barcodes safely:
{{ barcodeHTML|raw }}
2D Barcodes (QR, PDF417, Datamatrix):
use BG\BarcodeBundle\Util\Base2DBarcode;
$matrixCode = new Base2DBarcode();
$matrixCode->savePath = $cacheDir;
$imagePath = $matrixCode->getBarcodePNGPath($data, 'QRCODE,M', 3, 0);
QRCODE,H for noisy environments).Dynamic Barcode URLs:
# config/routes.yaml
barcode_generate:
path: /barcode/{type}/{data}
controller: App\Controller\BarcodeController::generate
// BarcodeController.php
public function generate($type, $data) {
$barcode = new Base1DBarcode();
$barcode->savePath = $this->getParameter('kernel.project_dir').'/public/barcodes/';
$imagePath = $barcode->getBarcodePNGPath($data, $type, 1.5, 0);
return $this->render('barcode/image.html.twig', ['path' => $imagePath]);
}
Batch Processing:
$products = $entityManager->getRepository(Product::class)->findAll();
foreach ($products as $product) {
$barcode = new Base1DBarcode();
$barcode->savePath = $cacheDir;
$product->setBarcodePath($barcode->getBarcodePNGPath($product->getSku(), 'EAN13'));
$entityManager->flush();
}
EntityType for barcode selection:
$builder->add('barcodeType', ChoiceType::class, [
'choices' => [
'EAN-13' => 'EAN13',
'QR Code' => 'QRCODE,M',
],
]);
return $this->json(['barcode_url' => '/barcodes/'.$imagePath]);
Base1DBarcode/Base2DBarcode classes to test controllers without generating files:
$barcodeMock = $this->createMock(Base1DBarcode::class);
$barcodeMock->method('getBarcodePNGPath')->willReturn('mock/path.png');
$this->container->set('barcode.generator', $barcodeMock);
Image Library Dependencies:
php-gd).php-imagick and configure the bundle via config/packages/barcode.yaml:
bg_barcode:
image_driver: imagick
var/log/dev.log for ImagickException or GD errors.File Permissions:
savePath lacks write permissions.$cacheDir = $this->getParameter('kernel.project_dir').'/public/barcodes/';
if (!file_exists($cacheDir)) mkdir($cacheDir, 0777, true);
Cache Paths:
/var/www/) and public (/public/) paths can break URLs.getBarcodeCachePath($public = true) for public-facing URLs:
$publicPath = $this->getBarcodeCachePath(true).$imagePath;
Data Validation:
EAN13) reject malformed input (e.g., non-numeric strings).if (!preg_match('/^\d{13}$/', $eanData)) {
throw new \InvalidArgumentException('Invalid EAN-13 format');
}
Memory Limits:
memory_limit.memory_limit in php.ini or optimize data size.Symfony 5+ Deprecations:
getRootDir() (deprecated in Symfony 5). Override the service:
# config/services.yaml
BG\BarcodeBundle\Util\Base1DBarcode:
arguments:
$kernel: '@kernel'
Log Generated Paths:
error_log("Barcode path: {$imagePath}");
Verify paths are correct and files exist.
Check Image Drivers:
if (!extension_loaded('gd') && !extension_loaded('imagick')) {
throw new \RuntimeException('GD or Imagick extension required');
}
Fallback for Missing Drivers:
if (!extension_loaded('gd')) {
return $this->render('barcode/html_fallback.html.twig', [
'html' => $barcode->getBarcodeHTML($data, $type)
]);
}
Custom Barcode Types:
Code128 variants):
class CustomBarcode extends Base1DBarcode {
public function getCustomBarcode($data) {
// Custom logic
}
}
services.yaml:
BG\AppBundle\Util\CustomBarcode:
tags: ['bg_barcode.generator']
Post-Processing:
savePath logic to add watermarks or resize images:
$barcode->setPostProcessCallback(function($path) {
$
How can I help you explore Laravel packages today?