How to Upload Base64 Encoded Images with Laravel?

11-Oct-2022

.

Admin

How to Upload Base64 Encoded Images with Laravel?

Hello Friends,

This tutorial will give you an example of how to convert base64 to an image in laravel. I’m going to show you how to upload base64 encoded images with laravel. it's a simple example of a laravel base64 image upload example. this example will help you laravel decode base64 images with an example. Here, Creating a basic example of how to save base64 images to a public folder in laravel.

Here in this example the base64 content image is converted and saved in the database as well in the public storage folder easy way. You can easily upload base64 images in the s3 AWS server as well

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Example 1: Upload base64 Image in Public Folder


public function store(Request $request)

{

if ($request->image) {

$folderPath = "uploads/";

$base64Image = explode(";base64,", $request->image);

$explodeImage = explode("image/", $base64Image[0]);

$imageType = $explodeImage[1];

$image_base64 = base64_decode($base64Image[1]);

$file = $folderPath . uniqid() . '. '.$imageType;

file_put_contents($file, $image_base64);

}

}

Example 2: Upload base64 File in AWS

public function store(Request $request)

{

if ($request->image) {

$folderPath = "uploads/";

$base64Image = explode(";base64,", $request->image);

$explodeImage = explode("image/", $base64Image[0]);

$imageName = $explodeImage[1];

$image_base64 = base64_decode($base64Image[1]);

$file = $folderPath . uniqid() . '. '.$imageName;

try {

$s3Url = $folderPath . $imageName;

Storage::disk('s3.bucket')->put($s3Url, $base64String, 'public');

} catch (Exception $e) {

Log::error($e);

}

}

}

I hope it can help you...

#Laravel