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.
Convert image to base64 string with 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')->path()));
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;
}
}
Thank you for reading this blog.
Read Also: How to Get Current Route Name in Laravel