April 18, 2017

Mass-generating QR-Code Coupon with Laravel

Okay, so my client asked me to generate some discount coupons for his loyal customers on hteir next visit to his cafe. The coupons must each have unique QR-code, so that the cashier lady could scan it and automatically reducing the total amount that the customer must pay.

That's eazy.. But..

He asked me to generate 1000 coupon codes! It would pretty sure took days to accomplish using photoshop *_*

Sleek-looking Discount Coupon

So I decided to mass-generat it using laravel. And with some "plugin" packages, of course. Here is how I did it.

Step 1 : Prepare a template for the discount coupon

The template could just be a JPEG or a PNG image file.

A coupon template I got from my client

Copy this file into the storage/app/ directory. Rename it to template.png, or template.jpg depending on original file type.

Step 2 : Install a QR Code Package

Assuming you already have a Laravel application running, get a qrcode package. I used milon/barcode by the way. Run this command on the terminal:

composer require milon/barcode

And wait until the package installation completed successfully. After the installing the qrcode, open up the config/app.php file and add this line Milon\Barcode\BarcodeServiceProvider::class somewhere in the providers array, and also add these two lines 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class and 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class somewhere inside the aliases array. I would look like this, afterward:

'providers' => [
    ...
    Milon\Barcode\BarcodeServiceProvider::class,
    ...
]
...
'aliases' => [
    ...
    'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class,
    'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class,
]

Step 3 : Create the Artisan Command for Generating Coupons

Take the advantage of Laravel's artisan command to mass-generate 1000 discount coupons. But first let's create a command that will do that. Run this command from the terminal:

php artisan make:command GenerateCoupons

This command will create a new file called GenerateCoupons.php inside the app/Console/Command/ directory. Open the file and pour some code into it.

Find a line that looks like this:
protected $signature = 'email:send {user}';
and change it into this
protected $signature = 'generate:coupon {how_many}';

Basicly, what this line provides is that, I will be able to run "generate:coupon" command from the terminal with an argument defining how many discount coupons we want to generate. For example, if I want to generate 1000 discount coupons, I would simply run "generate:coupon 1000" from my terminal.

Awesome...

Next, time to put some real coding here. Find the handle() function and add some serious codes ^_^ :

public function handle()
{
    $count  = $this->argument('how many');
    $img    = \Image::make(storage_path().'/app/template.png');
    for($i=1;$i<=$count;$i++) {
        $qrcode = \Image::make( base64_decode(\DNS2D::getBarcodePNG($base_url.$code, "QRCODE")))->heighten(390);
        $img->insert($qrcode, 'top-left',372,79);
        $img->save(public_path('qr/').$code.'.png',130);
        $this->info($i.' generated');
    }
}
If you have different template file from the one that I have, you can adjust the size of your qr-code, and also adjust the qr-code positioning relative to the template. Just change the number inside the heighten() function to adjust qr-code size (in pixels). Change the second, third, and fourth parameter of the insert() function to adjust the relative position of the qr-code relative to the template. Note that the second parameter could be any of these values:
  • top-left
  • top-right
  • bottom-left
  • bottom-right
This is the illustration how I measured those parameters:
Save this file and, open the next one, app/Console/Kernel.php.

Step 4 : Registering the Command

So we are currently editing the Kernel.php file. Find this line
protected $commands = [
    Commands\SendEmails::class
];
And change it to
protected $commands = [
    Commands\GenerateCoupons::class
];
this will register our GenerateCoupons class so that Laravel's artisan can recognize and able to execute it via the command name.

Step 5 : Moment of Truth

Run this from the terminal:

php artisan generate:coupon 1000

To find out whether the command successfully generating 1000 discount coupons, open up the public/coupons/ folder.

How's that? Pretty cool huh? Why don't you give it a try and leave a comment below

No comments:

Post a Comment