webguync Posted September 6, 2007 Share Posted September 6, 2007 Can someone point me in the right direction as to display an image in PHP that is being stored in MySQL? Basically I know how to store the image in MySQL, but getting it to display on a web page is what I am having trouble with. Any assistance is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/ Share on other sites More sharing options...
pocobueno1388 Posted September 6, 2007 Share Posted September 6, 2007 Well, assuming you have the filename stored in the database...do this: <?php //get the image from the DB $query = mysql_query("SELECT image FROM images WHERE ...")or die(mysql_error()); $row = mysql_fetch_assoc($query); //display the image echo "<img src='path/to/image/".$row['image']."'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342632 Share on other sites More sharing options...
cooldude832 Posted September 6, 2007 Share Posted September 6, 2007 poco I think that he is talking more along the lines of storing images as binary via a BLOB field. if so let me know I have a script to do so. Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342633 Share on other sites More sharing options...
pocobueno1388 Posted September 6, 2007 Share Posted September 6, 2007 poco I think that he is talking more along the lines of storing images as binary via a BLOB field. if so let me know I have a script to do so. Well, they really didn't say any of that...so I had to start somewhere. Webguync, you need to be a lot more specific if cooldude is right about what you want. Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342637 Share on other sites More sharing options...
cooldude832 Posted September 6, 2007 Share Posted September 6, 2007 I agree, its hard to help someone if they don't say what they want, just saying odds are they are looking at binary storage. Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342639 Share on other sites More sharing options...
webguync Posted September 6, 2007 Author Share Posted September 6, 2007 sorry to be vague in my original post. Yes, I am referring to storing images as binary via a BLOB field Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342666 Share on other sites More sharing options...
cooldude832 Posted September 6, 2007 Share Posted September 6, 2007 [code]I use 2 files to do this, you can see how the table is structured based on the query structure This is the viewer view.php?image_id=Numeric&width=Numeric [code] <?php // again we check the $_GET variable if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) { $sql = "SELECT ImageType, ImageTitle, ImageCat, Height, Width, ImageName FROM Images WHERE ImageID=".$_GET['image_id']; connectSQL(); $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); while($row=mysql_fetch_array($result)) { echo "<h3>".$row['ImageTitle']."</h3>"; echo "<img src=\"image.php?image_id=".$_GET['image_id']."&width=".$_GET['width']."\" alt=\"".$row['ImageName']."\" />"; echo "<br/>Image Name: ".$row['ImageName']; echo "<br/>Category: ".$row['ImageCat']; echo "<br/>Image Type: ".$row['ImageType']; echo "<br/>Width: ".$row['Width']."px Height: ".$row['Height']."px"; } } else { echo 'File not selected'; } ?> A file Image.php is also used here is the file <?php $id = $_GET['image_id']; if(!empty($_GET['width'])){ $size = $_GET['width']; } else{ $size = 500; } if($id) { connectSQL(); $query = "select Image, ImageType from Images where ImageID = $id"; $result = mysql_query($query) or die(mysql_error()); $data = mysql_result($result,0,"Image"); $type = mysql_result($result,0,"ImageType"); $type = "image/".$type; Header("Content-type: ".$type); $src = imagecreatefromstring($data); $width = imagesx($src); $height = imagesy($src); $aspect_ratio = $height/$width; $new_w = $size; $new_h = abs($new_w * $aspect_ratio); $img = imagecreatetruecolor($new_w,$new_h); imagecopyresized($img,$src,0,0,0,0,$new_w,$new_h,$width,$height); // determine image type and send it to the client if ($type == "image/pjpeg" || $type == "image/jpg" || type == "image/jpeg") { imagejpeg($img); } else if ($type == "image/x-png" || $type = "image/png") { imagepng($img); } else if ($type == "image/gif") { imagegif($img); } imagedestroy($img); } ?> Hope this gives you some ideas, this version accepts anything GD can spit back out via creating and matches image type which is very nice. I can explain it better if you have questions, the table structure can be derived from it pretty easily.[/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-342703 Share on other sites More sharing options...
webguync Posted September 6, 2007 Author Share Posted September 6, 2007 thanks, I am just now getting to this. I probably will have some questions once I look this over and will post them. thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-343356 Share on other sites More sharing options...
cooldude832 Posted September 6, 2007 Share Posted September 6, 2007 I'll let you figure out the uploader, but fyi I removed the image/ part of the file type in the database for efficiency purposes, so when you produce headers you will need to reapply it or modify the script accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/68153-displaying-an-image-from-a-mysql-db/#findComment-343363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.