DoeJohn's avatar

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

  • I am using Croppie jQuery plugin which returns the cropped image encoded in base64.

  • After submitting the form (with the cropped image encoded in base64) - I decode & resize it using the Intervention Image library:

        public function decodeResizeAndStore(Request $request)
        {
            $croppie_code = $request->croppie_code;
            
            // https://stackoverflow.com/a/11511605/4437206
            if (preg_match('/^data:image\/(\w+);base64,/', $croppie_code, $type)) {
                $encoded_base64_image = substr($croppie_code, strpos($croppie_code, ',') + 1);
                $type = strtolower($type[1]);
    
                $decoded_image = base64_decode($encoded_base64_image);
    
                $resized_image = Image::make($decoded_image)->resize(300, 200);
    
                // AND NOW I WANT TO STORE $resized_image using Laravel filesystem BUT...
            }
        }

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, 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?

0 likes
1 reply
morkramer's avatar

In this case we may need to use "stream()" method like following

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

Please or to participate in this conversation.