Saving an Intervention Image instance into Amazon S3

Published 2 years ago by SergioGregorutti

Hi,

Im using Intervention Image package to create thumbnails, etc. And I want to save the file into a Amazon S3 bucket.

This gives me an error:

...
$image = Image::make($image)->widen(800);
Storage::disk('s3')->put($path.$file, $image);
fstat() expects parameter 1 to be resource, object given

I think that I can save the image on my public directory and then copy the file into the bucket.

But, is there a best approach to do this? Like create the image and then save it into the bucket?

Thanks!

Best Answer (As Selected By SergioGregorutti)
SergioGregorutti

@JustinLien Saving it to AWS.

But finally I fixed the error and now is working.

Here´s is the code. Maybe it will help someone on the future:

...
$image_normal = Image::make($image)->widen(800, function ($constraint) {
            $constraint->upsize();
        });
        $image_thumb = Image::make($image)->crop(100,100);
        $image_normal = $image_normal->stream();
        $image_thumb = $image_thumb->stream();

        Storage::disk('s3')->put($path.$file, $image_normal->__toString());
        Storage::disk('s3')->put($path.'thumbnails/'.$file, $image_thumb->__toString());

The key was using "->stream()" and then "->__toString()".

SergioGregorutti

I read something about using this:

$image->stream()

But the error still persist.

JustinLien

@SergioGregorutti are you having issue with creating the image or saving it to AWS? Or both?

SergioGregorutti

@JustinLien Saving it to AWS.

But finally I fixed the error and now is working.

Here´s is the code. Maybe it will help someone on the future:

...
$image_normal = Image::make($image)->widen(800, function ($constraint) {
            $constraint->upsize();
        });
        $image_thumb = Image::make($image)->crop(100,100);
        $image_normal = $image_normal->stream();
        $image_thumb = $image_thumb->stream();

        Storage::disk('s3')->put($path.$file, $image_normal->__toString());
        Storage::disk('s3')->put($path.'thumbnails/'.$file, $image_thumb->__toString());

The key was using "->stream()" and then "->__toString()".

cwolfenberger

@SergioGregorutti Thanks for posting this. I've been having the same issue and was racking my brain for an answer. I hadn't seen anything about ->stream() until now.

mfakhrys

Thank you for this post.. It really helped me..

Thatdoorsajar

I was having the same problem as people here but none of the above solved. This was how I managed to get image uploads to s3 to work:

$image = Image::make($image)->encode('png');
Storage::disk('s3')->put($path.$file, $image->getEncoded());

This might be because I was encoding all my images to png format so I needed to grab the encoded value. I am using Intervention\Image 2.1. Thanks

jfranc014

Hi. Thanks for this answer @SergioGregorutti . I was having the same issue after upgrading to Laravel 5.1. I don't know if this was the exact reason for the problem, however thanks to this post it was solved.

manogi
manogi
2 years ago (28,595 XP)

Wow - this mega helped me. Thanks!

murrayw
murrayw
1 year ago (10,420 XP)

Thankyou, this saved me so much!

It's very tempting to assume there is an issue with AWS permissions instead of the image format being incorrect.

halcyon

thank you for the answer @Thatdoorsajar

i am using intervention/image 2.3.8 and your method is the one that works for me too

ringwoodinternet

A succinct version of the accepted answer. I didn't need '__toString()' and I chained the ->stream(); (github down so can't markup)

$pathAndFileName = 'test/5a0f77271aa350a3b35b147f684ffd13.jpeg'; // located in '/storage/app/public/test/*.jpg' $image = Image::make( Storage::disk('public')->get($pathAndFileName) )->resize(320,320)->stream();

$path = Storage::disk('public')->put('/test/5a0f77271aa350a3b35b147f684ffd13.jpeg', $image);

jus7uice

Dear all,

i got some error like this:

FatalErrorException in PostController.php line 68: Call to undefined method GuzzleHttp\Psr7\Stream::stream()

in this line:

image_normal = $image_normal->stream(); $image_thumb = $image_thumb->stream();

Can someone help me? Thank you.

troygilbert

The Image::__toSring() method is equivalent to Image::getEncoded()

danielcoimbra

My working code is:

$file = request()->file('my_input_file_name');

$imageName = $file->getClientOriginalName();

$img = Image::make($file);

$img->resize(null, 1000, function ($constraint) {
    $constraint->aspectRatio();
});

//detach method is the key! Hours to find it... :/
$resource = $img->stream()->detach();

$path = Storage::disk('s3')->put(
    'my-s3-folder/' . $imageName,
    $resource
);
DeadMerc

@danielcoimbra thank you very much, you example works for me.

Please sign in or create an account to participate in this conversation.