Binary data stored as resource

4 months ago

Bill Lions
Member | 19
+
0
-

I am storing some binary data in a bytea field.

$bin = base64_encode($values->image->getContents());

When I try to retrieve the data, I get a resource id.

How can I get the actual image data, and not the resource?

4 months ago

Bill Lions
Member | 19
+
0
-

Solution:
Because PDO returns BYTEA as a resource, and not a string, the above issue is encountered.

The workaround is in latte template

<img src="data:image/jpeg;base64,{php fpassthru($post->image_content)}" alt="{$post->image_name}" />

4 months ago

Milo
Nette Core | 1149
+
0
-

It works but IMHO cleaner way is to write own filter:

function ($resource, string $mime) {
    return "data:$mime;base64," . stream_get_contents($resource);
}

register it e.g. as base64data and use it:

<img src="{$post->image_content|base64data}">