0
  • 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 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?

PeraMika
  • 3,539
  • 9
  • 38
  • 63

1 Answers1

3

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
Morteza
  • 73
  • 1
  • 13