2

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.

3
  • You need to pass $conn to the function otherwise it has no connection as that is outside the scope of the function. Also where are you getting $username from? The same applies for it. Commented Jan 29, 2013 at 4:57
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jan 29, 2013 at 4:58
  • $conn is set. I am getting $username as well. I didnt post the connection code thats all. When I echo $row it prints as 'Array' on the page. When I print $content it prints garbage text.I am getting data from DB but just the image is not rendering Commented Jan 29, 2013 at 5:56

3 Answers 3

2

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).'"/>';
Sign up to request clarification or add additional context in comments.

Comments

1

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' />

4 Comments

I am not sure of this approach. Any idea where I can find a good tutorial ?
Do you want to first upload a image and then show it. Or you already have an image and just want to show it.
Please check this link. link
It would be more helpful if you simply answered the question instead of telling him to do it a different way. I found this page because I want to use blobs, not rewrite all the code I inherited.
0

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

1 Comment

If I comment out header function nothing is displayed. I checked in firefox with firebug as well, no response text. However if I echo $row,$content it prints as 'Array and some garbage text...'. I use PHP Designer for editing php. It doesn;t show any errors as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.