3

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 )

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,
    ));
}

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

But I don't see the picture ?

Can anyone help me?

4 Answers 4

9

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

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

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

Hallo Alain, No error but the picture is empty, I changed it to jpeg src="data:image/jpeg;base64,{{ entity.fotoB64 }}" because the blob data is JPEG
Please check my edit-- i guess the property accessor can't access hot properties.
1

A more direct way, without extra work in the controller:

In the Entity Class

/**
 * @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;
}

In the view

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

EDIT

Thanks to @b.enoit.be answer to my question here, I could improve this code so the image can be displayed more than once.

Comments

0

in your entity write your image getter like this:

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

and use it instead of the object "foto" property.

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

1 Comment

Hallo Udan, when using your info I get the error , An exception has been thrown during the rendering of a template "Warning: imagecreatefromstring(): Data is not in a recognized format in the Twig file
0

As it is said before you must use base64 method, but for a better performance and usability, the correct option is creating a custom twig filter (Twig extension) as described here .

<?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 '';
    }
}

In your template:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.