tags:

views:

100

answers:

3

Initially i added full source code for viewer. but it has some format problem and all the code was messed with HTML format. So now i am asking step by step. 1) I uploaded an image file to my MySQL database. (it has no error, i.e. loaded successfully) 2) Fetching it back to the browser from MySQL Database. It has also no problem(in some condition) a) It is printing OK if i am printing it before the HTML Tag. b) I am not able to see any basic HTML design after printing image on browser. c) If i am printing it inside html tag. It is showing some special characters and numbers, i.e. value which we see in phpmyadmin if we execute the query (select image from pix;)

So, Can any one help me to print image by fetching the image from MySql database? Or can any one convert this database saved image from MySQL into real image to store into hard-disk back to see/edit it?

+2  A: 

You are making a fundamental mistake here. An image resource is always stored and requested in a separate file, not in the HTML source code of the page you want to embed it in.

Such a separate file could be named, say, getimage.php.

It would fetch the image data from the database (e.g. from the record with the ID 890) and output it like e.g. so:

... code to fetch the image ....

header("Content-type: image/jpeg");  // or image/gif, image/png....

echo $imageData;

In the HTML page, you would embed it using

<img src="getimage.php?id=890">

There is the theoretical possibility to have image data directly in the HTML source but that's not what you want.

Pekka
Your suggestion is really too good and acceptable. I tried and still has some problem. Can you give me your email-id. So that i will send you that file to you and you will make changes into it.Hope you don't have any problem.Thanx for valueable suggestion.
Anup Prakash
@Anup sorry, I don't have more time than I can spend here on SO. If you have a follow-up question, ask a new one or add a comment/edit to your current one, someone will surely come up with an answer.
Pekka
A: 

The answer is simple:
Do not store images in the database.
Store it on the disk.

It will eliminate all these problems for you.

Col. Shrapnel
A: 

As with the other answers, the best solution is to separate the image from the html source code.

That being said, if you absolutely require the image to be part of the page, and are not using internet explorer, it is possible to embed the image in the page.

it should be as simple (an example being a png) as:

<img src="data:<?php echo $mimeType ?>;base64,<?php echo chunk_split(base64_encode($imageData)); ?>" />

This only works in non IE browsers (I'm not sure about IE8, as I don't have it around to test at the moment).

Just make sure you get the mime type right (ex, image/png for png).

A little bit more info: http://www.greywyvern.com/code/php/binary2base64

halkeye