Home / Snippets / Intervention Image encode image to base64 format in Laravel
Intervention Image encode image to base64 format in Laravel cover

Intervention Image encode image to base64 format in Laravel

1.4K

3 years ago

0 comments

To encode an image to base64 format in Laravel you can make use Intervention Image package.

Intervention Image


By using the Intervention Image package, you can call out the "encode()" method and pass in the "data-url" as the parameter to get the base64 value.

From the Laravel disk you can retrieve the image and pass it on to the Intervention Image "make()" method like below.
<?php

use Intervention\Image\Facades\Image;

$userPosterImage = User::first()
    ->poster_image;

$imageLocation = \Storage::disk('public')
    ->url($userPosterImage);

$imageInBase64 = (string) Image::make($imageLocation)
    ->encode('data-url');
Now that you have the image in base64 format you can pass it to the image tag and preview it on your browser.
<img src="{{ $imageInBase64 }}">
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this