0

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

Robert Wade
  • 4,918
  • 1
  • 16
  • 35
Camille Colvray
  • 420
  • 3
  • 16
  • 1
    You should add *how* you're trying to use the image blob to the question. Also this https://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag might be useful. – dlondero Dec 14 '17 at 08:57
  • i would to read the file in html src ! – Camille Colvray Dec 14 '17 at 09:00

1 Answers1

1

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 }}" />
Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19