Convert image to base64 string with Laravel - TechvBlogs

Convert image to base64 string with Laravel

In this article, You will learn How to Convert image to base64 string with Laravel.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 month ago

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')->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;
    }
}

Thank you for reading this blog.

Read Also: How to Get Current Route Name in Laravel

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.