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
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.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With