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
CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023

Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Exa...

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. I...

Read More

Nov-24-2021

How To Send Email In Laravel 9 Using Mailtrap
How To Send Email In Laravel 9...

In this article, we will explore the process of sending emails in Laravel 9 using Mailtrap. We will delve into how Larav...

Read More

Jul-27-2022

How To Create Zip File In Laravel 7/8
How To Create Zip File In Lara...

In this tutorial I will give you example of how to create zip file in laravel 7/8. Some times client's have requirme...

Read More

Dec-17-2021