Jump to content

[SOLVED] Retrieving image location


Scotty2024

Recommended Posts

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!

Link to comment
Share on other sites

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);
   }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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']);
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.