Ryoma Kishimoto
Ryoma Kishimoto

Reputation: 475

How to store QR code as image file in to Mysql Database

I`m implementing a simple application using Laravel. just wondering, when I send qr code in email text, does qr code need to be stored in database first to Specify file pass for the image??

If that answer is yes, is there any way that I`m able to store qr code without using form tag?

Upvotes: 2

Views: 15495

Answers (4)

kaamrul
kaamrul

Reputation: 53

$data = new ModelName();

$path = '/img/';

if(!\File::exists(public_path($path))) {
    \File::makeDirectory(public_path($path));
}

$file_path = $path . time() . '.png';
$image = \QrCode::format('png')
                 ->merge('img/t.jpg', 0.1, true)
                 ->size(200)->errorCorrection('H')
                 ->generate('A simple example of QR code!', $file_path)

$data->file = $file_path;
$data->save();

I hope this will help you, it works fine for me.

Upvotes: 0

jabdefiant
jabdefiant

Reputation: 161

I don't think you need to store the actual QR code.

A QR code is merely a way of representing a string of characters. Often people will put a URL into the QR code.

You can probably just store the source data into your db, and generate the QR from the data.

If the data is a URL, the device consuming the QR should be able to link to the url which will bring it back to your application. You could put parameters on the end of the URL to allow your app to retrieve the data from your db for that user.
You could even use a signed URL so that the end user cannot change it.

Here is an article that I found that may help. It's not laravel specific, but will help with the QR code understanding. https://www.kerneldev.com/2018/09/07/qr-codes-in-laravel-complete-guide/

Upvotes: 7

online Thomas
online Thomas

Reputation: 9401

You could also store the image as a BLOB, which has less overhead as a base64 encoded image and would not be indexed as a searchable string.

Even better might be to just store links to binaries in your database as opposed to the data itself.

Upvotes: 1

MRustamzade
MRustamzade

Reputation: 1455

You can do it converting image to base64 and then store it as text. for more information how to encode visit http://php.net/manual/en/function.base64-encode.php and for decode http://php.net/manual/en/function.base64-decode.php

example encode:

$file_encoded = base64_encode(file_get_contents($file)); //this is stringed data. save this in database. 

example decode:

$file_encoded = base64_decode ($file_encoded); //this will be file.

Upvotes: 3

Related Questions