How to display out the image?
Moderator: General Moderators
-
teckyan888
- Forum Commoner
- Posts: 40
- Joined: Tue May 11, 2004 10:46 am
How to display out the image?
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...
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
Here - this works for me.
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">
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;
}
?>Call the script like:
<img src="show_image.php?i=x">
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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.
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. 
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA