By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,015 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,015 IT Pros & Developers. It's quick & easy.

image from MySQL database not working

P: n/a
<?php
include 'dbinc.php';
$result = mysql_query("SELECT image,mime_type FROM photos WHERE
image_id=".intval($_GET['id']), $link) or die("MyErr:".mysql_error());
if ($row = mysql_fetch_assoc($result)) {
$ext=array('image/jpeg'=>'jpg','image/gif'=>'gif','image/png'=>'png','image/x-png'=>'png');
header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
header("Content-Type: $row[mime_type]");
header("Content-Disposition: inline;
$_GET[id].".$ext[$row['mime_type']]);
echo "\r\n".stripslashes($row['image']);
}
mysql_free_result($result);
?>

what am I doing wrong? I don't get an image. I've seen thumbnail stuff. I
don't want a thumbnail (that code didn't work either for some reason).
Apr 15 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jim Michaels wrote:
<?php
include 'dbinc.php';
$result = mysql_query("SELECT image,mime_type FROM photos WHERE
image_id=".intval($_GET['id']), $link) or die("MyErr:".mysql_error());
if ($row = mysql_fetch_assoc($result)) {
$ext=array('image/jpeg'=>'jpg','image/gif'=>'gif','image/png'=>'png','image/x-png'=>'png');
header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
header("Content-Type: $row[mime_type]");
header("Content-Disposition: inline;
$_GET[id].".$ext[$row['mime_type']]);
echo "\r\n".stripslashes($row['image']);
}
mysql_free_result($result);
?>

what am I doing wrong? I don't get an image. I've seen thumbnail stuff. I
don't want a thumbnail (that code didn't work either for some reason).


I have no idea what you did wrong. What do you get? What's in the source?
What's in the database row?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 16 '06 #2

P: n/a
hi,
I think the second header call needs quotes for 'mime_type'

hope it helps

Apr 16 '06 #3

P: n/a

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:Df******************************@comcast.com. ..
Jim Michaels wrote:
<?php
include 'dbinc.php';
$result = mysql_query("SELECT image,mime_type FROM photos WHERE
image_id=".intval($_GET['id']), $link) or die("MyErr:".mysql_error());
if ($row = mysql_fetch_assoc($result)) {

$ext=array('image/jpeg'=>'jpg','image/gif'=>'gif','image/png'=>'png','image/x-png'=>'png');
header("Expires: Mon, 26 Jul 2030 05:00:00 GMT");
header("Content-Type: $row[mime_type]");
header("Content-Disposition: inline;
$_GET[id].".$ext[$row['mime_type']]);
echo "\r\n".stripslashes($row['image']);
}
mysql_free_result($result);
?>

what am I doing wrong? I don't get an image. I've seen thumbnail stuff.
I don't want a thumbnail (that code didn't work either for some reason).


I have no idea what you did wrong. What do you get? What's in the
source? What's in the database row?


found the problem. stripslashes and the newlines turned out to be a bug. so
echo "\r\n".stripslashes($row['image']);
became
echo $row['image'];

and the problem I was having with error messages about headers already being
sent came because dbinc.php had some blank lines after the ?>
:-/
well, at least I found it.
The Expires header isn't necessary. I should probably take it out. The ;
filename after inline isn't necessary either. (maybe it make things easier
for the cache, but I disabled the page cache via headers elsewhere)

I found another method would be to use imagecreatefromstring() and that
somehow you can detect the mime-type from that rather than storing it in the
db, butyou also have the problem that if the image is hefty (1MB), that
function will consume 16MB of RAM. combined with the use of another common
function, it would take a total of 32MB. PHP would then die. this function
is a memory hog (why?). Then you use imagejpeg().

I *thought* you were supposed to send a newline to signify end of headers
before sending image data. It seems I was wrong. I got the right size
image placeholder, but no image. weird. guess I'd have to look at the
format to see why that is. (?) maybe PHP does some of this for me
automatically or something behind the scenes.
From the command-line I discovered after I sent out headers, imagejpeg()
secretly inserts \r\n into the image data. I had to get out my editor to
binary-delete the first 2 bytes for the image to come out right. my old
copy of TSE 2.8 is still good for a few things. :-)

Apr 17 '06 #4

P: n/a

"foaud167" <f.*******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
hi,
I think the second header call needs quotes for 'mime_type'
that would cause an error.


hope it helps

Apr 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.