Skip to content

Codegen

Directory: app/Services/Codegen/

Barcode and QR Code generation service. Generates PNG images from string content.

Overview

Two generators:

ServiceTypeOutputUse Case
GenerateBarcode1D Barcode (C128, EAN13, etc.)PNG binaryStudent IDs, book labels, asset tags
GenerateQRCodeQR CodePNG binaryQuick scan links, attendance, payment codes

Why It Exists

  • Centralized: One place for all barcode/QR generation instead of scattered Milon\Barcode calls
  • 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): string

Parameters:

ParamDefaultDescription
$contentData to encode (text, numbers)
$typeC128Barcode symbology (C128, EAN13, COD39, etc.)
$width2Width of each bar in pixels
$height30Height of the barcode in pixels

Algorithm:

  1. Call DNS1DFacade::getBarcodePNG($content, $type, $width, $height) → returns base64-encoded PNG
  2. Decode base64 to binary PNG data
  3. 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]): string

Parameters:

ParamDefaultDescription
$contentData to encode (URL, text, JSON)
$size120QR Code size in pixels
$color[0, 0, 0]RGB color array [R, G, B]

Algorithm:

  1. Instantiate DNS2D
  2. Call $d->getBarcodePNG($content, 'QRCODE', 3, 3, $color) → returns base64-encoded PNG
  3. Decode base64 to binary PNG data
  4. 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

FilePurpose
app/Services/Codegen/GenerateBarcode.php1D barcode generation (C128, EAN13, etc.)
app/Services/Codegen/GenerateQRCode.phpQR Code generation
composer.jsonDepends on milon/barcode package