I'm trying to use the Directory API to pull user information, which I'm able to do, but I can't seem to find any good examples on how to access user images.
I tried the following, which gets me a photo resource:
$photo = $service->users_photos->get($uid);
After that I found the utility method for web-safe base64 decoding and tried to access the data like so:
$data = Google_Utils::urlSafeB64Decode($data);
$data = base64_encode($data);
return "data:" . $mime . ";base64," . $data;
However, this produces a pixelated image. So I'm either missing a step or something is wrong with the data I'm getting. I can't seem to find any documentation on how to get this image using PHP, is there an official method for decoding the bytes received in photoData and converting them to an image?
Related
I have this line:
move_uploaded_file($_FILES["img"]["tmp_name"], "uploads/" . "$img_name");
That used to work when I received the image through a direct POST (without JSON.stringify the content).
Now I'm sending the ajax with the image inside of an array stringified, so I'm getting the contents of the array in the PHP file in this way:
$unstringified = json_decode(file_get_contents("php://input"), true);
And then I use:
$title = $unstringified["title"];
$content = $unstringified["content"];
$img = $unstringified["img"];
The problem is that now the move_uploaded_file stop working (seems to be no error but the image doesn't appear saved in the folder anymore). I tried some options like these, but didn't work.
move_uploaded_file($unstringified["img"]["tmp_name"], "uploads/" . "$img_name");
move_uploaded_file($_FILES[$unstringified["img"]]["tmp_name"], "uploads/" . "$img_name");
Any idea to solve this? Should I "convert" the image in any format until putting it in the array I will stringify? or I have to get the image in another way in the PHP file?
Thanks a lot in advance.
Leandro.
I wanted to pass the image inside a JSON because always that I've tried to pass and image with other string variables with formdata, I've always got an error, even playing with all posible variables of dataType, contentType , Cache, etc, and researching through lot of answers here. I couldn't find a solution to use form data to pass all togheter.
What I found I can do is to pass all inside within a JSON object without getting errors, so that's why I wanted to receive the image in PHP under this format and save it in this way.
But after trying lot of ways, again, and couldn't find a solution for this, I gave up, and use 2 separate sends, one json object with all the strings inside, and one formdata with only the image (if I don't add another variables like strings, there's no problem).
I'm sure it's not the best way, but it's what I can do.
Thanks all!
How I can store decoded image to laravel public image path and hash image name ?
I tried like this, but didnt work and doesn't get any error message;
if (!empty($request->image)) {
$file = base64_decode($request->image)->hashName();
base64_decode($request->image)->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
This code works fine but I use API and via curl sending image then needed base64 decoding:
if ($request->hasFile('image')) {
$file = $request->file('image')->hashName();
$request->image->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
What you suggest save image and send image url to api and downloads image too path or send base64 image encode and in api decode, like I'm trying at the moment?
You can't call functions on the output of base64_decode, as it returns as string.
I would suggest you post the name of the file along with the base 64 encoded content, which can then be saved using laravel's storage helpers:
Storage::disk('local')->put('image.png', base64_decode($request->image))
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 am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.
i am using Google API for creating QR Codes but it gives me a file with nothing in it.
my code something like that
$file = file_get_contents('https://chart.googleapis.com/chart?cht=qr&chs=177x177&chl=Hello World');
move_uploaded_file($file, $path.$file);
i tried most of the thing but clueless
please if anyone can help me. thanks in advance.
Google Chart API can create QR for you. but you need some conversion and stick to the basics if you want accurate response from API
i can see you are saving the response in $file can may be a reason to save the name in db.
so why not do it like this
$path = $folder_path.$file;
//Note: you need to give the server path; not the URL
but wait... you are creating a QR code for a String saying "Hello World", there is a blank space in the string and you are passing that string with the get method in the url so you need url encoding to encode the spaces to "%20" and so on... for correct url passing.
adding this will solve the problem
$my_qr_string = urlencode("Hello World");
another thing you are missing is character support for the QR
try using "UTF-8" character encoding to avoid any issues.
for uploading use file_put_contents() method for assured upload
in the end you got
file_put_contents($path, file_get_contents('https://chart.googleapis.com/chart?cht=qr&chs=177x177&choe=UTF-8&chl='.$my_qr_string));