我想在我的 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));
}
}
但是当我填写表格检查它是否有效时,就会弹出此错误消息:
错误 找不到类“App\Http\Controllers\Admin\Image”
这意味着这一行:
Image::make($file->getRealPath())->resize(300,null,function($constraint){
那么为什么当我已经将它包含在我的项目中时它会返回此错误?!
如果您知道,请告诉我...我将非常感激。
谢谢
在
config/app.php
上您需要添加:
$provides => [
Intervention\Image\ImageServiceProvider::class
],
还有,
$aliases => [
'Image' => Intervention\Image\Facades\Image::class
]
现在您可以拨打控制器顶部的
use Image;
:
use Image;
class AdminController extends Controller
{
// ...
}
有两个版本。如果 v3 与 v2 有点不同。有人需要了解配置过程并通过该链接执行步骤。它有一个例子。 [https://neaptech.com/laravel-images-intervention/][1]