PHP code example of isahaq / barcode

1. Go to this page and download the library: Download isahaq/barcode 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/ */

    

isahaq / barcode example snippets


'providers' => [
    // ...
    Isahaq\Barcode\Providers\BarcodeServiceProvider::class,
],

'aliases' => [
    // ...
    'Barcode' => Isahaq\Barcode\Facades\Barcode::class,
],

use Isahaq\Barcode\Types\Code128;
use Isahaq\Barcode\Renderers\PNGRenderer;

$barcodeType = new Code128();
$renderer = new PNGRenderer();
$barcode = $barcodeType->encode('1234567890');
$pngData = $renderer->render($barcode);
file_put_contents('barcode.png', $pngData);

// Generate QR Code
$qrCode = Barcode::qrCode()
    ->data('https://example.com')
    ->size(300)
    ->format('png')
    ->generate();

// Generate EAN13 Barcode
$ean13 = Barcode::code('EAN13', '1234567890128')
    ->png()
    ->save('barcode.png');

// Get as Base64 (for web display)
$base64 = Barcode::code('Code128', '123456')
    ->png()
    ->asBase64();

$barcodeImage = $renderer->render($barcode);
$base64 = base64_encode($barcodeImage);
echo '<img src="data:image/png;base64,' . $base64 . '" alt="Barcode" />';

use Isahaq\Barcode\Utils\BatchGenerator;

$batch = new BatchGenerator();
$codes = ['ABC123', 'DEF456', 'GHI789'];

foreach ($codes as $code) {
    $barcode = $batch->generate('Code128', $code, 'png');
    file_put_contents("barcode_{$code}.png", $barcode);
}

use Isahaq\Barcode\Utils\Validator;

$validator = new Validator();
if ($validator->validate('EAN13', '1234567890128')) {
    echo 'Valid EAN13';
} else {
    echo 'Invalid EAN13';
}


use Isahaq\Barcode\Types\Code128;
use Isahaq\Barcode\Renderers\PNGRenderer;
$barcodeType = new Code128();
$renderer = new PNGRenderer();
$barcode = $barcodeType->encode('1234567890');
$result = $renderer->render($barcode);
file_put_contents('barcode.png', $result);

// ... after generating $barcode (PNG data)
$barcodeImage = base64_encode($barcode);
echo '<img src="data:image/png;base64,' . $barcodeImage . '" alt="Barcode" />';
//with custom height width
echo '<img src="data:image/png;base64,' . $barcodeImage . '" alt="Barcode" />';

use Isahaq\Barcode\QrCodeBuilder;

$qrCode = QrCodeBuilder::create()
    ->data('https://example.com')
    ->size(300)
    ->margin(10)
    ->foregroundColor([0, 0, 0])
    ->backgroundColor([255, 255, 255])
    ->logoPath('path/to/logo.png') // Logo in the center
    ->label('Scan me!')            // Watermark or label below
    ->labelFont('path/to/font.ttf', 16)
    ->format('png')
    ->build();

// Save to file
$qrCode->saveToFile('qr-code-with-logo.png');

// Display as base64 image
echo '<img src="data:image/png;base64,' . base64_encode($qrCode->getString()) . '" alt="QR Code with Logo and Watermark" />';

use Isahaq\Barcode\Services\BarcodeService;
$barcodeService = new BarcodeService();
$pngData = $barcodeService->png('1234567890', 'code128');
$svgData = $barcodeService->svg('1234567890', 'ean13');
$htmlData = $barcodeService->html('1234567890', 'code39');
$options = [ 'width' => 300, 'height' => 100 ];
$customBarcode = $barcodeService->make('code128', 'png', '1234567890', $options);

use Isahaq\Barcode\QrCodeBuilder;
$qrCode = QrCodeBuilder::create()
    ->data('https://example.com')
    ->size(300)
    ->margin(10)
    ->foregroundColor([0, 0, 0])
    ->backgroundColor([255, 255, 255])
    ->logoPath('path/to/logo.png')
    ->label('Scan me!')
    ->labelFont('path/to/font.ttf', 16)
    ->format('png')
    ->build();
$qrCode->saveToFile('qr-code.png');
$dataUri = $qrCode->getDataUri();
echo "<img src='$dataUri' alt='QR Code'>";

use Isahaq\Barcode\Facades\Barcode;
$barcode = Barcode::png('1234567890', 'code128');
$qrCode = Barcode::modernQr([
    'data' => 'https://example.com',
    'size' => 300,
    'margin' => 10,
    'error_correction' => 'H',
    'foreground_color' => [0, 0, 0],
    'background_color' => [255, 255, 255],
    'label' => 'Scan me!'
]);
return response($barcode)->header('Content-Type', 'image/png');

use Isahaq\Barcode\Facades\Barcode;
$qrWithLogo = Barcode::modernQr([
    'data' => 'https://example.com',
    'size' => 300,
    'margin' => 10,
    'logoPath' => 'path/to/logo.png',
    'logoSize' => 60, // Logo size as percentage (default: 60)
    'label' => 'Scan me!',
    'error_correction' => 'H' // Use H for better logo support
]);
return response($qrWithLogo)->header('Content-Type', 'image/png');

$positions = Barcode::getWatermarkPositions();
// Returns: ['top-left', 'top-right', 'bottom-left', 'bottom-right', 'center',
//           'top-center', 'bottom-center', 'left-center', 'right-center']

public function generateBarcode(Request $request)
{
    $barcodeService = app('barcode');
    $data = $request->input('data', '1234567890');
    $type = $request->input('type', 'code128');
    $format = $request->input('format', 'png');
    $options = [ 'width' => $request->input('width', 300), 'height' => $request->input('height', 100) ];
    $barcode = $barcodeService->make($type, $format, $data, $options);
    return response($barcode)->header('Content-Type', $this->getMimeType($format));
}
private function getMimeType($format)
{
    return match($format) {
        'png' => 'image/png',
        'svg' => 'image/svg+xml',
        'html' => 'text/html',
        default => 'image/png'
    };
}

<img src="data:image/png;base64,{{ base64_encode(Barcode::png('1234567890')) }}" alt="Barcode">
//with custom height width
<img src="data:image/png;base64,{{ base64_encode(Barcode::png('1234567890','C128',['width' => 1,
    'height' => 40,
    'foreground_color' => [255, 0, 0], // Red
    'background_color' => [255, 255, 255], // White
    'padding' => 20
])) }}" alt="Barcode">

<img src="data:image/png;base64,{{ base64_encode(Barcode::modernQr(['data' => 'https://example.com'])) }}" alt="QR Code">

<!-- QR Code with Logo -->
<img src="data:image/png;base64,{{ base64_encode(Barcode::modernQr([
    'data' => 'https://example.com',
    'logoPath' => 'http://127.0.0.1:8000/assets/images/logo-dark.png',
    'logoSize' => 60,
    'error_correction' => 'H'
])) }}" alt="QR Code with Logo">
// routes/web.php
Route::get('/barcode/{data}', function ($data) {
    $barcode = Barcode::png($data, 'code128');
    return response($barcode)->header('Content-Type', 'image/png');
});
bash
php src/CLI/generate.php --type=Code128 --data=123456 --format=png --output=barcode.png
bash
php vendor/bin/generate.php --data="1234567890" --type="code128" --format="png" --output="barcode.png"
php vendor/bin/generate.php --data="https://example.com" --type="qrcode" --format="png" --output="qr.png"
php vendor/bin/generate.php --data="1234567890" --type="ean13" --format="svg" --output="barcode.svg"