无法在 Laravel 5.3 中的视图中检索图像
Unable to retrieve images in View in Laravel 5.3
我已将图片上传到存储文件夹中。现在我想检索视图中的图像。我现在正在本地环境中工作。 <img src={{ Storage::disk('local')->url($image->path) }}>
不起作用。我错过了什么?
首先,确保您在 public 和 storage/app/public 目录之间正确设置了符号链接。您可以使用以下命令执行此操作:
php artisan storage:link
更多信息请查看:https://laravel.com/docs/5.3/filesystem#configuration
在视图中,您可以显示这样的图像:
<img src="{{ asset($image->path) }}" />
此外,请确保您在数据库中正确存储图像路径。
为什么不创建一个函数来接收模型的名称或 ID 或您拥有的任何东西以及根据某种逻辑 return 图像?
例如:
- 获取文件名(如果保存在文件夹下一定要保存
folder/filename.extension, if)
- 搜索它并在正确的存储中获取文件
- Return 正确的文件 headers
- 创建一个接收文件名作为参数的 GET 路由并调用
下面显示了一个函数
// Controller
public function getImage(Request $request){
$filename = $request->filename;
$file = Storage::disk('local')->get($filename);
return response($file)->withHeaders(['Content-Type' => "image/png"]);
}
// Route
Route::get('/getThisImage/{filename}',[
'uses'=>'HomeController@getImage',
'as'=>'getImage'
]);
// Example of calling it in blade
<img src="{{route('getImage')}}/{{$image->path}}">
我已将图片上传到存储文件夹中。现在我想检索视图中的图像。我现在正在本地环境中工作。 <img src={{ Storage::disk('local')->url($image->path) }}>
不起作用。我错过了什么?
首先,确保您在 public 和 storage/app/public 目录之间正确设置了符号链接。您可以使用以下命令执行此操作:
php artisan storage:link
更多信息请查看:https://laravel.com/docs/5.3/filesystem#configuration
在视图中,您可以显示这样的图像:
<img src="{{ asset($image->path) }}" />
此外,请确保您在数据库中正确存储图像路径。
为什么不创建一个函数来接收模型的名称或 ID 或您拥有的任何东西以及根据某种逻辑 return 图像? 例如:
- 获取文件名(如果保存在文件夹下一定要保存 folder/filename.extension, if)
- 搜索它并在正确的存储中获取文件
- Return 正确的文件 headers
- 创建一个接收文件名作为参数的 GET 路由并调用 下面显示了一个函数
// Controller public function getImage(Request $request){ $filename = $request->filename; $file = Storage::disk('local')->get($filename); return response($file)->withHeaders(['Content-Type' => "image/png"]); } // Route Route::get('/getThisImage/{filename}',[ 'uses'=>'HomeController@getImage', 'as'=>'getImage' ]); // Example of calling it in blade <img src="{{route('getImage')}}/{{$image->path}}">