How to display out the image?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
teckyan888
Forum Commoner
Posts: 40
Joined: Tue May 11, 2004 10:46 am

How to display out the image?

Post 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...
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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">
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You can have as many <img> tags as you like on one page, so yeah of course it will.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

you might be able to do with output buffering?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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. :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Exactly :)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Oh yea that makes sense.
Post Reply