Camille Colvray
Camille Colvray

Reputation: 420

Symfony display blob value

I have a project to Symfony 3. With MySQL I have a blob field for a profile user image (in bin extension).

enter image description here

This one is saved from my entity user with the preUpload. (I had set the path new file for test only)

enter image description here

But I can't read the file. How can I do this?

The dump of the entity: enter image description here

Upvotes: 0

Views: 625

Answers (1)

Ollie in PGH
Ollie in PGH

Reputation: 2629

You can display blobs as images with the following HTML5 tag.

<img src="data:image/jpeg;base64,<base64 code here>" />

If you're using this in a Symfony controller it would look like:

/// ... controller code

$user = $this->getDoctrine()->getRepository(User:class)->find($id);
$img = $user->getImg();

// ... more controller code

return $this->render('AppBundle:yourpage.html.twig', [
        'user' => $user,
        'img' => base64_encode($img),
]);

And in twig, you'd display the base64 data in an image tag like this:

<img src="data:image/jpeg;base64,{{ img }}" />

Upvotes: 1

Related Questions