显示来自存储 laravel 5.3 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41984496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display image from storage laravel 5.3
提问by Ahmadz Issa
I'm new to laravel framework, I want to display an image stored in storage folder. I tried this
我是 Laravel 框架的新手,我想显示存储在存储文件夹中的图像。我试过这个
Route
路线
Route::get('/userimage/{filename}', [
'uses' =>'PostController@getUserImage',
'as' => 'account.image',
]);
blade file to get the url and display the image
Blade 文件获取 url 并显示图像
</section>
@if (Storage::disk('public')->has($user->first_name . '-' . $user->id . '.jpg'))
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<img src="{{ route('account.image', ['filename' => $user->first_name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
</div>
</section>
controller
控制器
public function getUserImage($filename)
{
$file = Storage::disk('public')->get($filename);
return new Response($file,200);
}
How can I display the image using the code above ?
如何使用上面的代码显示图像?
回答by Anwar
Your controller should handle all the "behind the scenes" process, and your view only display html and php variables. That is the main point of the MVC pattern.
您的控制器应该处理所有“幕后”过程,您的视图只显示 html 和 php 变量。这就是 MVC 模式的要点。
So your controller should return a view, and inject in its view the file path.
所以你的控制器应该返回一个视图,并在它的视图中注入文件路径。
public function getUserImage($filename)
{
$file = Storage::disk('public')->get($filename);
return view('yourviewnamehere', ['myFile' => $file]);
}
Now, your view will be able to access the variable $myFile
:
现在,您的视图将能够访问变量$myFile
:
<!-- ... -->
@if (Storage::disk('public')->has($user->first_name . '-' . $user->id . '.jpg'))
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<img src="{{ URL::to('img/' . $myFile) ]) }}" alt="" class="img-responsive">
</div>
</section>
@endif
<!-- ... -->
I assumed the file path for the images in public/img/
, so feel free to change this part of the code. Also, whether your variable contains the suffix .jpg
or not, you should add it or not.
我假设了 中图像的文件路径public/img/
,因此可以随意更改这部分代码。此外,无论您的变量是否包含后缀.jpg
,您都应该添加它。
回答by gandalf
I had the same problem. The thing is, you can not access stuff in storage so as it is suggested hereyou can simply make a symbolic link
from public/storage
to storage/app/public
by using this command php artisan storage:link
and then you can access whatever in storage directory through public directory like this asset('storage/path_to_file')
,
of course sometimes it is not possible for you to do so (for instance when you are deploying your project to a shared host). If this is the case,
you can try uploading the files to public directory(which is accessible to everyone) and address the files using asset()
method
1. you can simply define a new disk driver in disks array which exists in config/filesystems.php
. in laravel 5.4, three drivers have been defined by default('local'
, 'public'
, 's3'
). you just need to add your own driver which here we call it 'MyDiskDriver'
(but you can call it whatever you want).
config/filesystems.php
我有同样的问题。关键是,你可以在存储无法访问的东西,所以它建议在这里你可以简单地做一个symbolic link
从public/storage
到storage/app/public
使用此命令php artisan storage:link
,然后就可以在存储目录通过公共目录这样的访问什么asset('storage/path_to_file')
,当然有时是不可能的为您这样做(例如,当您将项目部署到共享主机时)。如果是这种情况,您可以尝试将文件上传到公共目录(每个人都可以访问)并使用asset()
方法 1寻址文件。您可以简单地在磁盘阵列中定义一个新的磁盘驱动程序,该驱动程序存在于config/filesystems.php
. 在 laravel 5.4 中,默认定义了三个驱动程序( 'local'
, 'public'
,'s3'
)。你只需要添加你自己的驱动程序,我们在这里称之为它'MyDiskDriver'
(但你可以随意调用它)。
config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
'MyDiskDriver' => [
'driver' => 'local',
'root' => public_path('uploads'),
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
],
];
notice that for 'MyDiskDriver', 'root' has been set to public/uploads (which means when you use store method of Illuminate\Http\UploadedFile class and pass the name of the 'MyDiskDriver' as the second argument, laravel would upload the file in public/uploads directory.)
请注意,对于 'MyDiskDriver','root' 已设置为 public/uploads(这意味着当您使用 Illuminate\Http\UploadedFile 类的 store 方法并将 'MyDiskDriver' 的名称作为第二个参数传递时,laravel 将上传public/uploads 目录中的文件。)
when you are using store method of Illuminate\Http\UploadedFile class, you can pass the name of your newly created disk driver(here 'MyDiskDriver') as the second argument so that laravel can use it to store the file ,you are trying to store, in the directory that you specified in 'root'(here public/uploads).
you can access the uploaded file using the
asset()
method.
当您使用 Illuminate\Http\UploadedFile 类的 store 方法时,您可以将新创建的磁盘驱动程序的名称(此处为 'MyDiskDriver')作为第二个参数传递,以便 Laravel 可以使用它来存储文件,您正在尝试存储在您在“root”中指定的目录中(此处为 public/uploads)。
您可以使用该
asset()
方法访问上传的文件。
Edit
编辑
Instead of doing step 2, you can change the value of FILESYSTEM_DRIVER
environment variable in .env
to the name of the disk driver that you have just created, In our case MyDiskDriver
.
This way you can easily change the disk driver without the need of changing your code, just by changing the FILESYSTEM_DRIVER
environment variable's value
您可以将FILESYSTEM_DRIVER
环境变量的值更改为.env
您刚刚创建的磁盘驱动程序的名称,而不是执行步骤 2 ,在我们的示例中MyDiskDriver
。这样您就可以轻松更改磁盘驱动程序,而无需更改代码,只需更改FILESYSTEM_DRIVER
环境变量的值
回答by Jan Wytze
Laravel 5 - How to access image uploaded in storage within View?
Laravel 5 - 如何在 View 中访问存储中上传的图像?
Check the first answer, you can replace all the File
stuff with Storage
.
检查第一个答案,您可以将所有File
内容替换为Storage
.
Here is a function I made:
这是我制作的一个函数:
public function getUserImage($filename)
{
$storagepath = $filename;
if (!Storage::disk('public')->exists($storagepath)) {
$storagepath = 'defaultuserimage.jpeg';
}
$path = Storage::disk('public')->getDriver()->getAdapter()->applyPathPrefix($storagepath);
return Image::make($path)->response();
}
It requires the intervention image package.
它需要干预图像包。