PeraMika
PeraMika

Reputation: 3688

Intervention Image + Laravel's File Storage: Store resized/decoded base64 image (Intervention Image encode() doesn't work)

Finally, I want to store the resized image using Laravel's filesytem (File Storage) and that's where I'm stuck - when I try this:

Storage::put($path, (string) $resized_image->encode());

... it doesn't work. Actually, it is working something - it looks like there is some memory leak or something, the browser's tab freezes, my RAM & CPU usage go high...

So I just tried:

dd($resized_image->encode());

... and yes, this is where it definitely crashes - when using encode() method.

I am not sure why, maybe this is happening because I'm not working with a standard image upload but with decoded base64?

But, on the other side, Intervention Image can create a new image instance from the base64 as well as from the decoded base64: http://image.intervention.io/api/make ... and, in my case, this works OK:

$resized_image = Image::make($decoded_image)->resize(300, 200);

I could then use the save() method and everything would work OK. But I need to use Laravel's File Storage.

Do you know how I can handle this?

Upvotes: 0

Views: 3813

Answers (1)

Morteza
Morteza

Reputation: 73

Assuming you're using latest version of Laravel (5.7):

you can use stream method like so:

// use jpg format and quality of 100
$resized_image = Image::make($decoded_image)->resize(300, 200)->stream('jpg', 100);
// then use Illuminate\Support\Facades\Storage
Storage::disk('your_disk')->put('path/to/image.jpg', $resized_image); // check return for success and failure

Upvotes: 3

Related Questions