以symfony显示存储在BLOB数据库中的图像 [英] Display image stored in BLOB database in symfony
问题描述
我将图像(blob数据)加载到我的GETer实体中 当我刚刚在我的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)">
而不是<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屋!