Reputation: 23
Hi I saved a jpg image in BLOB format in mysql. I am having problem rendering the image using PHP onto my view page. When I use
header('Content-type: image/jpeg')
it gives a message on my page as Image cannot be displayed because it contains errors. When I print without header function is displays me some garbage text. Here is my code :
function loadImage(){
//connect to db
mysql_select_db('aaa',$conn);
$query = mysql_query("SELECT * FROM asdf WHERE UserName='".$userName.");
$row = mysql_fetch_array($query);
$content = $row['ProfileImage'];
header('Content-type: image/jpeg');
echo $content;
}
Here is the html code :
<img src='loadImage.php?func=loadImage' />
If the image is displayed then I plan to use
loadImage.php?func=loadImage?id=number'
But image itself is not displaying. Any help greatly appreciated.
Upvotes: 2
Views: 6867
Reputation: 21
hope this helps someone...
assuming the image is already uploaded in blob format then you can use this
first fetch the records whose images you want to display using query and store blob image in $blobimg
then use this to convert blob file
echo '<img src ="data:image/jpeg;base64,'.base64_encode($blobimg).'"/>';
Upvotes: 2
Reputation: 11
check if you are getting any other errors on the page by commenting out header('Content-type: image/jpeg');
if your script is set to output errors then the image will not be rendered properly
Upvotes: 0
Reputation: 848
Why saving image in database.
The best practise is save your image in some folder and give your image a unique name. Then save image name into the database.
While displaying the image use
<img src='path/to/image/image_name' />
Upvotes: 1