ItSolutionStuff.com

How to Convert Image to Base64 in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hi Friends,

In this tute, we will discuss laravel convert image to base64. you can see how to convert image to base64 in laravel. We will look at an example of convert image to base64 laravel. This post will give you a simple example of convert image into base64 laravel. So, let's follow a few steps to create an example of convert image to base64 php laravel.

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

Sometimes, we require to convert image to base64 string in laravel application. In this example, I will give you two examples to convert image to base64 string. in the first example, we will take image path and convert it into base64 image. in the second example, we will take the file object and convert it into base64 string.

without any ado, let's see examples of code.

Example 1:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$imagePath = public_path("images/20220405140258.jpg");

$image = "data:image/png;base64,".base64_encode(file_get_contents($imagePath));

dd($image);

}

}

Output:

data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQE....

Example 2:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'file' => 'required',

]);

$image = "data:image/png;base64,".base64_encode(file_get_contents($request->file('file')->path()));

dd($image);

}

}

Output:

data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQE....

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Convert JSON to Array in Laravel?

Read Now →

How to use Carbon in Laravel Blade or Controller File?

Read Now →

How to Call Controller Function in Blade Laravel?

Read Now →

How to Override Auth Login Method in Laravel 8?

Read Now →

How to use Google Recaptcha V3 in Laravel App?

Read Now →

Laravel Convert PDF to Image Example

Read Now →

How to add Default Value of Column in Laravel Migration?

Read Now →

Laravel Carbon Convert String to Date Example

Read Now →

Laravel Carbon Check If Date is Greater Than Other Date

Read Now →

How to Convert Object to Array in Laravel?

Read Now →

How to Get Last Inserted Id in Laravel?

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →