Convert Image to Base 64 String With Laravel

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')->pat‌​h()));

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



Leave a reply



Submit