Shu Pesmerga
Shu Pesmerga

Reputation: 145

Display BLOB image data to view

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.

Upvotes: 0

Views: 1574

Answers (2)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

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.'">';

Upvotes: 1

NatrillyAsprated
NatrillyAsprated

Reputation: 11

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?

Upvotes: 1

Related Questions