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.
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);
//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
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());
}