php Laravel 8:如何正确使用图像库

z31licg0  于 5个月前  发布在  PHP
关注(0)|答案(2)|浏览(44)

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

Intervention\Image\ImageServiceProvider::class,

字符串
这一行也被添加到aliases部分:

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


现在,在我的Controller中,我编写了以下代码:

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));
    }
}


但是当我填写表单检查它是否工作时,这个错误消息就会弹出:

错误类'App\Http\Controllers\Admin\Image'未找到

这意味着这条线:

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


那么,为什么它返回这个错误,而我已经包括在我的项目?!
如果你知道,请告诉我...我会很感激的。
谢谢

sg2wtvxw

sg2wtvxw1#

config/app.php上,您需要添加:

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

字符串
而且,

$aliases => [

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


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

use Image;

class AdminController extends Controller
{
    // ...
}

hgtggwj0

hgtggwj02#

这有两个版本。如果v3位不同于v2。有人需要知道配置过程,并按照步骤通过该链接。它有一个例子。https://neaptech.com/laravel-images-intervention/

相关问题