Convert image to base 64 string with Laravel
Laravel's $request->file()
doesn't return the actual file content. It returns an instance of the UploadedFile
-class.
You need to load the actual file to be able to convert it:
$image = base64_encode(file_get_contents($request->file('image')->path()));
Convert existed images to base64 in laravel php
If your upload images in public folder try:
$path = public_path('images/upload/file_name');
If your upload images in Storage public folder try:
$path = storage_path('images/upload/file_name');
To encode the image to base64 try:
base64_encode($file_path);
Laravel File Storage: How to store (decoded) base64 image?
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
.
How to convert base64 image to UploadedFile Laravel
I believe file
parameter on setImage
is not a File
object. So the $request->file('file')
is null
, because you attach a string
(base64), not a file.
You told us that output from console.log
is base64 path, then you need to convert that (base64) to file.
Since you're using Laravel, here is the technique:
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\File;
.....
$base64File = $request->input('file');
// decode the base64 file
$fileData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64File));
// save it to temporary dir first.
$tmpFilePath = sys_get_temp_dir() . '/' . Str::uuid()->toString();
file_put_contents($tmpFilePath, $fileData);
// this just to help us get file info.
$tmpFile = new File($tmpFilePath);
$file = new UploadedFile(
$tmpFile->getPathname(),
$tmpFile->getFilename(),
$tmpFile->getMimeType(),
0,
true // Mark it as test, since the file isn't from real HTTP POST.
);
$file->store('avatars');
Update
Since you're using vue-image-upload-resize, I check the documentation that it has built in function to change the output from base64 to blob, so you can just:
<image-uploader
...
output-format="blob"
... />
how to save base64 image decode to public folder in laravel
//Controller
use Illuminate\Support\Facades\Storage;
//Inside method
$image = $request->image; // your base64 encoded
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = str_random(10) . '.png';
Storage::disk('local')->put($imageName, base64_decode($image));
Also, make sure that your local
disk is configured like that in /config/filesystems.php
'local' => [
'driver' => 'local',
'root' => storage_path('app/public'),
]
With that, file will be saved in /storage/app/public
directory.
Don't forget to write php artisan storage:link
to make files from that directory available in /public
directory, so users will can retrieve them.
Laravel Api return image as base 64 string
Use an accessor: https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor
Add this to your model:
protected $appends = ['imageString'];
public function getImageStringAttribute() {
$type = pathinfo($this->image, PATHINFO_EXTENSION);
$data = file_get_contents($this->image);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
return $base64;
}
Related Topics
How to Remove the Extra Square Bracket from Json Having Multiple Arrays
Alert Show Up When I Refresh Page
Find Combination(S) Sum of Element(S) in Array Whose Sum Equal to a Given Number
Success Message Pop-Up After from Submit Button Clicked
On a Function That Gets Settings from a Db I Ran into the Error
How to Change Default Url from Localhost:8000 to Other Ip in Laravel When We Run "Php Artisan Serve"
How to Update Multiple Select Box Values in PHP MySQL
How to Remove Duplicate Rows in Laravel
Install PHP Intl Extension on Macos
Regex to Check the String Contains Only Letter and Numbers But Not Only Numbers
What Is the Default Username and Password of Phpmyadmin
How to Get Last Insert Id in Sqlite
Unable to Upload File Greater Than 2Mb in Laravel
Undefined Index: User_Id. Where, User_Id Is Session Variable