图像(Blob)在浏览器中只显示一次

I'm using Symfony2 and Twig:

In the Entity Class

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

// ...

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

In the view

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

But if I write

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

Then the browser display it only the first time. In the browser (Firefox) The DOM looks like this:

<img src="data:image/png;base64,/9j/4QS...//much more chars//...f7R+ooYz//Z">
<img src="data:image/png;base64,">

So the image content is not present in the second img tag.

Any idea how to show the image more than once?

You will need to either rewind your stream

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

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

Or maybe better for performance, have a property storing the raw content of the blob:

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