如何将上传的图片保存到 laravel 中的 Storage?

新手上路,请多包涵

我正在使用图像干预将图像保存到存储文件夹。我有下面的代码,它似乎只是用空白图像保存了一个文件名。我想我需要一种方法将文件内容写入文件夹但为片段而苦苦挣扎。

 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();
            });

            //dd();
            Storage::disk('local')->put('images/1/smalls'.'/'.$fileName, $img, 'public');

原文由 Eden WebStudio 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 931
2 个回答

你需要做

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');
}

原文由 Avik Aghajanyan 发布,翻译遵循 CC BY-SA 3.0 许可协议

简单的代码。

 if($request->hasFile('image')){
    $object->image = $request->image->store('your_path/image');
}

谢谢。

原文由 Raja 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题