How to save uploaded image to Storage in laravel?

前端 未结 4 1438
太阳男子
太阳男子 2020-12-29 14:45

I am using Image intervention to save an image to the storage folder. I have the code below and it seems to just save a file name with a blank image. I think I need a way fo

4条回答
  •  半阙折子戏
    2020-12-29 15:22

    You need to do

    if ($request->hasFile('photo')) {
                $image      = $request->file('photo');
                $fileName   = time() . '.' . $image->getClientOriginalExtension();
    
                $img = Image::make($image->getRealPath());
                $img->resize(120, 120, function ($constraint) {
                    $constraint->aspectRatio();                 
                });
    
                $img->stream(); // <-- Key point
    
                //dd();
                Storage::disk('local')->put('images/1/smalls'.'/'.$fileName, $img, 'public');
    }
    

提交回复
热议问题