在 symfony 中显示存储在 BLOB 数据库中的图像 [英] Display image stored in BLOB database in symfony

查看:22
本文介绍了在 symfony 中显示存储在 BLOB 数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 GETer 实体中加载我的图像(blob 数据)当我在 GETer 中返回 ($this->foto) 时,我在屏幕上看到:Resource id #284当我像这样更改我的 GETer 时: return stream_get_contents($this->foto);我看到了这些: ?JFIF ?(,,,,,,,, (和更多)

I load my image (blob data ) in my GETer Entity When I just return ($this->foto) in my GETer I see :Resource id #284 on the screen When I change my GETer like this : return stream_get_contents($this->foto); I see these : ���JFIF��� ( ,,,,,,,, ( and more )

在我的控制器中调用 index.html.twig 以显示我的所有实体

In my Controller a call the index.html.twig to show all my entities

    /**
 * Lists all Producten entities.
 *
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('CustomCMSBundle:Producten')->findAll();

    return $this->render('CustomCMSBundle:Producten:index.html.twig', array(
        'entities' => $entities,
    ));
}

现在在我看来 (index.html.twig) 我喜欢展示图片

Now in my views ( index.html.twig ) I like to show the picture

       {% for entity in entities %}
        <tr>
            <td>
                <img  src="{{ entity.foto}}" alt="" width="80" height="80" />
            </td>
            <td>
                {{ entity.foto }}
            </td>
            <td>
            <ul>
                <li>
                    <a href="{{ path('cms_producten_show', { 'id': entity.id }) }}">show</a>
                </li>
                <li>
                    <a href="{{ path('cms_producten_edit', { 'id': entity.id }) }}">edit</a>
                </li>
            </ul>
            </td>
        </tr>
    {% endfor %}

但我没有看到图片?

谁能帮帮我?

推荐答案

你使用的是 <img src="(raw image)"> 而不是

You are using <img src="(raw image)"> instead of <img src="(image's url)">

一种快速的解决方案是将您的图像编码为 base64 并将其嵌入.

A quick solution is to encode your image in base64 and embed it.

控制器

$images = array();
foreach ($entities as $key => $entity) {
  $images[$key] = base64_encode(stream_get_contents($entity->getFoto()));
}

// ...

return $this->render('CustomCMSBundle:Producten:index.html.twig', array(
    'entities' => $entities,
    'images' => $images,
));

查看

{% for key, entity in entities %}
  {# ... #}
  <img alt="Embedded Image" src="data:image/png;base64,{{ images[key] }}" />
  {# ... #}
{% endfor %}

这篇关于在 symfony 中显示存储在 BLOB 数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆