Codegen
Directory: app/Services/Codegen/
Barcode and QR Code generation service. Generates PNG images from string content.
Overview
Two generators:
| Service | Type | Output | Use Case |
|---|---|---|---|
GenerateBarcode | 1D Barcode (C128, EAN13, etc.) | PNG binary | Student IDs, book labels, asset tags |
GenerateQRCode | QR Code | PNG binary | Quick scan links, attendance, payment codes |
Why It Exists
- Centralized: One place for all barcode/QR generation instead of scattered
Milon\Barcodecalls - Consistent: Same encoding options, same error handling across the application
- Testable: Services can be mocked or swapped without touching controllers
Methods
GenerateBarcode::generate(string $content, string $type = 'C128', int $width = 2, int $height = 30): string
Generates a 1D barcode PNG.
php
public function generate(string $content, string $type = 'C128', int $width = 2, int $height = 30): stringParameters:
| Param | Default | Description |
|---|---|---|
$content | — | Data to encode (text, numbers) |
$type | C128 | Barcode symbology (C128, EAN13, COD39, etc.) |
$width | 2 | Width of each bar in pixels |
$height | 30 | Height of the barcode in pixels |
Algorithm:
- Call
DNS1DFacade::getBarcodePNG($content, $type, $width, $height)→ returns base64-encoded PNG - Decode base64 to binary PNG data
- Return raw PNG string
GenerateQRCode::generate(string $content, int $size = 120, array $color = [0, 0, 0]): string
Generates a QR Code PNG.
php
public function generate(string $content, int $size = 120, array $color = [0, 0, 0]): stringParameters:
| Param | Default | Description |
|---|---|---|
$content | — | Data to encode (URL, text, JSON) |
$size | 120 | QR Code size in pixels |
$color | [0, 0, 0] | RGB color array [R, G, B] |
Algorithm:
- Instantiate
DNS2D - Call
$d->getBarcodePNG($content, 'QRCODE', 3, 3, $color)→ returns base64-encoded PNG - Decode base64 to binary PNG data
- Return raw PNG string
How to Use
Via Service Directly
php
use App\Services\Codegen\GenerateBarcode;
use App\Services\Codegen\GenerateQRCode;
// Barcode
$barcode = app(GenerateBarcode::class);
$png = $barcode->generate('STU-2026-001', 'C128', 2, 50);
// QR Code
$qr = app(GenerateQRCode::class);
$png = $qr->generate('https://sutomo.sch.id/student/001', 200, [0, 0, 0]);Store as Asset
php
use App\Models\Platform\Asset\Asset;
$asset = new Asset;
$asset->uploadedFile = $png; // raw PNG from generator
$asset->uploadOptions = [
'folder' => 'barcodes',
'file_name' => 'barcode-stu-001.png',
];
$asset->save();Return as Response
php
use App\Services\Codegen\GenerateQRCode;
$png = app(GenerateQRCode::class)->generate('attendance-token-xyz');
return response($png, 200, [
'Content-Type' => 'image/png',
'Content-Disposition' => 'inline; filename="qrcode.png"',
]);Key Files
| File | Purpose |
|---|---|
app/Services/Codegen/GenerateBarcode.php | 1D barcode generation (C128, EAN13, etc.) |
app/Services/Codegen/GenerateQRCode.php | QR Code generation |
composer.json | Depends on milon/barcode package |