DEV Community

Zahir Din
Zahir Din

Posted on

Generate QR image using Laravel Job imagick

First let's start with install intervention/image && simplesoftwareio/simple-qrcode package.

composer require intervention/image
composer require simplesoftwareio/simple-qrcode
Enter fullscreen mode Exit fullscreen mode

Then we can generate a QR file using jobs. Here is my app/Jobs/GenerateQrImageJob.php file example:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class GenerateQrImageJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    protected $text;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($text)
    {
        $this->text = $text;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Image::configure(['driver' => 'imagick']);
        $storage = Storage::disk('public');
        $file_path = $storage->path($this->text);

        // Create SVG file
        $svg_image = QrCode::size(150)->generate($this->text);
        $storage->put($this->text . '.svg', $svg_image);

        // Change SVG file to BMP file
        $img = Image::make($file_path . '.svg');
        $img->save($file_path . '.bmp');
        $storage->delete($this->text . '.svg');
    }
}

Enter fullscreen mode Exit fullscreen mode

Based on this job, I create a SVG file. Then save it in storage.

I want the file is in BMP format. Then I need to change the SVG file to BMP format.

Thank You.

Top comments (0)