How to Convert image to base64 string with Laravel

BOOKC, this time share about How to Convert image to base64 string with Laravel

How to Convert image to base64 string with Laravel - While developing some projects there could be requirements to store base64 strings instead of the actual image then We need to convert the image to base64 strings. In this short article, We will learn How to convert an image to a base64 string with Laravel. We will cover all possibilities to convert the image to a base64 string in laravel.

Method 1

<?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;
    }
}

Method 2

<?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;
    }
}

Method 3

<?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;
    }
}

A few articles about How to Convert image to base64 string with Laravel Hopefully useful for you.

 

 

 

Bookc
Admin

Hopefully the information we provide can be useful.

Press ESC to close