kalpa1024's avatar

How to save QR code from Google API into storage

I am using Google API to generate QR code. I want to store this in my storage as a png or jpg in my Laravel project. How can I extract the file for me to be able to save to my storage ? I saw a few examples from the internet but nothing specific about storing the QR code in an actual file.

(URL : https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl=My_Data )

0 likes
3 replies
s4muel's avatar
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; //optional
//...
$contents = file_get_contents('https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl=My_Data');

$filename = 'your_unique_filename.png'; //e.g. user id, or a hash, or random string, or uuid
//$filename = (string) Str::uuid() . '.png';

Storage::put($filename, $contents);
1 like
gitwithravish's avatar

//get file contents from url
$url = "https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl=My_Data";
$contents = file_get_contents($url);

//generate unique name for the file to be saved
// you can use any other way to create filename
$filename = uniqid($url);

//save to storage
$imagepath = \Storage::put($filename, $contents);

//save $imagepath into database if needed
1 like
Sinnbeck's avatar
use Illuminate\Support\Facades\Http; //top of file

$request = Http::get('https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl=My_Data'');

if ($request->ok() {
    Storage::put('filename.png', $request->body());
}
1 like

Please or to participate in this conversation.