我想将Glide用于我的所有图片来源。
关注this instruction后,我可以在http://myweb.dev/img/test.jpg?w=200&filt=sepia
获得图片。
路线:
Route::get('/img/{path}', 'ImageController@show')->where('path', '.*');
控制器:
namespace App\Http\Controllers;
use Illuminate\Contracts\Filesystem\Filesystem;
use League\Glide\Responses\LaravelResponseFactory;
use League\Glide\ServerFactory;
class ImageController extends Controller
{
public function show(Filesystem $filesystem, $path)
{
$server = ServerFactory::create([
'response' => new LaravelResponseFactory(app('request')),
'source' => $filesystem->getDriver(),
'cache' => $filesystem->getDriver(),
'cache_path_prefix' => '.cache',
'base_url' => 'img',
]);
return $server->getImageResponse($path, request()->all());
}
}
由于我的所有图片都是/ public / uploads目录,因此我将其更改为以下内容。
路线:
Route::get('uploads/{path}', 'ImageController@show')->where('path', '.*');
控制器
public function show(Filesystem $filesystem, $path)
{
$server = ServerFactory::create([
'response' => new LaravelResponseFactory(app('request')),
'source' => public_path(),
'cache' => public_path(),
'cache_path_prefix' => '.cache',
'base_url' => 'uploads',
]);
return $server->getImageResponse($path, request()->all());
}
当我转到http://myweb.dev/uploads/test.jpg?w=200&filt=sepia时,我得到的图像没有任何变化,也没有创建任何.cache目录。
我也试过了base_path()
和/uploads
,但也没有用。我正在使用elfinder的uploads目录。
我在这里做错了什么?
答案 0 :(得分:0)
这很有用。我将图像放在public/uploads
目录中。
路线
Route::get('/img/{path}', 'ImageController@show')->where('path', '.*');
控制器
namespace App\Http\Controllers;
use Illuminate\Contracts\Filesystem\Filesystem;
use League\Glide\Responses\LaravelResponseFactory;
use League\Glide\ServerFactory;
class ImageController extends Controller
{
public function show(Filesystem $filesystem, $path)
{
$server = ServerFactory::create([
'response' => new LaravelResponseFactory(app('request')),
'source' => public_path('uploads'),
'cache' => public_path('uploads'),
'cache_path_prefix' => '.cache',
'base_url' => 'img',
]);
return $server->getImageResponse($path, request()->all());
}
}