How To Convert Image To Base64 In Laravel 9

Websolutionstuff | Dec-29-2022 | Categories : Laravel PHP

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

Recommended Post
Featured Post
How To Image Upload With Progress Bar Angular 15
How To Image Upload With Progr...

As an Angular 15 developer, I understand the importance of incorporating image upload functionality with a progress bar...

Read More

Jun-21-2023

How to Get All Routes in Laravel 10
How to Get All Routes in Larav...

Hey there! This tutorial guides you through the process of retrieving a comprehensive list of all routes in a Larav...

Read More

Dec-13-2023

How to Create Artisan Command in Laravel with Arguments Support?
How to Create Artisan Command...

Laravel is a comprehensive framework that provides an extensive range of artisan commands, enabling the automation of di...

Read More

Oct-06-2023

Queen Elizabeth II Portrait Using CSS
Queen Elizabeth II Portrait Us...

In this article, we will see Queen Elizabeth II portrait using CSS. Elizabeth II was Queen of the United Kingd...

Read More

Sep-11-2022