Laravel File Storage: How to store (decoded) base64 image? - php

How to store base64 image using the Laravel's filesytem (File Storage) methods?
For example, I can decode base64 image like this:
base64_decode($encoded_image);
but all of the Laravel's methods for storing files can accept either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance.
So I guess I'd have to convert base64 image (or decoded base64 image) to Illuminate\Http\File or Illuminate\Http\UploadedFile, but how?

Just use put to store the encoded contents:
Storage::put('file.jpg', $encoded_image);
All it's doing is wrapping file_put_contents.
Then to read it back out:
$data = base64_decode(Storage::get('file.jpg'));
Which, you guess it, is wrapping file_get_contents.

You can upload your base64 Image using laravel File Storage like this
$base64_image = $request->input('base64_image'); // your base64 encoded
#list($type, $file_data) = explode(';', $base64_image);
#list(, $file_data) = explode(',', $file_data);
$imageName = str_random(10).'.'.'png';
Storage::disk('local')->put($imageName, base64_decode($file_data));
Hope it will help you

Related

Laravel decode base64 image and hashName and store public folder

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))

Postman upload file on API using raw

How do you upload a file on postman using raw mode.
I used a json entry like this:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"#/User/images.jpg"
}
the problem was the # in php was deprecated and now recommended to use curlFile, how to do it in a json form?
Thanks.
Since you are sending the image as part of the JSON, you would need to encode it. Base64 encoding is a good choice. On the server side, you will need to decode it as well.
See this answer to How to convert an image to base64 encoding?:
$imagedata = file_get_contents("/path/to/image.jpg");
$base64 = base64_encode($imagedata);
Once you have the data, you can add it to the JSON:
{
"inp_doc_uid": "750761691595cf3398da311004881705",
"tas_uid": "71415152859433e07c33b11085554015",
"app_doc_comment":"test upload",
"form":"TWFuIGlzIGRpc3R..."
}
Also see Base64 Reference

PHP base64 encode a pdf file

I am using an API where I can send a document to something like dropbox. According to the documentation, the file which is sent needs to be BASE64 encoded data.
As such, I am trying something like this
$b64Doc = chunk_split(base64_encode($this->pdfdoc));
Where $this->pdfdoc is the path to my PDF document.
At the moment, the file is being sent over but it seems invalid (displays nothing).
Am I correctly converting my PDF to BASE64 encoded data?
Thanks
base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file
$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
base64_encode() will encode whatever string you pass to it. If the value you pass is the file name, all you are going to get is an encoded filename, not the contents of the file.
You'll probably want to do file_get_contents($this->pdfdoc) or something first.
Convert base64 to pdf and save to server path.
// Real date format (xxx-xx-xx)
$toDay = date("Y-m-d");
// we give the file a random name
$name = "archive_".$toDay."_XXXXX_.pdf";
// a route is created, (it must already be created in its repository(pdf)).
$rute = "pdf/".$name;
// decode base64
$pdf_b64 = base64_decode($base_64);
// you record the file in existing folder
if(file_put_contents($rute, $pdf_b64)){
//just to force download by the browser
header("Content-type: application/pdf");
//print base64 decoded
echo $pdf_b64;
}

handling a json base64 encode image from iphone app with php

I am working on a php REST API that will be used with an iPhone app. It is another developer who develops the app.
From that app it is possible to take a image and upload it to the webserver. I will recieve the images from the iphone as base64 with json, but I am unsure how to process them with a PHP script.
The images will be send like:
{
"image1":"Base64Data",
"image2":"Base64Data",
"image3":"Base64Data",
"image4":"Base64Data"
}
will it be something like:
$image1 = json_decode($jsonData);
Is it possible to get the same / a like data from the base64 string as from $_FILES[] upload? When images a upload the normal way from the website, they are being handled with a cropper and thumbnail generator. I want the base64 images to be handled the same way.
your json which looks like this
{
"image1":"Base64Data",
"image2":"Base64Data",
"image3":"Base64Data",
"image4":"Base64Data"
}
will not be something like:
$image1 = json_decode($jsonData);
more like this
$json = json_decode($jsonData, true);
$image1 = $json['image1'];
There will be no values similar to $_FILES except the data of the file.
you can easily do
$imagedata = base64_decode($image1);
file_put_contents("file.jpg", $imagedata);
You can decode Base64 encoded, with this: base64_decode($yourstring)
Also, you can decode the Json and after the Base 64 encoded.

How to create GD Image from base64 encoded jpeg?

I have an upload Api that as a response object delivers (along with other stuff inside a Json object) a base64 encoded jpeg image.
I create the encoded image as so:
$im; // gd image resource
ob_start();
imagejpeg($im);
$data = base64_encode(ob_get_clean());
The data then is put inside a form field using javascript and submitted.
How can I create a GD resource from that again so that I actually can save that image as a file?
Everything in PHP.
You can use imagecreatefromstring() function:
$data = base64_decode($_POST['image_as_base64']);
$formImage = imagecreatefromstring($data);
"String" does not mean a "real" string. In that case it means bytes/blob data.
How can I create a GD resource from that again so that I actually can save that image as a file?
Are you sure you need to do that extra step? How about:
file_put_contents('MyFile.jpg', base64_decode($_POST['MyFormField']));
I did by this way:
// input is in format: data:image/png;base64,...
$str="data:image/png;base64,";
$data=str_replace($str,"",$_POST['image']);
$data = base64_decode($data);
file_put_contents('tmp/'.mktime().'.png', $data);

Categories