我在 GETer 实体中加载图像(blob 数据)
当我在 GETer 中返回 ($this->foto) 时,我在屏幕上看到:Resource id #284
当我像这样更改 GETer 时: return stream_get_contents($this->foto);
我看到这些:“JFIF”(、、、、、、、(以及更多)
在我的控制器中调用index.html.twig来显示我的所有实体
/**
* 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)中我喜欢显示图片
{% 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 并嵌入。
Controller
$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,
));
View
{% for key, entity in entities %}
{# ... #}
<img alt="Embedded Image" src="data:image/png;base64,{{ images[key] }}" />
{# ... #}
{% endfor %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)