Images out of DB

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
Nightcat
Forum Newbie
Posts: 3
Joined: Tue Mar 13, 2007 8:43 am

Images out of DB

Post by Nightcat »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys

Got a problem and getting a bit desparate on my chances of solving it.

I am trying to display images that are stored in db

[syntax="sql"]
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);

As I work on it I managed to get one image to show, but I need to show all of them, currently there's 3 jpeg ons

Here's my code so far:[/syntax]

Code: Select all

<?php 
	$host = "";
	$user = "";
	$password = "";
	$database = "";
		
	$connection = mysql_connect($host,$user,$password)
			or die ("Couldn't connect to a server");
				
	$database = mysql_select_db($database, $connection);
	

	$query  = "SELECT id, type, content FROM upload";
	$result = mysql_query($query) or die('Error, query failed');

        $count = mysql_num_rows($result);


	if(mysql_num_rows($result) == 0)
	{
		echo "Database is empty <br>";
	} 

	else
	{
		while($results = mysql_fetch_array($result))
		{
			$content = $results["content"];
			$type = $results["type"];
			$id = $results["id"];
			
			
			$image= imagecreatefromstring($content);
			
			switch ($type){
			
			case "image/jpeg":
			header("Content-type: image/jpeg");
			$display = imagejpeg ($image);
			imagedestroy($image);
			break;
			
			case "image/png":
			header("Content-type: image/png");
			$display = imagepng($image);
			imagedestroy($image);
			break;
			
			case "image/gif":
			header ("Content-type: image/gif");
			$display = imagegif($image);
			imagedestroy($image);	
			break;
			
			}
		}

	}
	
?>


<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
	for ($i=0; i<$count; $i++){
		echo $count;
		echo' <img src="' . $display . '" /><br />';
	}
		

?>

</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Nightcat
Forum Newbie
Posts: 3
Joined: Tue Mar 13, 2007 8:43 am

Post by Nightcat »

To the admin

Thanks for the pointer :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Images and HTML cannot be in the same stream as you are apparently attempting to do. However, since imagejpeg(), among others, return a boolean, you won't even get that far. :P In fact, the content is already image data, there's no need to involve GD at all.

What needs to happen is your script containing the HTML needs to make references to another script for all the image source links. That script must fetch the image requested. Each image will be a separate request to that script. This is why it's rarely thought to be a good idea to store images in a database.
Nightcat
Forum Newbie
Posts: 3
Joined: Tue Mar 13, 2007 8:43 am

Post by Nightcat »

Are there any samples on the net I could look at?

Cause I already searched, but might be puting in wrong search phrases :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try the phrase "image* database" in our search, minus the quotes. Make sure to check the "all terms" option.
Post Reply