Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Qrcode Detector Decoder Laravel Package

khanamiryan/qrcode-detector-decoder

Pure-PHP QR code detector/decoder (ported from ZXing). Reads QR codes from image files using GD, no extra extensions required. Simple API: create a Zxing\QrReader with a path and call text() to get the decoded content.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing the package via Composer:

composer require khanamiryan/qrcode-detector-decoder

Then decode a QR code in minutes—no extension setup needed. In a Laravel controller:

use Zxing\QrReader;

public function decode(Request $request)
{
    $path = $request->file('qr_image')->getRealPath();
    $qrcode = new QrReader($path);
    
    $text = $qrcode->text();
    if ($text === false) {
        return response()->json(['error' => 'QR code not readable'], 400);
    }

    return response()->json(['data' => $text]);
}

This handles common workflows like validating uploaded ticket images or scanning loyalty QR codes—ideal for server-side processing where client-side JS is impractical.

Implementation Patterns

  • Service wrapper: Encapsulate decoding logic in a reusable service (e.g., QRDecoderService) for clean DI in controllers/jobs:
    class QRDecoderService
    {
        public function decode(string $path): ?string
        {
            $reader = new QrReader($path);
            return $reader->text() ?: null;
        }
    }
    
  • Queue processing: Decode bulk QR codes from S3 in jobs—e.g., ProcessInventoryScansJob that reads images from $storage->path('scans/*.jpg').
  • Preprocessing pipeline: Improve reliability by preprocessing images before decoding:
    $image = imagecreatefromjpeg($path);
    $gray = imagefilter($image, IMG_FILTER_GRAYSCALE);
    $binary = imagefilter($gray, IMG_FILTER_BRIGHTNESS, -30);
    imagepng($binary, $tempPath = sys_get_temp_dir() . '/qr_prep.png');
    $decoded = (new QrReader($tempPath))->text();
    
  • Binary mode: For non-text data (e.g., JWT payloads), enable binary mode (v2.0.3+):
    $reader = new QrReader($path, QrReader::SOURCE_TYPE_FILE, true);
    $raw = $reader->text();
    $payload = json_decode(mb_convert_encoding($raw, 'UTF-8', 'auto'), true);
    

Gotchas and Tips

  • GD is non-negotiable: The library exclusively uses GD. Verify availability: php artisan tinker --execute='extension_loaded("gd")'. Docker users: add --with-gd or use official PHP images with GD enabled.
  • Path validation matters: QrReader throws RuntimeException if the file doesn’t exist—always validate Storage::disk('local')->exists($path) first.
  • Error Handling: text() returns false on decode failure, not null. Use strict comparison: if ($text === false).
  • Encoding chaos: QR codes may use non-UTF-8 encodings (e.g., Shift-JIS). Normalize output:
    $text = mb_convert_encoding($text ?? '', 'UTF-8', 'auto');
    
  • Memory limits: Large images can exhaust memory in CLI jobs. Explicitly set limits before decoding:
    ini_set('memory_limit', '512M'); // Critical in queue workers
    
  • Testing: Inject QrReader via constructor to mock in tests:
    $mock = $this->createMock(QrReader::class);
    $mock->method('text')->willReturn('test-data');
    
  • Avoid legacy versions: Use ^2.0 for PHP 8.1–8.3 support. Version 1.x has critical PHP 8+ type errors. Check your composer.lock!
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport