无法从存储 laravel 5 中获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30875139/
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
Can't get images from storage laravel 5
提问by marybane
Hey so i'm trying to put my images into a slideshow i have on my index.
the images are saved on storage/app
, and then the rest of the path i retrive from my database.
嘿,所以我正在尝试将我的图像放入索引中的幻灯片中。图像保存在storage/app
,然后是我从数据库中检索的其余路径。
I managed to output the correct path on the <img src>
tag, and the title gets output like its supposed to, but the image won't.
我设法在<img src>
标签上输出了正确的路径,标题像预期的那样输出,但图像不会。
I send and array from the controller and then cycle through it on my view:
我从控制器发送和数组,然后在我的视图中循环:
Controller:
控制器:
public function index()
{
$projects = Project::all()->take(4);
return view('index' , compact('projects'));
}
Index view
索引视图
@foreach($projects as $project)
{{$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}}
<li><img src="{{$storagePath.$project->fetchMedia->first()->public_name}}" title=" {{$project->name}}"></li>
@endforeach
The HTML output is the correct path : "/home/vagrant/project/storage/app/projects/Domaa6.jpg"
HTML 输出是正确的路径:“/home/vagrant/project/storage/app/projects/Domaa6.jpg”
but it doesn't show the image.
但它不显示图像。
I already tried to use the HTML::image helper but had no success either.
我已经尝试使用 HTML::image 助手,但也没有成功。
is there a way to get it working like it is or do i need to create a new controller to manipulate my images?
有没有办法让它像现在这样工作,或者我需要创建一个新的控制器来操纵我的图像吗?
The fetchMedia
is the function on my model that returns my Media.
这fetchMedia
是我的模型上返回我的媒体的函数。
public function fetchMedia()
{
return $this->hasMany('App\Media');
}
回答by Emeka Mbah
What you are trying to do is not possible in the storage directory but possible only in public directory, also exposing the path or URL to your laravel storage directory creates some vulnerability and its bad practice
您尝试做的事情在存储目录中是不可能的,而只能在公共目录中进行,而且将路径或 URL 暴露给您的 Laravel 存储目录会造成一些漏洞及其不良做法
However there is a way to workaround it:
但是有一种方法可以解决它:
First, you need an Image package to generate the image dynamically when a certain route is referenced, I recommend intervention/imagewhich is has many methods that makes image manage a breeze.
首先,您需要一个 Image 包来在引用特定路线时动态生成图像,我推荐干预/图像,它有很多方法可以使图像管理变得轻而易举。
To achieve this take the following steps:
为此,请执行以下步骤:
1. Install Intervention Image:
1. 安装干预镜像:
Add Intervention Image to yourr composer.json and the do composer update
将干预图像添加到您的 composer.json 并执行 composer update
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"intervention/image": "2.*",
"intervention/imagecache": "2.*"
.....
In the $providers array add the service providers for this package.
在 $providers 数组中添加此包的服务提供者。
'Intervention\Image\ImageServiceProvider'
Add the facade of this package to the $aliases array.
将此包的外观添加到 $aliases 数组中。
'Image' => 'Intervention\Image\Facades\Image
'
'Image' => 'Intervention\Image\Facades\Image
'
Configure Intervention Image in Laravel 5
在 Laravel 5 中配置干预图像
$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
Publish configuration in Laravel 4
在 Laravel 4 中发布配置
$ php artisan config:publish intervention/image
For more installation detail https://github.com/Intervention/image
更多安装细节https://github.com/Intervention/image
After you install intervention/image you can do something like this:
安装干预/图像后,您可以执行以下操作:
Image::make($path=storage_path('stock-photos/image1.jpg'));
Image::make($path=storage_path('stock-photos/image1.jpg'));
2. Setup a route that will handle image requests
2. 设置一个处理图片请求的路由
Route::get('stock-photos/{image}', function($image){
//do so other checks here if you wish
if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404);
return Image::make($image)->response('jpg'); //will ensure a jpg is always returned
});
3. Then later in the view you can do:
3.然后在视图中您可以执行以下操作:
@foreach($projects as $project)
<li>
<img src="{{url('stock-photos/'. $project->fetchMedia->first()->public_name)}}" title="{{$project->name}}">
</li>
@endforeach
回答by Flyingearl
Unfortunately I can't comment on Digitlimit's post (as I'm not high enough in reputation yet) but their response worked for me but with one change. Instead of returning the Image::make($image)
I used the response()
method instead as this seemed to generate the header content for me. So for example
不幸的是,我无法对 Digitlimit 的帖子发表评论(因为我的声誉还不够高),但他们的回应对我有用,但有一个变化。而不是返回Image::make($image)
我使用了该response()
方法,因为这似乎为我生成了标题内容。所以例如
Route::get('stock-photos/{image}', function($image){
if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404);
$returnImage = Image::make($image);
return $returnImage->response();
});
Hope that helps others with a similar issue.
希望能帮助其他有类似问题的人。
回答by Rob
Although I've been using Laravel for about 2 years now, I've just started to process image uploads with it very recently (using version 5.3.16). Troubleshooting image processing lead me to this Stack Overflow post... The original accepted answer pretty much solved the issues I was having, with some minor changes as below:
虽然我已经使用 Laravel 大约 2 年了,但我最近才开始使用它处理图像上传(使用版本 5.3.16)。图像处理故障排除使我看到了这篇 Stack Overflow 帖子......最初接受的答案几乎解决了我遇到的问题,有一些小的变化如下:
Route::get('images/books/{book_id}/{image}', function($book_id, $image){
$storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
$imageFilePath = $storagePath . "images/books/{$book_id}/{$image}";
if(!File::exists($imageFilePath)) abort(404);
return Image::make($imageFilePath)->response();
});
In this particular example, I want to retrieve a particular uploaded image for a book. For the given book, I have created a directory which matches the book's I.D, and have uploaded the image into there.
在这个特定示例中,我想检索一本书的特定上传图像。对于给定的书,我创建了一个与书的 ID 匹配的目录,并将图像上传到那里。
To retrieve the image, I first get the public storage directory. This is usually defined in the 'filesystems.php' config file in config/app directory. My example is below (omitted extras not needed for this answer):
为了检索图像,我首先获取公共存储目录。这通常在 config/app 目录中的“filesystems.php”配置文件中定义。我的例子如下(省略了这个答案不需要的额外内容):
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. A "local" driver, as well as a variety of cloud
| based drivers are available for your choosing. Just store away!
|
| Supported: "local", "ftp", "s3", "rackspace"
|
*/
'default' => 'local',
.
.
.
'disks' => [
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
],
];
The rest of the route info follows the same pattern as the first accepted answer. Rendering the image also follows the accepted answer.
其余的路线信息遵循与第一个接受的答案相同的模式。渲染图像也遵循公认的答案。
回答by Almazik G
when displaying images you don't need to reference the file path. What gets into the img
tag is URL to the image you want to display. So, in you case this would be
显示图像时,您不需要引用文件路径。进入img
标签的是您要显示的图像的 URL。所以,在你的情况下,这将是
@foreach($projects as $project)
{{$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}}
<li><img src="/{{$project->fetchMedia->first()->public_name}}" title=" {{$project->name}}"></li>
@endforeach
But. this won't work either as the resources that you make http
requests to should be publicaly accessable. (like css, js-scripts, images). With this being said, you need to store you images in a different location, public folder is the perfect place for this. If you save your images for example in public/images/projects
then in your view you would reference them like this
但。这也不起作用,因为您http
向其发出请求的资源应该是可公开访问的。(如 css、js 脚本、图像)。话虽如此,您需要将图像存储在不同的位置,公共文件夹是完美的地方。例如,如果您将图像保存在public/images/projects
然后在您的视图中,您将像这样引用它们
<img src="/images/projects/{{$project->fetchMedia->first()->public_name}}" />