Re: Retrieving Image Location in PHP from MySQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The thing is, I do not have a website or a place where I am storing my images. I am storing the images on my local PC in folder as mentioned in my earlier post. I have tried using only the

else {
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
}

Which you have suggested. This is the piece of code which is returning all the binary character stuff. The code is definitely fetching the image. But it is not able to deliver to the browser in the form of an image. I am trying to read the image file as binary, evident from the FILE_BINARY command. So may be, that is causing the image to be displayed as binary. Is there any way we can convert the binary content to jpeg/jpg content again. This is my guess after having gone through the code again.\

Thanks,
Sashi

Nathan Nobbe wrote:
On Sat, Mar 7, 2009 at 8:12 PM, Sashikanth Gurram <sashi34u@xxxxxx> wrote:

I am just storing the location of the image(as a varchar type), not the
image itself. For example for a particular image, the location is stored as
C:\wamp\bin\apache\apache2.2.8\htdocs\Bldgs_lots\Burruss.jpg


echo "</table>\n";
$err=1;
if ($img = file_get_contents("$location", FILE_BINARY))
{
if ($img = imagecreatefromstring($img)) $err = 0;
}
if ($err)
{
header('Content-Type: text/html');
echo '<html><body><p style="font-size:9px">Error getting
image...</p></body></html>';
}
else
{
header('Content-Type: image/jpeg');
imagejpeg($img);
echo '<img src="mysqli.php?&img=' . $location . '" border="1" height="150"
width="200" alt="' . $build . '">';
imagedestroy($img);
}
?>

im really not sure why youre doing it this way, maybe you can elaborate..?
the more-or-less straightforward way to do this, if you want the url in an
image tag, is to expose access to these images through the webserver, so
that you might have a url like (assume youre site is mysite.com),

http://mysite.com/Bldgs_lots/Burruss.jpg

and at that point, including it in a page becomes trivial.

say you have the location, $location, from the db already, as above, then

$url = 'http://mysite.com/' . $location;
echo '<img src="' . $url . '" border="1" height="150" width="200" alt="' .
$build . '">';

also, if i were to guess why youre getting junk is b/c youre using the
header() function to tell the browser youre sending out an image.  i believe
it will then try to treat w/e you send it as the binary of a jpeg.  which
<img... is not.  if you are trying to send just the image and not create a
page of html, then theres no need for the <img> tag.  iirc, all you need to
do is call imagejpeg().  from the manual on imagejpeg(),

The path to save the file to. If not set or *NULL*, the raw image stream
will be outputted directly.

so basically, just,

else {
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
}

-nathan



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sashikanth Gurram
Graduate Research Assistant
Department of Civil and Environmental Engineering
Virginia Tech
Blacksburg, VA 24060, USA


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux