Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel - Resize images and save to Amazon S3

When the user upload his profile pic i want to create 3 versions of this image with different sizes, then upload everything to amazon s3.

I use image intervention package for resizing images, this my code so far.

public function store(Request $request){

    if($request->has('avatar')){

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

        $filename = md5(time()).'_'.$avatar->getClientOriginalName();

        $normal = Image::make($avatar)->resize(160, 160);

        $medium = Image::make($avatar)->resize(80, 80);

        $small = Image::make($avatar)->resize(40, 40);

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/normal/'.$filename, fopen($normal, 'r+'), 'public');

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/medium/'.$filename, fopen($medium, 'r+'), 'public');

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/small/'.$filename, fopen($small, 'r+'), 'public');

        $user = User::findorFail(Auth::user()->id);
        $user->avatar = $filename;
        $user->save();

        return redirect()->back();
    }

}

When i try to submit the file i got this error.

fopen(): Filename cannot be empty

Any help is appreciated

like image 559
Idilassi Jassi Avatar asked Jun 24 '18 15:06

Idilassi Jassi


People also ask

How do you save pictures on Amazon S3?

You can create a dataset using images stored in an Amazon S3 bucket. With this option, you can use the folder structure in your Amazon S3 bucket to automatically classify your images. You can store the images in the console bucket or another Amazon S3 bucket in your account.

Is S3 good for storing images?

The more efficient and cost-effective option is to use AWS's S3 service for storing the image files. Using S3 is a very low-cost option. Effectively, all you are paying for is transferring files into an S3 bucket and serving those images to your users.


1 Answers

UPDATE i made it work like this, if someone has the same issue i hope he will find this code helpful.

public function store(Request $request){

    if($request->has('avatar')){

        $avatar = $request->file('avatar');
        $extension = $request->file('avatar')->getClientOriginalExtension();

        $filename = md5(time()).'_'.$avatar->getClientOriginalName();

        $normal = Image::make($avatar)->resize(160, 160)->encode($extension);
        $medium = Image::make($avatar)->resize(80, 80)->encode($extension);
        $small = Image::make($avatar)->resize(40, 40)->encode($extension);

        //$path = '/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename;

        //dd($normal);

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename, (string)$normal, 'public');

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/medium/'.$filename, (string)$medium, 'public');

        Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/small/'.$filename, (string)$small, 'public');

        $user = User::findorFail(Auth::user()->id);
        $user->avatar = $filename;
        $user->save();

        return redirect()->back();
    }

}
like image 121
Idilassi Jassi Avatar answered Jan 01 '23 07:01

Idilassi Jassi