检索存储在存储文件夹中的图像 [英] Retrieving image stored in storage folder
本文介绍了检索存储在存储文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用laravel 5.3,试图在视图中检索图像.我该怎么做? 文件夹结构:storage/app/avatard
Using laravel 5.3, I am trying to retrieve an image in a view. How I could do this? folder structure: storage/app/avatard
这是代码:
public function storeAvatar(Request $request, $username)
{
$user = User::where('name', $username)->first();
$avatar = $request->file('avatar')->store('avatars');
$avatar = explode('avatars/', $avatar);
$user->user_setting()->updateOrCreate(
['user_id' => $user->id],
['avatar' => $avatar[1]]
);
return back();
}
这是将图像路径保存在数据库中的方式:
This is how the image path is saved in the database:
/users/avatar/default.png
/users/avatar/default.png
推荐答案
由于存储路径对于公众直接不可用,因此可以实现类似的目的.您需要在这样的路由中提供公共网址.
Somewhat like this you can achieve as storage path is directly unavailable for public. you need to provide public url in route like this.
可见
<img src="{{route('avatar',$filename)}}" />
或
<img src="/avatars/{{$filename}}" />
在routes/web.php中
In routes/web.php
Route::get('/avatars/{filename}', function ($filename)
{
$path = storage_path() . '/avatars/' . $filename;
if(!File::exists($path)) abort(404);
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
})->name('avatar');
这篇关于检索存储在存储文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文