tuncaybahadir/quar
Quar is a Laravel QR code generator for PHP 8.2+ and Laravel 10–13. Create QR codes quickly with a fluent API: set size, colors, and eye/marker styles (square, rounded, circle, ring). Returns ready-to-render output for Blade views.
composer require tuncaybahadir/quar
use tbQuar\Facades\Quar;
$qrCode = Quar::generate('https://example.com');
<div>{{ $qrCode }}</div>
tbQuar\Facades\Quar facade provides a fluent interface for all QR code customizations.Generate a QR code for a user profile link and save it to storage:
$qr = Quar::format('png')
->size(200)
->color('#32a852')
->generate("https://app.example.com/profile/{$user->id}", storage_path("app/qr_{$user->id}.png"));
Use the facade in controllers or services to generate QR codes on-the-fly:
// In a controller
public function generateInvoiceQr($invoiceId) {
$qr = Quar::size(150)
->color('#2c3e50')
->eye('rounded')
->generate("invoice/{$invoiceId}");
return view('invoice', ['qr' => $qr]);
}
Generate multiple QR codes in a loop (e.g., for bulk exports):
$products = Product::all();
foreach ($products as $product) {
$qr = Quar::size(100)
->generate("product/{$product->slug}")
->saveAs(storage_path("app/qr_products/{$product->id}.png"));
}
when()Leverage Laravel’s Conditionable trait for dynamic QR code customization:
$qr = Quar::size(200)
->when($user->isPremium, function ($qr) {
$qr->gradient(255, 0, 0, 0, 0, 255, 'horizontal');
})
->generate($user->profileUrl);
Add descriptive text to QR codes (e.g., for tickets or labels):
$qr = Quar::size(300)
->withText("Event Ticket: {$event->name}")
->configureText(function ($text) {
$text->setPosition('bottom')
->setFontSize(10)
->setTextColor('#ffffff')
->setBackgroundColor('#000000')
->setBackgroundOpacity(0.7);
})
->generate("event/{$event->ticket_code}");
Embed logos for branding (ensure margin(1) is set to avoid rendering bugs):
$qr = Quar::format('png')
->margin(1)
->merge(public_path('assets/logo.png'), 0.25)
->size(400)
->generate("https://example.com");
<img src="data:image/png;base64,{{ base64_encode($qr) }}" />
storage_path() for file-based QR codes:
$path = storage_path("app/qr_codes/{$user->id}.png");
Quar::generate($url, $path);
Return QR codes as binary responses:
return response($qr, 200, [
'Content-Type' => 'image/png',
'Content-Disposition' => "inline; filename=\"qr_{$id}.png\"",
]);
Mock the facade in tests:
$mockQr = Mockery::mock('alias:tbQuar\Facades\Quar');
$mockQr->shouldReceive('generate')
->with('test-url')
->andReturn('<img src="data:image/png;base64,...">');
$qr = Cache::remember("qr_{$url}", now()->addHours(1), function () use ($url) {
return Quar::generate($url);
});
Quar::setPngCompression(75)->generate($url, $path);
Logo Rendering Bug
margin() is not set to 1 or higher.->margin(1) before ->merge():
Quar::margin(1)->merge($logoPath, $scale)->size(400)->generate($url);
Hex Color Parsing
'#32a852') must be passed as strings, not arrays.->color('#32a852') instead of ->color([50, 168, 82]) for hex values.Text Overlay Conflicts
->withText() before other modifiers:
Quar::withText('Label')->configureText(...)->size(300)->generate($url);
File Permissions
storage/ may fail if directories lack write permissions.php artisan storage:link and ensure storage/app is writable:
chmod -R 755 storage/app
Gradient Limits
->gradient(R, G, B, R, G, B, 'type'):
Quar::gradient(255, 0, 0, 0, 0, 255, 'horizontal')->generate($url);
dd($qr) to verify the output before rendering.->generate($url, $path) are absolute and writable.bacon/bacon-qrcode and simplesoftwareio/simple-qrcode are up-to-date.Custom Text Styling
Extend the TextOverlay class to add features like custom fonts:
// app/Services/CustomTextOverlay.php
use tbQuar\TextOverlay;
class CustomTextOverlay extends TextOverlay {
public function setCustomFont($path) {
$this->font = $path;
return $this;
}
}
Then use it via the facade:
Quar::withText('Custom')->configureText(function ($text) {
$text->setCustomFont(public_path('fonts/roboto.ttf'));
});
Dynamic Size Calculation Create a helper to auto-scale QR codes based on content length:
function dynamicQrSize($content) {
return strlen($content) * 5 + 100; // Adjust multiplier as needed
}
Usage:
$size = dynamicQrSize($user->profileUrl);
Quar::size($size)->generate($user->profileUrl);
Event Listeners Trigger actions when QR codes are generated (e.g., log usage):
// app/Listeners/LogQrGeneration.php
public function handle($event) {
Log::info("QR generated for URL: {$event->url}");
}
Register the listener in EventServiceProvider:
protected $listen = [
'tbQuar\Events\QrGenerated' => [
'App\Listeners\LogQrGeneration',
],
];
square markers, bottom text position), but always verify critical settings like margin() for logos.How can I help you explore Laravel packages today?