How to save uploaded image to Storage in laravel?

前端 未结 4 1439
太阳男子
太阳男子 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

    Here is another way to save images using intervention package on storage path with desired name. (using Storage::putFileAs method )

    public function store(Request $request)
    {
        if ($request->hasFile('photo')) {
    
            $image      = $request->file('photo');
            $image_name = time() . '.' . $image->extension();
    
            $image = Image::make($request->file('photo'))
                ->resize(120, 120, function ($constraint) {
                    $constraint->aspectRatio();
                 });
    
            //here you can define any directory name whatever you want, if dir is not exist it will created automatically.
            Storage::putFileAs('public/images/1/smalls/' . $image_name, (string)$image->encode('png', 95), $image_name);
        }
    }
    
    

提交回复
热议问题