Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SergioGregorutti's avatar

Saving an Intervention Image instance into Amazon S3

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!

0 likes
24 replies
SergioGregorutti's avatar

I read something about using this:

$image->stream()

But the error still persist.

1 like
SergioGregorutti's avatar
Level 3

@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()".

51 likes
cwolfenberger's avatar

@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.

1 like
mfakhrys's avatar

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

1 like
Thatdoorsajar's avatar

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

8 likes
jfranc014's avatar

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.

1 like
murrayw's avatar

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's avatar

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

bristoldigital's avatar

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's avatar

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's avatar

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

danielcoimbra's avatar

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
);
18 likes
FredVanelli's avatar

The following worked for me:

  • Laravel 5.4
  • Image Intervention 2.4
$image = Image::make($request->file('my_file'));

//Edit image with intervention...

//Store with stream()
Storage::put('path/filename.jpg', $image->stream());
                
andreMart's avatar

My working code is:


 if ($request->hasFile('image')) {

            $file = $request->file('image');
        
        // never has a name conflit 
            
            $imageName = uniqid(date('YmdHis')) . '.' . $file->getClientOriginalName();

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

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

            
            $resource = $img->stream()->detach();
    //add public
            $storagePath = Storage::disk('s3')->put(
                'users/' . $imageName,
                $resource,'public'
            );

1 like
Firemaps's avatar
            $filepath = $y.'/'.$m.'/'.$d.'/'.$filename;

            $s3 = \Storage::disk('s3');

            $s3->put($filepath, $image->stream(), 'public');

            $awsPath = $s3->url($filepath);
rktaxali's avatar

The following code works for me

             // save in AWS s3
                $filepath = "client/profile/".$client_id . '/'. uniqid(date('YmdHis')) . '-' .  $basename;  
               $width =  config('app.clientProfileImageWidth') ;
               $image = Image::make($request->file('file'))->resize( $width, null, function ($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                })->encode('jpg', 60);
        
                $ok =Storage::disk('s3')->put($filepath, $image->stream());


                if ($ok)
                {
                    $upload_path =  $filepath;
                    $returnArray['upload_path'] =   $filepath;
                    $returnArray['path'] =env('AWS_S3_SILKWEB_URL') .   $filepath;
                }
    

Please or to participate in this conversation.