无法从存储laravel 5获取图像 [英] Can't get images from storage laravel 5
问题描述
嘿所以我试图将我的图像放到我的索引上的幻灯片中。
图像保存在 storage / app
上,然后我从数据库中恢复其余路径。
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.
我设法在< img src>
标签上输出正确的路径,并且标题得到的输出与它应该的一样,但图像不会。
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.
我从控制器发送数组,然后在我的视图中循环显示:
I send and array from the controller and then cycle through it on my view:
控制器:
public function index()
{
$projects = Project::all()->take(4);
return view('index' , compact('projects'));
}
索引视图
@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
HTML输出是正确的路径:/ home / vagrant / project /storage/app/projects/Domaa6.jpg
The HTML output is the correct path : "/home/vagrant/project/storage/app/projects/Domaa6.jpg"
但它没有显示图像。
我已经尝试使用HTML :: image帮助程序,但也没有成功。
I already tried to use the HTML::image helper but had no success either.
有没有办法让它像它一样工作或者我是否需要创建一个新的控制器来操纵我的图像?
is there a way to get it working like it is or do i need to create a new controller to manipulate my images?
fetchMedia
是我的模型上的函数,它返回我的媒体。
The fetchMedia
is the function on my model that returns my Media.
public function fetchMedia()
{
return $this->hasMany('App\Media');
}
推荐答案
你想做什么在存储目录中是不可能的,但只能在公共目录中使用,同时将路径或URL暴露给laravel存储目录会产生一些漏洞及其不良做法
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
但是有一种方法可以解决它:
首先,当引用某个路径时,你需要一个Image包来动态生成图像,我建议 介入/图片 ,它有许多让图像管理变得轻而易举的方法。
First, you need an Image package to generate the image dynamically when a certain route is referenced, I recommend intervention/image which is has many methods that makes image manage a breeze.
要实现此目标,请执行以下步骤:
1。安装干预图像:
将干预图像添加到yourr composer.json并执行作曲家更新
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"intervention/image": "2.*",
"intervention/imagecache": "2.*"
.....
在$ providers数组中添加此软件包的服务提供商。
'Intervention\Image\ImageServiceProvider'
将此包的外观添加到$ aliases数组。
'图片'=> '干预\图像\法会'\\图像
'
在Laravel 5中配置干预图像
$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
在Laravel 4中发布配置
$ php artisan config:publish 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')) ;
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。然后在视图中你可以这样做:
@foreach($projects as $project)
<li>
<img src="{{url('stock-photos/'. $project->fetchMedia->first()->public_name)}}" title="{{$project->name}}">
</li>
@endforeach
这篇关于无法从存储laravel 5获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!