Search code examples
phplaravelamazon-s3intervention

Uploading to S3 with Laravel Intervention image returns boolean and not path


I am having an issue with uploading an image in Laravel. Sometimes the image needs reorienting - when doing this with Intervention image it simply returns a boolean instead of the path of the image in s3. When using it without Intervention Image it works fine

I've tried exif reading data and using imagerotate to no avail, it errors out

I run the following

$image = $request->file('photo');
$path = \Storage::disk('s3')->put('users/'.\Auth::id().'/posts', $image, 'public');
dd($path); // /users/1/posts/39grjigrje.jpg

the $path variable is great and is the s3 path, however running:

$image = $request->file('photo');
$image = \Image::make($image);
$image->orientate();
$path = \Storage::disk('s3')->put('users/'.\Auth::id().'/posts', $image, 'public');
dd($path); // true

the $path variable is simply just a boolean and doesn't return the stored file path

I expect a path e.g. /images/1/kfjeieuge.jpg which I get when i don't use intervention image, when I use intervention I get a boolean.


Solution

  • In first example:

    $image = $request->file('photo');
    $path = \Storage::disk('s3')->put('users/'.\Auth::id().'/posts', $image, 
    'public');
    

    $request->file('photo') file will return instance of Illuminate\Http\UploadedFile class. you can check it here in documentation link.

    And as Tarun said put method check for instance of Illuminate\Http\UploadedFile class. So in that case, it will uploaded successful.

    In second example:

    $image = $request->file('photo');
    $image = \Image::make($image); //here
    $image->orientate();
    $path = \Storage::disk('s3')->put('users/' . \Auth::id() . '/posts', $image, 'public');
    

    You are overwriting $image variable with instance Image class (second line). Which is wrong. You need to pass Stream or file.

    Try below code:

    $image = $request->file('photo');
    $image = \Image::make($image); //here
    $image->orientate()->encode('jpg');
    $filename = time() . '.jpg';
    $path = \Storage::disk('s3')->put('users/' . \Auth::id() . '/posts/'.$filename, $image->getEncoded(), 'public');