I am working on a project using ( LARAVEL 5.5) that creates QR codes and i would like to store the image that is built in the backend
this the code that creates the image and convert it to base64:
public function CreateQr($data)
{
//Encrypting the data + creating the QR code with the Encrypted text
$enc = (new CryptoController())->Encrypt($data);
$src = base64_encode(QrCode::format('png')->size(250)->generate($enc));
// the ID of the logged user
$userID = $_SESSION['currentUserID'];
$target = 'img/'.$userID.'/';
if (!file_exists($target)) {
mkdir($target, 0777, true);
}
copy($src, $target);
return $src;
}
the CreateQr function receives a string text and convert that text
into a crypted QR code
I would like to store the ( base64 ) images into a folder ( $target )
I have a seen almost every question in here but didn't find something closer to my needs.
This is my first time using PHP so please do advise me on how to fix and approache this. thanx in advance
The way that you are storing the files is recommended. Visit the Laravel's documentation in File Storage on how to create and move files.
Also, In my opinion, do not convert the files to base64, store the QR image as you wo
Related
Good day everyone! I'm pretty new to Laravel, and I've been doing a little project with QrCode for about a month.
So my question is: "How do I store a generated QR code into the database in Laravel?"
I have created a function which will create random strings (which act as a unique ID for my assets) and converted into QR code. My next action would be to place the QR code image into my database. I've been following a lot of Laravel tutorial but it's only on uploading image not saving an automatically generated image.
I did save the QR code image inside public folder but it will get overwritten every time I create a new QR code.
This is how I create random strings and convert them into the QR Code and save them
$rs = md5(time(). mt_rand(1,100000));
$assets = Input::all();
$assets = new Assets;
$assets->assets_name = Input::get('assets_name');
$assets->assets_random_string = $rs;
$assets->save();
$file = public_path('qr.png');
\QRCode::text($rs)->setOutFile($file)->png();
return redirect('assets/list')->with('assets', $assets)
Please do comment if I've done anything wrong inside my existing so I can improve my code. And let me know if you need anything from my project.
Thank you so much!
Any help is welcomed!
Save to content of the QR Code in the DB and generate the QR Code on demand, that should be the best way.
The correct way and most optimized way is that you save your file path to qr.png in you database. And once you need to show your QR code you simply pull out the path from you database and attaches it to an HTML img tag.
$rs = md5(time(). mt_rand(1,100000));
$assets = Input::all();
$assets = new Assets;
$assets->assets_name = Input::get('assets_name');
$assets->assets_random_string = $rs;
$path = 'images/';
if(!\File::exists(public_path($path))) {
\File::makeDirectory(public_path($path));
}
$file = $path . time() . '.png';
\QrCode::format('png')->generate($rs, $file);
$assets->file = $file;
$assets->save();
I hope it will works,it works for me.
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?
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/
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.
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.
$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.
I have a question about the application generate QR code image.
I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it.
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
my code is bellow:
function generate_qrcode($url){
$filename = 'hante_qrcode.png';
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
//generate QR code image
$o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<pre>";
print_r($o);
print_r('<img src="hante_qrcode.png">');
}
if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
yes
how to solve this problem?
there are two ways to solve this problem
you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples
don't store image on server and find some js to generate qr code directly from client side.
having a one demo for that check if you can use it
var qrcode = new QRCode("qrcode");
qrcode.makeCode('https://stackoverflow.com');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<div id="qrcode"></div>
Of course it will be overwritten.
Solution 1
Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url.
$filename = md5($url) . ".png";
if(!file_exists($filename)){
$o = QRcode::png($url, $filename, ...);
}
echo '<img src="'.$filename.'">';
Solution 2
If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html.
// image.php
// Still we want to give uniqe filename because we can get another request while one request is processing
$filename = md5(microtime) . "_qr.png";
$o = QRcode::png($url, $filename, ...);
$image = file_get_contents($filename);
// remove the file after stored in a variable
unlink($filename);
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
echo $image;
// index.html
<img src="image.php?url=someurl">
I'm trying to upload image file from my android app to a server. This is my php file that is processing incoming images from my website and iOS app to be used in move_uploaded_file function:
<?php include '../../../init.php';
$post_id = $_GET['post_id'];
$image_temp = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$image_ext = strtolower(end(explode('.', $image_name)));
upload_page_image($image_temp, $image_ext, $post_id);
Now, in android I came up to the point when I have Base64 encoded string:
params.put("image",imageToString(bitmap));
Then, I bring this to my PHP file like this:
<$php
....
$image = base64_decode(_POST['image']);
...
But now, how do I brake it in to $_FILES['image']['temp_name'] and $_FILES['image']['name']? All the other examples I went through are using file_put_contents. I need it to be move_uploaded_files.
Any help is appreciated.
Thanks
this worked for me enter link description here
You must submit image using Multipart Form Data for use move_uploaded_files in php
Ok, that was the most difficult stuff I ever went through. The problem is that when I use php function file_put_contents(), it first makes everything easy on android app side, just convert bitmap to Base64 and pass it to php file. But... one very important thing got lost in this process.... it's that $exif['Orientation'] when using exif_read_data. This parameter is not there any more.
Now, when going back to android app and trying to get image orientation before sending the file to php, the standard function such as below:
ExifInterface exifReader = new ExifInterface(mFilePath);
exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
.. or similar doesn't work. It always returns 0. ALWAYS
Solution came from using Glide https://github.com/bumptech/glide library and get current image orientation using:
InputStream is = getActivity().getContentResolver().openInputStream(originalUri);
int orientation = new ImageHeaderParser(is).getOrientation();
Once you get orientation, you pass image as Base64 and orientation number and then process upload and do orientation change and so on.
I'm developing a laravel RESTful app that accepts image strings from users and must store them.
images are encoded and sent to my app. I know that I have to Receive and decode image like this:
$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
but I dont know how to save them to files, without knowing the format of the image?is there any way to find out the image extension, so that I can use functions like:
imagepng
You can use getimagesizefromstring();
$size = getimagesizefromstring($imageData);
if ($size['mime'])
return $size['mime'];