在symfony中显示存储在BLOB数据库中的图像

Soi*_*ois 3 blob symfony twig

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

在我的Controller中,调用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,
    ));
}
Run Code Online (Sandbox Code Playgroud)

现在在我的观点(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 %}
Run Code Online (Sandbox Code Playgroud)

但我没看到这张照片?

谁能帮我?

Ala*_*blo 8

你正在使用<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,
));
Run Code Online (Sandbox Code Playgroud)

View

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