Scotty2024 Posted October 27, 2009 Share Posted October 27, 2009 I have the location of images saved in a database. The images themselves are not stored in the database, only their location. The picture ID and location is stored in the database. The idea is that you search for a picture based on its ID and it's location is returned. So I built a retrieve.php page that finds the location and a display.php page which shows the image. For example display.php <img src="retrieve.php?id=<?PHP echo picID; ?>"> retrieve.php $id = htmlentities($_GET['id']); if($id != '') { $query = "SELECT location FROM pictures WHERE id='$id'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) { $imageData = mysql_fetch_array($result); echo $imageData['location']; } } This echos the correct path of the image (which is just text), but it fails to display in display.php. How can I get the location from retrieve.php to display.php? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/ Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 What you're trying to do is the set the source of an image to a page containing only html. What you're going to want to do is look into the PHP GD Library. What you can do is using imagecreatefrom*() (imagecreatefrompng, etc..) create the imageresource from the location then output it to the browser using image*() (imagepng(), etc..). Assuming it's a .png image it would be something like this: $id = htmlentities($_GET['id']); if($id != '') { $query = "SELECT location FROM pictures WHERE id='$id'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) { $imageData = mysql_fetch_array($result); $im = imagecreatefrompng($imageData['location']); header('Content-type: image/png'); // Set Content-type header to image/png to output an image imagepng($im); } } Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/#findComment-945739 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 To just output an image as is, use readfile after the Content-type header. You will also either need to store the correct content-type with each image (assuming the can be different types) or you will need to test the file extension to determine the correct content-type header to output. Using the GD functions takes a huge amount of server resources because it creates an uncompressed bitmap image of the file. If you are not manipulating the image, there is no need to use GD. Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/#findComment-945784 Share on other sites More sharing options...
Scotty2024 Posted October 28, 2009 Author Share Posted October 28, 2009 Hello AlexWD and PFMaBiSmAd. Alex, I was able to get your method to work! Thanks! PFM, I couldn't get readfile() to work. Do I switch readfile() with imagepng() and add ob_clean() flush() in Alex's example? And can I still use <img src="retrieve.php?id=<?PHP echo picID; ?>"> in display.php to retrieve the image? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/#findComment-946539 Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 No, my example was to get it from the url, not a blob field. For PFMaBiSmAd's method you'd do this: $id = htmlentities($_GET['id']); if($id != '') { $query = "SELECT location FROM pictures WHERE id='$id'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) { $imageData = mysql_fetch_array($result); header('Content-type: image/png'); // Set Content-type header to image/png to output an image readfile($imageData['location']); } } Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/#findComment-946540 Share on other sites More sharing options...
Scotty2024 Posted October 28, 2009 Author Share Posted October 28, 2009 Thanks again Alex. I got both methods to work! Quote Link to comment https://forums.phpfreaks.com/topic/179250-solved-retrieving-image-location/#findComment-946541 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.