在 Laravel 中访问公共文件夹中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26286175/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:46:40  来源:igfitidea点击:

Access images inside public folder in laravel

laravel

提问by PaladiN

How can I access the images stored inside public/imagesfolder without the use of Laravel's own functions like assets()?

如何在public/images不使用 Laravel 自己的功能的情况下访问存储在文件夹中的图像assets()

I just want to get the direct URL of the images stored in images folder.

我只想获取存储在图像文件夹中的图像的直接 URL。

Eg:

例如:

localhost/webapp/images/stackoverflow.png

When requesting the link i should get the direct image.

请求链接时,我应该获得直接图像。

回答by Colin Schoen

If you are inside a blade template

如果您在刀片模板内

{{ URL::to('/') }}/images/stackoverflow.png

{{ URL::to('/') }}/images/stackoverflow.png

回答by Dadu Laal

Just put your Images in Public Directory (public/...folder or direct images).
Public directory is by default rendered by laravel application.
Let's suppose I stored images in public/images/myimage.jpg.
Then in your HTML view, page like: (image.blade.php)

只需将您的图像放入公共目录(公共/...文件夹或直接图像)。
公共目录默认由 laravel 应用程序呈现。
假设我将图像存储在public/images/myimage.jpg.
然后在您的 HTML 视图中,页面如下:(image.blade.php)

<img src="{{url('/images/myimage.jpg')}}" alt="Image"/>

回答by Thomas Jensen

I have created an Asset helper of my own.

我已经创建了我自己的资产助手。

First I defined the asset types and path in app/config/assets.php:

首先我定义了资产类型和路径app/config/assets.php

return array(

    /*
    |--------------------------------------------------------------------------
    | Assets paths
    |--------------------------------------------------------------------------
    |
    | Location of all application assets, relative to the public folder,
    | may be used together with absolute paths or with URLs.
    |
    */

    'images' => '/storage/images',
    'css' => '/assets/css',
    'img' => '/assets/img',
    'js' => '/assets/js'
);

Then the actual Assetclass:

然后是实际的Asset类:

class Asset
{
    private static function getUrl($type, $file)
    {
        return URL::to(Config::get('assets.' . $type) . '/' . $file);
    }

    public static function css($file)
    {
        return self::getUrl('css', $file);
    }

    public static function img($file)
    {
        return self::getUrl('img', $file);
    }

    public static function js($file)
    {
        return self::getUrl('js', $file);
    }

}

So in my view I can do this to show an image:

所以在我看来,我可以这样做来显示图像:

{{ HTML::image(Asset::img('logo.png'), "My logo")) }}

Or like this to implement a Java script:

或者像这样实现一个Java脚本:

{{ HTML::script(Asset::js('my_script.js')) }}

回答by Nimeshika Prabodhani

when you want to access images which are in public/images folder and if you want to access it without using laravel functions, use as follows:

当你想访问 public/images 文件夹中的图像并且你想在不使用 Laravel 函数的情况下访问它时,使用如下:

<img src={{url('/images/photo.type')}} width="" height="" alt=""/>

This works fine.

这工作正常。

回答by osama ellahi

Just use public_path() it will find public folder and address it itself.

只需使用 public_path() 它将找到公共文件夹并自行寻址。

<img src=public_path().'/images/imagename.jpg' >