PHP code example of laikait / barcode-generator

1. Go to this page and download the library: Download laikait/barcode-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laikait / barcode-generator example snippets


use Laika\Barcode\Barcode;

$svg = Barcode::type('code128')->data('Hello, World!')->svg();

header('Content-Type: image/svg+xml');
echo $svg;

$svg = Barcode::type('ean13')
    ->data('590123412345')   // 12 digits — check digit auto-computed
    ->svg();

$png = Barcode::type('code128')->data('LAIKA-2025')->png();

header('Content-Type: image/png');
echo $png;

$path = Barcode::type('code128')->data('ABC-123')->save('/var/www/barcodes/label.svg');
$path = Barcode::type('ean13')->data('590123412345')->save('/var/www/barcodes/product.png');

$svg = Barcode::type('ean8')->data('9638507')->svg();
echo '<div>' . $svg . '</div>';

$svg = Barcode::type('ean8')->data('9638507')->svgBase64();
$png = Barcode::type('ean8')->data('9638507')->pngBase64();
echo '<img src="<?= $svg 

$svg = Barcode::type('code128')
    ->data('HF5H65AP')
    ->options([
        'watermark_text'     => 'SAMPLE',
        'watermark_color'    => '#cc0000',
        'watermark_opacity'  => 0.15,
        'watermark_rotate'   => -25,
        'watermark_position' => 'center',
    ])
    ->svg();

use Laika\Barcode\QrCode;

$svg = QrCode::data('https://example.com')->svg();

$png = QrCode::data('https://example.com')->png();

header('Content-Type: image/png');
echo $png;

$path = QrCode::data('https://example.com')->save('/var/www/qr/code.svg');
$path = QrCode::data('https://example.com')->save('/var/www/qr/code.png');

$svg = QrCode::type('ean8')->data('9638507')->svgBase64();
$png = QrCode::type('ean8')->data('9638507')->pngBase64();
echo '<img src="<?= $svg 

// SVG
$svg = QrCode::data('https://laika.dev')
    ->options([
        'title'        => 'Scan to visit',
        'title_color'  => '#003366',
        'footer'       => 'laika.dev — PHP Framework',
        'footer_color' => '#999999',
        'footer_align' => 'center',
    ])
    ->svg();

// PNG
$png = QrCode::data('https://laika.dev')
    ->options([
        'title'  => 'Scan to visit',
        'footer' => 'laika.dev',
    ])
    ->png();

$svg = QrCode::data('https://laika.dev')
    ->watermarkText('LAIKA')
    ->svg();

// Styled
$svg = QrCode::data('https://laika.dev')
    ->watermarkText('★', [
        'watermark_size'   => 0.20,
        'watermark_bg'     => '#1a1a2e',
        'watermark_color'  => '#ffffff',
        'watermark_radius' => 50,
    ])
    ->svg();

// From file path
$png = QrCode::data('https://laika.dev')
    ->watermarkImage('/path/to/logo.png')
    ->png();

// From base64 data URI
$svg = QrCode::data('https://laika.dev')
    ->watermarkImage('data:image/png;base64,...')
    ->svg();

$png = QrCode::data('HF5H65AP')
    ->watermarkText('LAIKA')
    ->options([
        'title'        => 'Serial Number',
        'title_color'  => '#003366',
        'footer'       => 'HF5H65AP — laika.dev',
        'footer_color' => '#666666',
    ])
    ->png();

// Returns bool[][] — true = dark module
$matrix = QrCode::data('test')->matrix();

foreach ($matrix as $row => $cols) {
    foreach ($cols as $col => $dark) {
        // drive your own renderer
    }
}

use Laika\Barcode\Barcode;
use Laika\Barcode\Contracts\BarcodeInterface;

class MyEncoder implements BarcodeInterface
{
    public function encode(string $data): array { /* return bar array */ }
    public function validate(string $data): bool { /* ... */ }
    public function label(string $data): string { return $data; }
}

Barcode::register('myformat', MyEncoder::class);

$svg = Barcode::type('myformat')->data('test')->svg();

use Laika\Barcode\Contracts\RendererInterface;

class HtmlRenderer implements RendererInterface
{
    public function render(array $bars, string $label, array $options = []): string
    {
        $html = '<div style="display:flex;">';
        foreach ($bars as $bar) {
            $color = $bar['bar'] ? '#000' : 'transparent';
            $html .= sprintf(
                '<div style="width:%dpx;height:80px;background:%s;"></div>',
                $bar['width'] * 2, $color
            );
        }
        return $html . '</div><p>' . htmlspecialchars($label) . '</p>';
    }
}

$html = Barcode::type('code39')
    ->data('HELLO')
    ->renderWith(new HtmlRenderer());