Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intervention Image - save to variable in base64 encoded format

I'm using Laravel with image manipulating package Intervention Image.

I want to save cropped image to variable and then to DB but can't find in documentation how to export result as string. Here's my code:

$img = Image::make($uploadedImage);
$img->crop(160, 210);
$imageEncoded = // ?

There's save(), but it only saves to file.

How can I export modified Intervention Image to string variable? (data:image/jpeg;base64,…)

like image 783
Limon Monte Avatar asked Nov 07 '15 17:11

Limon Monte


People also ask

How do I save Base64 encoded image?

ToBase64String method. Now in order to save the Base64 encoded string as Image File, the Base64 encoded string is converted back to Byte Array using the Convert. FromBase64String function. Finally the Byte Array is saved to Folder on Disk as File using WriteAllBytes method of the File class.

Can you Base64 encode an image?

Base64 is an encoding algorithm that converts any characters, binary data, and even images or sound files into a readable string, which can be saved or transported over the network without data loss. The characters generated from Base64 encoding consist of Latin letters, digits, plus, and slash.

How is an image converted to Base64?

To get to a base64 representation:Paint uses its png encoder to convert the bitmap of pixels into a stream of bytes that it compresses and appends headers to so that when it sees those bytes again it knows how to display them. Image is written to disk as series of bytes. You run a base64 encoder on smile. png .

How do I save a file in Base64?

How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.


1 Answers

You can use encode for that.

$data = (string) Image::make('public/bar.png')->encode('data-url');
like image 200
Drown Avatar answered Sep 28 '22 07:09

Drown