Intervention Image - save to variable in base64 encoded format
Asked Answered
A

1

12

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,…)

Angloamerican answered 7/11, 2015 at 17:25 Comment(0)
B
27

You can use encode for that.

$data = (string) Image::make('public/bar.png')->encode('data-url');
Brachypterous answered 7/11, 2015 at 17:47 Comment(3)
Thank you @Drown, exactly what I needed.Angloamerican
this is for creating a data-uri that is specifically for img src tags, if you want the plain base64, try $b64 = base64_encode($img->encode()->encoded);Headlong
@parse Your proposed edit would have been better as separate answer, in my opinion.Ethridge

© 2022 - 2024 — McMap. All rights reserved.