Jump to content

View Image in browser


sandy1028

Recommended Posts

//DB Connection

$sql ="select img_val from image where id=152";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    $Image=$row['img_val'];
#       print "Image is $Image";
}

header( "Content-type: image/jpeg");
echo "<img src=\"$Image\"; alt=\"Image from DB\" />";

When I view the file in browser, some characters are printed and not the image.

Please tell me how to view the exact image in the browser.

Link to comment
https://forums.phpfreaks.com/topic/204068-view-image-in-browser/
Share on other sites

The <img src="..." attribute value MUST BE a URL to an image, NOT binary image data.

 

The URL that you put into the src="..." attribute must cause the correct Content-type: header to be output, followed by the binary image data.

 

Since you are outputting the binary image data using a .php script, you would put a .php file in as the URL. You will also typically put a GET parameter on the end of the URL that indicates which image you want (so that you can use the same .php file to output any available image.)

 

The <img tag that you produce for your web page would look something like -

 

<img src="myimage.php?id=152" alt="">

 

The myimage.php file (or whatever name you choose) will need to get the id (using $_GET['id']), validate it, and put that into the query in order to retrieve the correct binary image data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.