使用 Laravel 安装了干预,但调整大小的方法不存在

Installed Intervention with Laravel but resize method does not exist

我正在使用 Laravel 5.2 的干预,我已经使用 Composer 和 included Intervention\Image\ImageServiceProvider::class 以及 'Image' => Intervention\Image\Facades\Image::class in the config/app.php

安装了它

我还在使用它的控制器中添加了 use 语句 use Intervention\Image\ImageManager;

这是我尝试处理照片的函数,但是当我提交调用此函数的表单时,我收到此错误消息

 BadMethodCallException in Macroable.php line 81:
    Method resize does not exist.

函数

public function postAvatarUpload(Request $request)
    {
         $this->validate($request, [
            'image' => 'required|image|max:3000|mimes:jpeg,jpg,png',
        ]);
        $user = Auth::user();
        $usersname = $user->username;
        $file = $request->file('image');
        $resizedImg = $file->resize(200,200);
        $ext = $file->getClientOriginalExtension();
        $filename = $usersname . '.' . $ext;
        if (Storage::disk('public')->has($usersname)) {
            Storage::delete($usersname);
        }
           Storage::disk('public')->put($filename, File::get($resizedImg));
           $avatarPath = Storage::url($filename);
            Auth::user()->update([
                'image' => $avatarPath,
            ]);

        return redirect()->route('profile.index', 
                ['username' => Auth::user()->username]);
    }

您正在调用文件的调整大小方法,而不是干预。 如果将 $resizedImg = $file->resize(200,200); 替换为 $resizedImg = Image::make($file)->resize(200,200); 我认为应该可以工作。

首先您应该保存文件,然后使用 make.

方法创建 ImageManager 的实例(对象)

示例:

public function upload(Request $request)
{
    $file = $request->file('image');

    $path = 'path/to';
    $fileName = 'example_name.' . $file->extension();

    $file->move($path, $fileName);

    $image = ImageManager::make($path . DIRECTORY_SEPARATOR . $fileName);
}

此外,您可以使用外观 Intervention\Image\Facades\Image 而不是 ImageManager class。