温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - How to save uploaded image to Storage in laravel?
laravel laravel-5 php

php - 如何将上传的图像保存到laravel中的存储中?

发布于 2020-03-27 15:44:19

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

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
被浏览
121
Avik Aghajanyan 2017-02-02 07:01

你需要做

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