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 Record And Play Audio In JavaScript
How To Record And Play Audio I...

In this article, we will see how to record and play audio in javascript. Here, we will learn about how to record au...

Read More

Feb-20-2023

How to Generate Fake Data using Tinker in Laravel 11
How to Generate Fake Data usin...

Hello, laravel web developers! In this article, we'll see how to generate fake data using Tinker in laravel 11....

Read More

May-22-2024

Laravel 9 Phone Number Validation Using Regex
Laravel 9 Phone Number Validat...

In this article, we will see laravel 9 phone number validation using regex. In laravel 9 we will see different meth...

Read More

Oct-25-2022

Laravel 8 Many To Many Relationship Example
Laravel 8 Many To Many Relatio...

In this example we will see laravel 8 many to many relationship example. Use many to many relationship in lara...

Read More

Nov-15-2021