- How do I install chillerlan/php-qrcode in a Laravel project?
- Run `composer require chillerlan/php-qrcode` to install the package. For optional dependencies like GD or ImageMagick, add them to your `composer.json` under `require` or `suggest` based on your needs. The package is framework-agnostic but integrates seamlessly with Laravel’s service container.
- Which Laravel versions does this package support?
- The package is PHP 8.4+ compatible and works with any Laravel 10.x or 11.x project. It doesn’t enforce Laravel-specific dependencies, so it’s a drop-in solution for modern Laravel apps. Always test with your specific Laravel version to confirm compatibility.
- Can I generate QR codes dynamically in Laravel Blade templates?
- Yes. Use the QRCode class directly in Blade or bind it to Laravel’s service container. For SVG output, embed the generated string directly in your Blade file. For PNG/JPEG, save the file to storage and reference it via `<img>` tags. Example: `{{ $qrcode->render('svg') }}`.
- What are the optional dependencies, and how do I handle them?
- Optional dependencies include `ext-gd` (for PNG/JPEG), `ext-imagick` (for advanced image processing), and `setasign/fpdf` (for PDF output). Use `composer require --with-all-dependencies` or specify them in `composer.json` under `suggest`. Fall back to text/SVG modes if these extensions are unavailable.
- How do I configure error correction levels (ECC) for QR codes?
- Set the error correction level via the QRCode constructor or configuration. Example: `$qrcode = new QRCode(['errorCorrectionLevel' => 'H']);` or define it in `config/qrcode.php`. Supported levels are `L` (low), `M` (medium), `Q` (quartile), and `H` (high). Higher levels increase scan reliability but reduce capacity.
- Is there a way to read QR codes in Laravel using this package?
- Yes, the package includes a ZXing-derived QR reader. Use `QRCode::scan()` with a GD or ImageMagick resource to decode QR codes from images. Example: `$data = QRCode::scan($imageResource);`. Note that this requires `ext-gd` or `ext-imagick` and may need additional setup for production.
- How can I optimize QR code generation for performance in Laravel?
- Offload generation to Laravel Queues for non-critical paths to avoid timeouts. Cache generated QR codes using Laravel’s cache system or Redis. For high-volume use, pre-generate static QR codes and store them in `storage/app/qrcodes` with unique filenames.
- Does this package support generating QR codes for sensitive data (e.g., API tokens)?
- While the package itself doesn’t enforce security, you can encode sensitive data by embedding it in a data URI (e.g., `data:text/plain;base64,...`) or using checksums. Avoid storing raw secrets in QR codes; instead, use them to reference encrypted or hashed values. Always validate scanned data server-side.
- Can I use this package to generate QR codes for PDFs in Laravel?
- Yes, if you install the `setasign/fpdf` dependency. Use the `QRPdf` class to embed QR codes in PDFs generated via libraries like `dompdf` or `barryvdh/laravel-dompdf`. Example: `$pdf->writeQRCode($data, $x, $y, $size);`. Ensure the dependency is listed in `composer.json`.
- What alternatives exist for QR code generation in Laravel, and why choose this package?
- Alternatives include `endroid/qr-code`, `bacon/bacon-qr-code`, and JavaScript libraries like `@chillerlan/qrcode`. This package stands out for its ZXing-based reader, support for mixed encoding modes, and modular output formats (SVG, PNG, PDF). It’s also actively maintained with PHP 8.4+ support and integrates cleanly with Laravel’s ecosystem.