Laravel 8:如何正确使用介入影像库

Laravel 8: How To Use Intervention Image Library Properly

我想为我的 Laravel 项目使用 Intervention Image 库,所以我只是通过 Composer 安装它并将这行代码添加到 config/app.php:

Intervention\Image\ImageServiceProvider::class,

并且这一行也被添加到 aliases 部分:

'Image' => Intervention\Image\Facades\Image::class,

现在在我的控制器上,我编写了这个:

class AdminController extends Controller
{
    protected function uploadImages($file)
    {
        $year = Carbon::now()->year;
        $imagePath = "/upload/images/{$year}/";
        $filename = $file->getClientOriginalName();
        $file = $file->move(public_path($imagePath), $filename);
        $sizes = ["300","600","900"];
        Image::make($file->getRealPath())->resize(300,null,function($constraint){
            $constraint->aspectRatio();
        })->save(public_path($imagePath . "300_" . $filename));
    }
}

但是当我填写表格以检查它是否有效时,就会弹出此错误消息:

Error Class 'App\Http\Controllers\Admin\Image' not found

这意味着这一行:

Image::make($file->getRealPath())->resize(300,null,function($constraint){

所以为什么它 returns 这个错误,而我已经将它包含在我的项目中了?!

如果您知道,请告诉我...我将不胜感激。

谢谢

config/app.php 您需要添加:

$provides => [
    Intervention\Image\ImageServiceProvider::class
],

并且,

$aliases => [

    'Image' => Intervention\Image\Facades\Image::class
]

现在您可以在控制器顶部调用 use Image; :

use Image;

class AdminController extends Controller
{
    // ...
}