保护图像不被 public laravel 5
Protect Images from being public laravel 5
我见过某些保护图像的技术,比如只向经过身份验证的用户显示它们。一个例子是
它只告诉我们大约 1 张图像。如果我们想检索多个私有图像并将它们显示给经过身份验证的用户怎么办?。什么是最合适的方式?我们不能生成一个 link 到存储目录对吗?
将图像保存在不可公开访问的存储文件夹中。然后创建路由根据参数生成图片,参数可以是图片名称或路径。使用 auth
中间件包装此路由。根据其参数在路由的控制器方法中使用适当的 headers 显示图像内容。
编辑:检查这个给你一个想法。
示例路线
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">
我见过某些保护图像的技术,比如只向经过身份验证的用户显示它们。一个例子是
它只告诉我们大约 1 张图像。如果我们想检索多个私有图像并将它们显示给经过身份验证的用户怎么办?。什么是最合适的方式?我们不能生成一个 link 到存储目录对吗?
将图像保存在不可公开访问的存储文件夹中。然后创建路由根据参数生成图片,参数可以是图片名称或路径。使用 auth
中间件包装此路由。根据其参数在路由的控制器方法中使用适当的 headers 显示图像内容。
编辑:检查这个给你一个想法。
示例路线
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">