简体   繁体   English

如何在 Laravel Blade 中用 html 显示单个图像?

[英]How to display a single image with html in Laravel Blade?

I want to display an image from my database, stored as a blob.我想显示我的数据库中的图像,存储为 blob。 The Blade file to view the picture is as follows.查看图片的Blade文件如下。

<div class="col-lg-7"> 
    <img src="{{ url('images/' . $id )}} "/>
</div>

I tried to replace with this code:我试图用这个代码替换:

  <img style="display:block;width:100%;height:100%;" src="{{ url('images/' . $id) }} " />

It shows the view in the page like this:它显示页面中的视图,如下所示:

看图片

I use the following function in my web route to get the image.我在我的网络路由中使用以下函数来获取图像。

// Get profile image
Route::get('images/{id}', function ($id) {
    // Find the user
    $item = App\Itemregistration::find($id);

    // Return the image in the response with the correct MIME type
    return response()->make($item->Picture, 200, array(
        'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($item->Picture)
    ));
});

Previously I was able to display the picture;以前我可以显示图片; the image is stored in the images folder.图像存储在图像文件夹中。 However, after a few developments in another area, suddenly I just noticed the picture could not be displayed.但是,在另一个区域进行了一些开发后,我突然发现图片无法显示。 I had checked using URL (for example http://127.0.0.1:8000/images/210 ) the image is downloaded as an unknown file.我已经使用 URL(例如http://127.0.0.1:8000/images/210 )检查了图像作为未知文件下载。 I had compared the code with the previous version;我已经将代码与以前的版本进行了比较; it is the same.这是相同的。 I am confused, what caused the problem.我很困惑,是什么导致了问题。

Do anyone has an idea which way to identify the problem?有没有人知道哪种方法可以识别问题? Am I missing something or change something accidentally?Thanks.我是否遗漏了什么或不小心更改了什么?谢谢。

Wouldn't it be easier to just render the image in the view directly, rather than define a function and then use it as you've done?直接在视图中渲染图像,而不是定义一个函数然后像你所做的那样使用它不是更容易吗?

    <img src="{{asset('your_image_folder_path'.$image->name)}}">

I see you are trying to access the image by it so maybe you can also do something like such:我看到您正在尝试通过它访问图像,所以也许您也可以执行以下操作:

    public function methodName($id)
    {
       $image= YourImageModel::find($id);
       return view('your_view_with_compacted_image_variable');

    }

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

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