PHP display image BLOB from MySQL [duplicate]
Asked Answered
B

2

58

I'm attempting to display an image stored in the BLOB column in the database;

I fetch the data from the database with a SELECT perform no transformations on the data and display it with the following (from a script whose only output is the following):

header("Content-Type: image/jpeg");
echo $image;

Please note chrome is displaying the content size as the correct size for the image as well as the correct mime type (image/jpeg). nothing is echoing out before the header and ive checked the blob in the database is correct. There is also no trailing whitespace before or after the <?php ?> tags.

chrome/IE displays an image icon but not the image itself. any ideas?

EDIT: image is got the from the database as such:

$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$row = $sth->fetch();
$image = $row['image'];

var_dump($image) gives:

string 'ÿØÿà�JFIF��x�x��ÿá�ZExif��MM�*�����������J��������Q�������Q������tQ������t�����† ��±ÿÛ�C�       

ÿÛ�CÿÀ�_"�ÿÄ����������� 
ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚ 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³    ´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ��������'... (length=60766)
Busse answered 13/12, 2013 at 0:5 Comment(6)
Post what constitutes $image please (I.e. queries, etc)Veteran
@Busse Did you try my answer below?Mirellamirelle
try to save $image into binary file and open it, maybe data in DB is corrupted?Linguini
Have you tried viewing the source code in the browser? Sometimes a PHP error or notice messes the output up.Libeler
You've not shown us the code which INSERTs the image - which is as important as the code for retrieving it.Astigmia
It's seems like you are saving the image as is in binary, but you should convert it to base64. Also storing images in database is bad-practice. If you can change it, do it now or you will get performance problems. Try avoiding BLOB or TEXT fields. Save the filename and do something like <img src="images/<?php echo $image->filename;?>" alt=".." title="..">Toxicant
N
151

Try it like this.

For inserting into DB

$db = new mysqli("localhost", "root", "", "DbName");
$image = file_get_contents($_FILES['images']['tmp_name']);
$query = "INSERT INTO products (image) VALUES(?)";
$stmt = $db->prepare($query);
$stmt->bind_param('s', $image);
$stmt->execute();

For accessing image from Blob

$db = new mysqli("localhost", "root", "", "DbName");
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
Navel answered 13/12, 2013 at 0:6 Comment(4)
i want to display this echo on html iframe. how this can be done?Analogical
Hello! I really appreciate this but what happens if the image is another format?Sphygmic
@CarlosMontiel, to save image into db is not a good practice. Save Image on directory and save its image name or path into database to access that image. Someone ask the question, I give the answer. that's it. : )Navel
@CarlosMontiel, I am not sure but it will need to be work on other formats too like PNG or GIF etc.Navel
M
18

This is what I use to display images from blob:

echo '<img src="data:image/jpeg;base64,'.base64_encode($image->load()) .'" />';
Mirellamirelle answered 13/12, 2013 at 0:11 Comment(3)
is $image a php object?Micrometeorology
Working like charm! Thank you.. @ProgrammingNewb you can place your image data type in $image. it could be a PHP variable from $row["photo"];Kotta
nice to know it worked for you, I took a different approach, I created a folder in my server and store all images there, and i store the link of the image in my mysql database, then whenever I want to display an image, call the link stored in my databaseMicrometeorology

© 2022 - 2024 — McMap. All rights reserved.