Reputation: 145
GOAL: I'm trying to display an Image into my Laravel 7 view, retrived directly from database.
PROCESS:
jobs $job_name
.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.
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.
<img>{{ base64_encode($job_details->job_image) }}</img>
. Failed. Output is "/9j/4AAQSkZJRgABAQ".
<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.
<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
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
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