Laravel 9 How to Convert Image to Base64 Tutorial

Reading Time: 3 minutes
1,306 Views

Inside this article we will see the concept i.e Laravel 9 How to Convert Image to Base64 Tutorial. Article contains the classified information about How to Convert Image to Base64 in PHP Laravel.

If you are looking for a solution i.e Convert image to base 64 string with Laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Base64 is a binary to text encoding scheme. Binary data is represented as an ASCII string. Typically, Base64 encoding is used to encode binary data for transmission over text based media without changing the original byte stream.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Convert Image To Base64

Let’s assume a controller i.e DemoController.php. And also an image into /public folder. Image path is supposed to be /public/images/1658717898.png

Controller code will be look like this.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $imagePath = public_path("images/1658717898.png");
        $image = "data:image/png;base64,".base64_encode(file_get_contents($imagePath));
  
        echo $image;
    }
}

Output

data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQE....

Example #2: Convert Image To Base64

Let’s assume a controller i.e DemoController.php. Suppose you are uploading an image from a form.

We want to convert the uploaded image into base64 encoded string. You need to do, controller code will be look like this.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request)
    {
        $request->validate([
            'file' => 'required',
        ]);
        $image = "data:image/png;base64,".base64_encode(file_get_contents($request->file('file')->path()));
        
        echo $image;
    }
}

file is the name attribute of file upload button.

We hope this article helped you to learn Laravel 9 How to Convert Image to Base64 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.