In this article, we will see how to convert an image to base64 in laravel 9. Here, we will convert the image to base64 in laravel 7, laravel 8, and laravel 9. Sometimes we are required to store the base64 string instead of the image then we will be required to convert the image to base64.
So, let's see laravel 9 converts the image to base64, and laravel 7/8/9 converts the image to base64.
Example 1:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request){
$image = base64_encode(file_get_contents($request->file('image')->path()));
echo $image;
}
}
Example 2:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request){
$image = public_path('storage/img/test.jpg');
$base64 = base64_encode(file_get_contents($image));
echo $base64;
}
}
Example 3:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request){
$image = storage_path('app/public/img/test.jpg');
$base64 = base64_encode(file_get_contents($image));
echo $base64;
}
}
You might also like:
In this article, we will see the laravel 9 and laravel 10 multiple where condition query example. We will learn&nbs...
Sep-30-2022
Today we will learn how to use sweetalert2 In laravel, You can use sweetalert2 in laravel as well as php, ...
May-03-2021
In this article, we will see laravel 9 authentication using inertia js. Here, you can learn how to authenticat...
Dec-05-2022
In this tutorial we will see laravel 8 socialite login with github account. explains how to integrate OAuth github...
Oct-25-2021