Re: Retrieving Image Location in PHP from MySQL

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

 



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

[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