简体   繁体   English

无法从存储 laravel 5 中获取图像

[英]Can't get images from storage laravel 5

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');
}

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/image which 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 和 do 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'));

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

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.不幸的是,我无法对 Digitlimit 的帖子发表评论(因为我的声誉还不够高),但他们的回应对我有用,但有一个变化。 Instead of returning the Image::make($image) I used the response() method instead as this seemed to generate the header content for me.我没有返回Image::make($image)而是使用response()方法,因为这似乎为我生成了标题内容。 So for example所以例如

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.希望能帮助其他有类似问题的人。

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.进入img标签的是您要显示的图像的 URL。 So, in you case this would be所以,在你的情况下,这将是

@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.这也不起作用,因为您向其发出http请求的资源应该是可公开访问的。 (like css, js-scripts, images). (如 css、js 脚本、图像)。 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例如,如果您将图像保存在public/images/projects那么在您的视图中,您将像这样引用它们

<img src="/images/projects/{{$project->fetchMedia->first()->public_name}}" />

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).虽然我已经使用 Laravel 大约 2 年了,但我最近才开始使用它处理图像上传(使用版本 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:图像处理故障排除使我看到了这篇 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 ID, 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.这通常在 config/app 目录中的“filesystems.php”配置文件中定义。 My example is below (omitted extras not needed for this answer):我的例子如下(省略了这个答案不需要的额外内容):

<?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.渲染图像也遵循公认的答案。

storage_path() The storage_path function returns the fully qualified path to your application's storage directory. storage_path() storage_path 函数返回应用程序存储目录的完全限定路径。 You may also use the storage_path function to generate a fully qualified path to a given file within the storage directory:您还可以使用 storage_path 函数生成存储目录中给定文件的完全限定路径:

 $path = storage_path();

 $path = storage_path('app/file.txt');

https://laravel.com/docs/8.x/helpers#method-storage-path https://laravel.com/docs/8.x/helpers#method-storage-path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM