Laravel 9 How to Convert Image to Base64 Tutorial

Share this Article
Reading Time: 3 minutes
297 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5