0

GOAL: I'm trying to display an Image into my Laravel 7 view, retrived directly from database.

In my MySQL WORKBENCH: enter image description here

PROCESS:

  1. As you can see in my controller JobsController, I will select and retrieve the image from database, identified by jobs $job_name.

enter image description here

As you can see here, I tried to dd($job_name) and we can see that BLOB data in $job_image variable. I will pass this Collection object to view and display it directly.

enter image description here

Most SO questions relate to displaying the image using <img src="myFileName"/>. Problem is I'm not storing it as a file but instead just retrieve and display it directly. I just want to dump all the variables into my view file like other job details.

Solutions tried in my view file.

  1. <img>{{ base64_encode($job_details->job_image) }}</img>. Failed. Output is "/9j/4AAQSkZJRgABAQ".

  2. <img>{{ base64_decode($job_details->job_image) }}</img>. I don't have much understanding about what encoding and decoding happens when data is stored and retrieved from and to the database. Here I decoded the variable. No output.

  3. <img src="{{ $job_details->job_image}}"/>. This is dumb, src attribute expects an PATH.

So there, none of these would turn the variable content into an image. Please tell me what I am missing here.

Shu Pesmerga
  • 155
  • 1
  • 1
  • 15

2 Answers2

1

You're almost there. You have to use the base64 string in the src attribute. Make sure you also include the data type and other relevant information at the beginning. See: How to display Base64 images in HTML?

  • Thanks! Sounds so ridiculous that I'm just missing 1 step there. LOL! But who would have guessed that SRC attrib accepts that kind of magic. – Shu Pesmerga Apr 03 '20 at 06:55
1

Try this

$image = 'your_image_blob_name';

$imageData = base64_encode(file_get_contents($image));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="'.$src.'">';
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
  • Thanks! Such a detailed answer. I like how the mime_content_type make the file types dynamic dpending on the original file. – Shu Pesmerga Apr 03 '20 at 06:56