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))
Related
I am using Croppie jQuery plugin which returns the cropped image encoded in base64.
After submitting the form (with the cropped image encoded in base64) - I decode & resize it using the Intervention Image library:
public function decodeResizeAndStore(Request $request)
{
$croppie_code = $request->croppie_code;
// https://stackoverflow.com/a/11511605/4437206
if (preg_match('/^data:image\/(\w+);base64,/', $croppie_code, $type)) {
$encoded_base64_image = substr($croppie_code, strpos($croppie_code, ',') + 1);
$type = strtolower($type[1]);
$decoded_image = base64_decode($encoded_base64_image);
$resized_image = Image::make($decoded_image)->resize(300, 200);
// AND NOW I WANT TO STORE $resized_image using Laravel filesystem BUT...
}
}
Finally, I want to store the resized image using Laravel's filesytem (File Storage) and that's where I'm stuck - when I try this:
Storage::put($path, (string) $resized_image->encode());
... it doesn't work. Actually, it is working something - it looks like there is some memory leak or something, the browser's tab freezes, my RAM & CPU usage go high...
So I just tried:
dd($resized_image->encode());
... and yes, this is where it definitely crashes - when using encode() method.
I am not sure why, maybe this is happening because I'm not working with a standard image upload but with decoded base64?
But, on the other side, Intervention Image can create a new image instance from the base64 as well as from the decoded base64: http://image.intervention.io/api/make
... and, in my case, this works OK:
$resized_image = Image::make($decoded_image)->resize(300, 200);
I could then use the save() method and everything would work OK. But I need to use Laravel's File Storage.
Do you know how I can handle this?
Assuming you're using latest version of Laravel (5.7):
you can use stream method like so:
// use jpg format and quality of 100
$resized_image = Image::make($decoded_image)->resize(300, 200)->stream('jpg', 100);
// then use Illuminate\Support\Facades\Storage
Storage::disk('your_disk')->put('path/to/image.jpg', $resized_image); // check return for success and failure
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
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;
}
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'];
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.