使用图像干预包调整大小后,将图像存储在October CMS数据库中

时间:2018-10-23 16:44:14

标签: laravel octobercms

我使用干预图像包调整用户头像的大小后,尝试将用户头像存储在October CMS数据库中,我使用以下代码:

if (Input::hasFile('avatar')) {
    $file= $user->avatar;
    $filenamewithextension = $file->getClientOriginalName();
    //get filename without extension
    $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
    //get file extension
    $extension = $file->getClientOriginalExtension();
    //filename to store
    $filenametostore = $filename.'_'.uniqid().'.'.$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;
}

但是我得到一个错误:

Call to undefined method October\Rain\Database\QueryBuilder::getClientOriginalName()

有人可以帮我吗?!

1 个答案:

答案 0 :(得分:0)

您在错误的对象上调用getClientOriginalName(),请尝试将代码中的以下行更改为此:

if (Input::hasFile('avatar')) {
    $file = Input::file('avatar');
    /* ... */
    Storage::put('public/profile_images/'. $filenametostore, fopen($file->getRealPath(), 'r+'));
    Storage::put('public/profile_images/thumbnail/'. $filenametostore, fopen($file->getRealPath(), 'r+'));
    /* ... */
}