我修改头像后,将头像保存在octobercms数据库中

时间:2018-10-24 16:38:30

标签: laravel octobercms

使用干预图像调整用户头像的大小后,我尝试将其存储在octobercms数据库中,如下所示:

        if (Input::hasFile('avatar')) {


    $file= Input::file('avatar');

  $filenamewithextension =   $file->getClientOriginalName();


   //get filename without extension
           $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);


  //get file extension
      $extension = $file->getClientOriginalExtension();


   //filename to store
           $filenametostore = $filename.'_'.time().'.'.$extension;





            Storage::put('public/profile_images/'. $filenametostore, fopen($file, 'r+'));
            Storage::put('public/profile_images/thumbnail/'. $filenametostore, fopen($file, 'r+'));



    //Resize image here
           $thumbnailpath ='storage/app/public/profile_images/thumbnail/'.$filenametostore;


             $img = Image::make($file->getRealPath());
             $img->crop(request('w'), request('h'), request('x1'), request('y1'));


      $img->save($thumbnailpath);

        $user->avatar=  $filenametostore;


        }

我收到此错误:

The avatar must be an image.
C:\wamp643\www\october3\vendor\october\rain\src\Database\Traits\Validation.php line 340

我真的不知道该怎么做,我是一个初学者。

请帮助我!

1 个答案:

答案 0 :(得分:0)

您只是在设置文件名,应该在此处设置图片的完整路径。

  

假设您已经在用户模型中拥有avatar个附件关系

public $attachOne = [
    'avatar' => ['System\Models\File']
];
  

替换

$user->avatar = $filenametostore;
  

$user->avatar = storage_path($thumbnailpath);

也许应该有用。

如有疑问,请发表评论。