League/Flysystem fstat() expects parameter 1 to be resource, object given
Asked Answered
I

3

8

I am in the process of upgrading a project from Laravel 5 to 5.1. One package that needed to be updated was League\Flysystem.

I am using Intervention\Image to resize an image and then Flysystem to save it to S3. The code below was working with 5.0 -

// Album ID
$id = $request->input('id');
// Filename for this photo
$filename = str_random() . ".jpg";

// Get the storage disk
$disk = Storage::disk('s3');

// Resize the photo
$image = Image::make($request->file('photo'));
$image->orientate();
$image->resize(1024, 748, function ($constraint) {
            $constraint->aspectRatio();
});
$image->encode('jpg');
// Save the photo to the disk
$disk->put("img/album/$id/$filename", $image);

But now I am receiving the following error: fstat() expects parameter 1 to be resource, object given, thrown in league\flysystem\src\Util.php, line 250.

I am using "intervention/image": "~2.1", "league/flysystem-aws-s3-v3" : "~1.0",

Any ideas what might be causing this?

Implicative answered 9/6, 2015 at 17:6 Comment(0)
S
11

You might have been lucky before some type casting on your $image object made a string out of it, I guess a simple change of your last line to

$disk->put("img/album/$id/$filename", $image->__toString());

will fix the problem and is safer anyway as the put method officially only accepts strings (and looking at the implementation of php resources as wekk).
That should keep you compatible to changes in the long run.

Shipment answered 9/6, 2015 at 17:35 Comment(5)
Now receive Command (_toString) is not available for driver (Gd).Implicative
is that a writing error (_toString) or did you have only one underscore? It should be __toString() with two underscores.Shipment
Sure, so first of all put accepts either a string or a php resource. I guess that it worked before was because somewhere inside the flysystem code a string function or an explicit string cast happened on the input argument (so something in the lines of (string) $image which would do the same as doing $image->__toString()). That is a magic method that is used to convert the given object into a string whenever necessary and intervention Image implements it. _toString is not implemented and throws the error because intervention tries to execute that as Image (manipulation) command.Shipment
Got it, thanks! Was expecting this to be much harder to solve. Really appreciate the help. :)Implicative
Great I could be of help. :) Sometimes if you expect something to be hard to solve you don't see the easy solution, have the same problem sometimes :)Shipment
M
17

The better way to do this is to type cast the encoded output:

http://image.intervention.io/api/encode

$image->encode('jpg');
$disk->put("img/album/$id/$filename", (string) $image);
Metts answered 13/6, 2015 at 16:12 Comment(2)
I actually agree, do the typecast like suggested :)Shipment
This approach works well with Guzzle. Nice and simple.Sthenic
S
11

You might have been lucky before some type casting on your $image object made a string out of it, I guess a simple change of your last line to

$disk->put("img/album/$id/$filename", $image->__toString());

will fix the problem and is safer anyway as the put method officially only accepts strings (and looking at the implementation of php resources as wekk).
That should keep you compatible to changes in the long run.

Shipment answered 9/6, 2015 at 17:35 Comment(5)
Now receive Command (_toString) is not available for driver (Gd).Implicative
is that a writing error (_toString) or did you have only one underscore? It should be __toString() with two underscores.Shipment
Sure, so first of all put accepts either a string or a php resource. I guess that it worked before was because somewhere inside the flysystem code a string function or an explicit string cast happened on the input argument (so something in the lines of (string) $image which would do the same as doing $image->__toString()). That is a magic method that is used to convert the given object into a string whenever necessary and intervention Image implements it. _toString is not implemented and throws the error because intervention tries to execute that as Image (manipulation) command.Shipment
Got it, thanks! Was expecting this to be much harder to solve. Really appreciate the help. :)Implicative
Great I could be of help. :) Sometimes if you expect something to be hard to solve you don't see the easy solution, have the same problem sometimes :)Shipment
T
0

i got version "intervention/image": "^2.4",

The __toString() didn't work for me, the file was created corrupted... i did ->stream()->getContents().

Terminable answered 5/12, 2018 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.