无法从文件中读取图像()

时间:2017-05-16 03:09:22

标签: php laravel

我尝试让我的用户上传他们的个人资料图片,当我点击提交按钮保存更改时会出现此错误

NotReadableException in Decoder.php line 20: Unable to read image from file ().

这是我在控制器中的更新方法:

public function update(Request $request, $id)
    {
        $user = User::find($id);
        $this->validate($request, array(
          'first_name' => 'required|string|max:255',
          'last_name' => 'required|string|max:255',
          'gender' => 'required',
          'birthday' => 'required|date_format:d/m/Y|max:25',
          'email' => [
              'required', 'string', 'nullable', 'email', 'max:255',
              Rule::unique('users')->ignore($user->id),
            ],
          'password' => 'nullable|string|min:6|confirmed',
          'user_image' => 'sometimes|image',
        ));

        $user = User::find($id);

        $user->first_name = $request->input('first_name');
        $user->last_name = $request->input('last_name');
        $user->gender = $request->input('gender');
        $user->birthday = $request->input('birthday');
        $user->email = $request->input('email');
        if (trim(Input::get('password')) != '') {
          $user->password = Hash::make(trim(Input::get('password')));
        }


        //store user image
        if ($request->hasFile('user_image')) {
          $avatar = $request->file('user_image');
          $filename = time() . '.' . $avatar->getClientOriginalExtension();
          $location = public_path('avatars/' . $filename);
          Image::make($avatar)->resize(200, 200)->save($location);

          $user->image = $filename;
        }

        $user->save();

        Session::flash('success', 'Your information was successfully updated.');

        return redirect()->route('jobseeker.profile', $user->id);
    }

这是我的刀片模板图片部分:

<div class="panel-body">
<p>
@if (Auth::user()->image && File::exist(public_path("avatars/" . Auth::user()->image)))
<img src="{{ Auth::user()->image }}" alt="user image" class="img-responsive" >
 @else
<img src="{{ 'https:://www.gravatar.com/avatar/' . md5(strtolower(trim(Auth::user()->email))) . '?s=200&d=wavatar' }}" alt="user image" class="img-responsive" >
   @endif
</p>
<p>
   {{ Form::label('user_image', 'Upload Your Image') }}
   {{ Form::file('user_image') }}
</p>
</div>

我正在使用Laravel 5.4并上传图像我正在使用干预图像。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我修复了删除Image :: make并将代码更改为:

的问题
//store user image
        if ($request->hasFile('user_image')) {
          $avatar = $request->file('user_image');
          $filename = time() . '.' . $avatar->getClientOriginalExtension();
          $location = public_path('avatars/');
          $request->file('user_image')->move($location, $filename);


          $user->image = $filename;
        }

如果你有更好的主意,请分享。