geetpurwar's avatar

Image Resize & Upload to AWS S3

Hi, I am using following code and I am able to resize and upload image using image intervention but can't get correct url after image is uploaded.

This is my code:

function uploadAndReturnLink($file, $path)
{
    //$file = $request->file('file');

    $img = Image::make($file)->resize(300, null, function ($constraint) {$constraint->aspectRatio();});

    $filename = uuid().'.'.$file->getClientOriginalExtension();

        $cloudpath = Storage::disk('s3')->put($path.'/'.$filename, $img->stream()->__toString());

        return Storage::url($cloudpath);
}
0 likes
3 replies
bobbybouwmann's avatar
Level 88

Well your code looks good, but I think you also need to specify the disk on the part where you fetch the url. If the default isn't s3 the url won't work

return Storage::disk('s3')->url($cloudpath);

Also when you want to have a url you only need to specify the file name, which in your case should look like this

return Storage::disk('s3')->url($path . '/' . $filename);

Let me know if that works for you!

Please or to participate in this conversation.