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

时间:2015-02-03 08:07:12

标签: symfony twig blob

我在我的GETer实体中加载了我的图像(blob数据) 当我在我的GETer中返回($ this-> foto)时,我看到:屏幕上的资源ID#284 当我像这样更改我的GETer时:return stream_get_contents($ this-> foto); 我看到了这些: 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,
    ));
}

现在在我的观点(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 %}

但是我没看到这张照片?

任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:7)

您使用<img src="(raw image)">代替<img src="(image's url)">

快速解决方案是在base64中对图像进行编码并嵌入它。

控制器

$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 %}

答案 1 :(得分:0)

在您的实体中写下您的图像获取器:

public function getFoto()
{
    return imagecreatefromstring($this->foto);
}

并使用它代替对象&#34; foto&#34;属性。

php doc for the function:http://php.net/manual/de/function.imagecreatefromstring.php

答案 2 :(得分:0)

一种更直接的方法,无需在控制器中进行额外的工作:

在实体类中

/**
 * @ORM\Column(name="photo", type="blob", nullable=true)
 */
private $photo;

private $rawPhoto;

public function displayPhoto()
{
    if(null === $this->rawPhoto) {
        $this->rawPhoto = "data:image/png;base64," . base64_encode(stream_get_contents($this->getPhoto()));
    }

    return $this->rawPhoto;
}

在视图中

<img src="{{ entity.displayPhoto }}">

编辑

感谢@ b.enoit.be对我的问题here的回答,我可以改进此代码,以便可以多次显示图像。

答案 3 :(得分:0)

如前所述,必须使用base64方法,但是为了获得更好的性能和可用性,正确的选择是按照here的描述创建自定义的树枝过滤器(Twig扩展名)。

<?php


namespace Your\Namespace;


use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class TwigExtensions extends AbstractExtension
{
    public function getFilters()
    {
        return [
            new TwigFilter('base64', [$this, 'twig_base64_filter']),
        ];
    }

    function twig_base64_filter($source)
    {   if($source!=null) {           
           return base64_encode(stream_get_contents($source));
        }
        return '';
    }
}

在您的模板中:

<img src="data:image/png;base64,{{ entity.photo | base64 }}">
相关问题