Page 1 of 1
How to display out the image?
Posted: Thu May 13, 2004 5:11 am
by teckyan888
Let say i already insert my image into mysql......Then,i need to display out but when i use selection query,There is not image display out....So,anyone who know how to display out the image using the php?Thanks...
Posted: Thu May 13, 2004 7:03 am
by launchcode
Here - this works for me.
Code: Select all
<?php
$id = $_GET['i'];
if (isset($_GET['i']))
{
$id = $_GET['i'];
}
// If the ID isn't numeric or is less than 1 (impossible) then stick in a default image
if (!is_numeric($id) || $id < 1)
{
Header("Content-type: image/gif");
fpassthru('default_image.gif');
exit;
}
else
{
mysql_connect('localhost', 'root', '') or die('Could not connect to MySQL');
mysql_select_db('test');
$sql = "
SELECT
*
FROM
images
WHERE
id = '$id'
";
$result = mysql_query($sql);
$image_mime = mysql_result($result, 0, 'mime');
$image_data = mysql_result($result, 0, 'data');
Header("Content-type: $image_mime");
echo $image_data;
exit;
}
?>
It assumes the image data is correctly loaded into a BLOB field and that you have the Images MIME Type as well. If they are always going to be GIF files for example - you can skip this part and echo out the image/gif header for yourself.
Call the script like:
<img src="show_image.php?i=x">
Posted: Thu May 13, 2004 9:45 am
by pickle
launchcode wrote:
<img src="show_image.php?i=x">
Will this work if I want to display multiple images stored in a db on a single HTML page? Thanks.
Posted: Thu May 13, 2004 11:00 am
by launchcode
You can have as many <img> tags as you like on one page, so yeah of course it will.
Posted: Thu May 13, 2004 11:08 am
by pickle
Ya, I realize that, but the part that threw me is where you output the MIME type. I thought you had to output all the headers before output was sent. Or are you sending a MIME header for the image, and not the page, which makes it ok?
Posted: Thu May 13, 2004 11:09 am
by launchcode
You cannot suck the image data out of MySQL and just display it on the page - that'll never work, it's just raw binary data! So you have to do it via a script (as I posted above) which will do all the content-type header stuff for you.
Posted: Thu May 13, 2004 11:11 am
by launchcode
Incidentally - you can only ever have 1 Header on a page, right? So you could never have an HTML page and then part way down start trying to output an image, because the header has already been sent and you can't jump in and out of things like that, it's not how http works.
Posted: Thu May 13, 2004 11:12 am
by magicrobotmonkey
you might be able to do with output buffering?
Posted: Thu May 13, 2004 11:16 am
by launchcode
No, because that still isn't how the http protocol works. Once a data stream has started, it has started - you cannot swap its meaning half way through. An HTML page (be it generated from PHP or not) is 1 request to a web server, every single external element on that page (img, external css file, etc) is more requests to the server - each one of those requests can only effectively have 1 content-type. So when you stick an <img> tag on a page, the server is sending a content-type of image/gif for example. One page can equal masses of separate calls to the server, but each one of those calls is unique.
Posted: Thu May 13, 2004 11:19 am
by pickle
If I'm downloading a page with 2 images, an HTTP connection is made for the page, and another connection is made for each of the 2 images (not necessarily at the same time), because HTTP is stateless and connections aren't persistent. Since I'm making a new connection for each image, outputing the header for each image should work, and shouldn't break anything (I'm kind of thinking out loud here). So I guess I'm saying I see how your script works now, and I agree that I can have multiple images displayed using it.

Posted: Thu May 13, 2004 11:21 am
by launchcode
Exactly

Posted: Thu May 13, 2004 11:31 am
by magicrobotmonkey
Oh yea that makes sense.