How To Use Laravel To Convert An Image To A Base64 String

Hello Geek.

While developing some projects, it may be necessary to store base64 strings rather than the actual image, in which case we must convert the image to base64 strings. In this article, we will learn how to use Laravel to convert an image to a base64 string. We will go over all of the options for converting an image to a base64 string in Laravel.

First Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function store(Request $request){
        $image = base64_encode(file_get_contents($request->file('image')->pat‌​h()));
        echo $image;
    }
}

Second Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function store(Request $request){
        $image = public_path('storage/banner/15751609757424.jpg');
        $base64 = base64_encode(file_get_contents($image));
        echo $base64;
    }
}

Third Method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function store(Request $request){
        $image = $image = storage_path('app/public/banner/15751609757424.jpg');
        $base64 = base64_encode(file_get_contents($image));
        echo $base64;
    }
}

All the best nerd!

Leave a Reply