【发布时间】:2017-10-20 11:30:55
【问题描述】:
我已经看到某些保护图像的技术,例如仅向经过身份验证的用户显示它们。一个例子是
how-to-protect-image-from-public-view-in-laravel
它只告诉我们大约 1 张图片。如果我们想检索多个私有图像并将它们显示给经过身份验证的用户怎么办?什么是最合适的方式?我们无法生成指向存储目录的链接吗?
【问题讨论】:
标签: laravel laravel-5.2
我已经看到某些保护图像的技术,例如仅向经过身份验证的用户显示它们。一个例子是
how-to-protect-image-from-public-view-in-laravel
它只告诉我们大约 1 张图片。如果我们想检索多个私有图像并将它们显示给经过身份验证的用户怎么办?什么是最合适的方式?我们无法生成指向存储目录的链接吗?
【问题讨论】:
标签: laravel laravel-5.2
将图像保存在不可公开访问的存储文件夹中。然后根据参数创建一个生成图像的路由,参数可以是图像名称或路径。用auth 中间件包裹这条路线。根据路由的参数,在路由的控制器方法中显示带有适当标题的图像内容。
编辑:看看这个给你一个想法。
示例路线
Route::get('securedimage/{name}', 'SecuredImageController@show');
示例控制器方法
public function show($name)
{
// check if the image with name exists in the folder that you store them
// If the image doesn't exist then display dummy image or return nothing
$imagePath = storage_path('/upload/' . $name);
return Image::make($imagePath)->response();
}
然后你可以像这样访问图像
<img src="http://example.com/securedimage/ball.jpg">
<img src="http://example.com/securedimage/topsecret.jpg">
【讨论】: