Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 990
PeraMika Avatar asked Jul 12 '18 03:07

PeraMika


1 Answers

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.

like image 109
Brian Lee Avatar answered Nov 15 '22 16:11

Brian Lee