Access Blob Resource in Database


I’m storing thumbnails in a database and in the model this field is defined by

     * @var blob
     * @ORM\Column(name="thumbnail", type="blob", nullable=true)
    protected $thumbnail;

     * @return string
    public function getThumbnail()
        return $this->thumbnail;

but how do i get now the binary data for further processing (either in the controller to modify the thumbnail or directly in the fluid template to display as base64 encoded inline image)?

all i get is a “resource”, but i have not found anything about how to access the data of this resource :((((

any help would be appreciated …



hours of searching did not help to find the information needed, but a powerful nap and search again for 5 minutes resulted in:

$s = stream_get_contents($note->getThumbnail());
$img = base64_encde($s);

but how to do this in the fluid template?


Hi glad you found a way :wink:
Often clicking with your ide (phpStorm ^^) through the source code is the best guide one gets, as thats a very specific task ^^

I dont really know all about fluids default viewhelpers - but my guess is, that fluid doesnt have a viewhelper for this so A: you need to create a new view helper, or B: separate this concern into your php integration, and pass a fluid variable…

You want to render a datauri

So assign it with to your view like this

$this->view->assign('base64String', '<your string>');

and render it with fluid

<img src="data:image/png;base64, {base64String}" alt="Red dot" />

No need for viewhelper, since it’s a single value you render :slight_smile:

dear Søren,

yes, this is the way i’m currently doing this, but the problem is, that there’s not just a single image to render (because i get a resultset of notes, and each note has an image), so i have to modify/copy the resultset, and that’s not handy …

$n2 = [];
for ($repo->findNotes() as $note) {
  $n2[] = ['id' => $note->getId(), 'thumbnail' => base64_encode(stream_get_contents($note->getThumbnail()))];
$this->view->assign('notes', $n2);

but i thought about something like (without the code above)

<f:for each="{notes}" as "note">
  <td><img src="data:image/jpeg,base64, <f:doAnythingWithTheResource>{note.thumbnail}</f:doAnythingWithTheResource>"></td>

but it seems there is no builtin function/helper, so i would have to create my own ViewHelper, correct?


You thumbnail variable seems to be the result of your base64_encode method - so it’s a string.
If that is true, you don’t need to do anything, and you rendering would look like

<f:for each="{notes}" as "note">
  <td><img src="data:image/jpeg,base64, {note.thumbnail}"></td>

So, without any custom viewhelper - doesn’t that do the trick for you?

Note: Many base64 datauris in a single page, can really be a pain for the browser to render (you are putting the load on the CPU/GPU of the clients computer).

@hkrebs Did you get it to do what you wanted?

dear Soren,

i dont want to make a clone of the requestquery $notes to an array $n2 and converting all the the binary thumbnail properties to base64-encoded properties. and if i assign a object with a binary property to the view, i would need some kind of funktion to convert the binary to base64 - a built in function or a viewhelper …

but anyway, i solved it differently …

yes of course, as always in this forum :)))

i changed the source to NOT embed the base64-images as you mentioned - but not for the reason of CPU-load (the page designed is only for some administrators and not for the public, so CPU-power is not a question) , but other reasons.

but then a new question arose: how to output binary data in neos flow? i know how to render json-data (Model View Controller — Flow Framework dev-master documentation) but what about binary data? i haven’t found anything, so i make the output with a “primitive” view and f:format.raw - is there a better way?