PHP Help needed....displaying an image embedded in a BLOB!
PHP Help needed....displaying an image embedded in a BLOB!
Author
Discussion

beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
Hello,

I was hoping somebody could help. I'm trying to get my PHP code to retrieve an image from a BLOB in MySQL and then display it in my PHP code. I've tried all sorts and this is what I have right now:

PHP said:
< img src="
<?php
header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename=hot_photo.jpg');
echo $row_hot_photo['picture1'];
?>
">


echo $row_hot_photo['picture1'] is the variable from the database extracted using Dreamweaver as a development tool.

Can anyone identify the problem and provide a solution!?

Thanks!

Edited by beanbag on Monday 25th September 15:26

BliarsGoing

72,863 posts

269 months

Monday 25th September 2006
quotequote all
Missing semi-colon here
echo $row_hot_photo['picture1']


I take it your MySQL statement runs before that line?

beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
BliarsGoing said:
Missing semi-colon here
echo $row_hot_photo['picture1']


I take it your MySQL statement runs before that line?


Ta! Just added the semi-colon, but it still doesn't work. Yup, the MySQL statement runs before the line.

BliarsGoing

72,863 posts

269 months

Monday 25th September 2006
quotequote all
What happens if you test the MySQL statement in DW?

beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
BliarsGoing said:
What happens if you test the MySQL statement in DW?


I get a text output. (Basically garbage). So the data is there, but I can't get it to display correctly.

eg.

S^+¡|>ñušê~!ð—ŏ éÒø¯ºÕ'ÐÕÇ ½‹Q¶šòÂòÖÞe·šæÙãIØ ³pÁ(ø³ák{›¿ ü?Ð|gca Ëqª|2ñ0·ºòÑrOö}þÁ»ð· í^3¢ë M.Öâãâ ÄÙã_ñ ¯¨ê^ ø§¢¬Z47w/æ¼mo ZüŒT ³ Êä _Uë .ÚÇJ Ôn¼ÇüûE‡ f! þ [s­¼öó[ßà æŸp¬/¬®á Ã4gï+ÆÀ« È# PêS…9J߯àïúlCÏǯÙïMñe·…¾"~Ò^/ðgŽ+sñ_Çßeø«ðÓÄ­àÏ Ok ·4 â i·š9þÅ G ÝŒ Œ(|óôÿ „?´÷‡¼ 5óüY‚ÓWñ_ï5­cÁÿ ü% «SgÝáýkº«£OEfÕ£ªkHÛk¹-dŸm }Z&/CšOˆŸ ´ kQ 7ø1«^øfÆÂ5O |'×a×RiÎ^Y~Á2Áv Wb Jr_® Xпhÿ„Z¶…e¬ê ,´ð¥Æ«rcÓ|;ã{I´ VmÒmVÖõ"‘¤u(ØPGÍ×½t ㏠èüx¿áV¾! jÿ 5(¼Ckïy8ŠéG°…±ïN¸ñ×Áÿ [ WÖü ª¾œÆoøF|ql–óA"Ã5ê£| ŸàÀ®9¸(íoËúù‹™Á]»_×SfÃÇÞ ñ^‡e¯ø/Å:W‰<5=Ìî|MámJ;ÛI!µ“dâ ã%$Ì C•$d¸ÎTãÑ9ëï_*x'öføyá xfÃáü ¿Ã­"-fÒöïFð.¯&›¥^[Dí; ´Õ&Ø ‰KJá HiÎ FkêœîôÁ ºRj)éý^¦°œ’» <ãó¨ÜÈ 0Š ³~ñ‰è(vaŒ ±8^*@xÏO­ é^ÐZ3ø J GsO Èïž¼þµ^èsè '<ŒqА(ëÇ Òc ôÆ1ž´úÉ Nâcpyà❏Lœu¤õÁëÒ¤ ÆsÓ¡ÇZ9´ uq ãœý )Û}ù O½; ‡ ¨Ï8ã>™¢.7"Rm ‡ ¥ 8£ñ£üñEÕÉMØvAîsŽi¹£ôÍ?¡ ñŽ ¦¤†0óÔ÷Èæ—Ž}Çj õéŒõ žT ïUÍ °ÂI øô¤î;žôâÁ c=©ÛxëרÏzi

slapmatt

1,132 posts

252 months

Monday 25th September 2006
quotequote all
If you do a "View Source" of the output, what do you get inside the img tag?

beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
slapmatt said:
If you do a "View Source" of the output, what do you get inside the img tag?


Along the lines of this:

<img src="ÿØÿà JFIF ´ ´ ÿá
Exif II* b h 2 € … Å i‡ ” Canon Canon EOS 300D DIGITAL 2005:02:25 08:12:08 š‚ ‚
'ˆ d  0221   & ‘ ‘ : ’
B ’ J ’
R ’ Z ’ ’
’ b |’ € j †’ ê   0100   ST   gh   ƒ„ ¢ ò ¢ ú ¢ ÃÄ ¢ Ö× £ çèé ¤ ú‘ ¤ Û‡ ¤ ? ¤ Q„ P 8
2005:02:25 08:12:08 2005:02:25 08:12:08 jR Šø ^ . ì H P ! X “ š ¬ Ì †v m  ª¹ ì p € 4d,
À
0 Á
J ª d ¨
n © ) ‚ ® P ´ Ô ¶ ÿÿ @ ÿÿÿ7 t ÿÿÿÿÿÿ ÿÿÿÿ ÿÿÿÿÿ ’ b d B   È Ÿ Ê ¬ Ž œ Ì … ü ÿÿ ÿÿÿÿÿÿIMG:EOS 300D DIGITAL JPEG Firmware Version 1.1.1 % d {F ™¾ y ] k : * % # <
N
[‚ X ) ê÷ ܆ ဠ¸~ QT j n fŽ »ì Ÿû ü= ^¬É ôN X „ý ïú ÊŸ òÜ «å «ç  © ²V ›^^ ‡]É êè %š  š Y: uG U« Çr Ô^ DØ v. Aʾ sû …Ë ±ê à= )è ü ¢˜ “÷ J s w ¯ Ñ × 0 ¿ è î ª * w w § Ì Š ‹ ñ Ÿ Å Æ r
Ò þ ^ ä R E : h x z x z — — ö `  ý
ü þ ú C ÿÿ Ÿ p ð/ 84& à. | @ S ÿÛ C ÿÛ C ÿÀ « € " ÿÄ
ÿÄ µ } !1A Qa "q 2‘¡ #B±Á RÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿÄ µ w !1 AQ aq "2 B‘¡±Á #3Rð brÑ

Plotloss

67,280 posts

300 months

Monday 25th September 2006
quotequote all
its picking up the actual image binary data rather than the location of the image by the looks of things.

Is the image actually stored in the database as a BLOB?

BliarsGoing

72,863 posts

269 months

Monday 25th September 2006
quotequote all
So you're getting what you ask for... You're just not performing the conversion by the looks of it scratchchin

Tripps

5,814 posts

302 months

Monday 25th September 2006
quotequote all
Is that PHP snippet part of the main page? As has been said, it looks like the binary data of the image is just being output inside the HTML, I may be wrong (don't do much browser work) but the src attribute doesn't take data that way...

The way I've always done images is to have a seperate page for the image source that returns the image in the format required, using much the same code as your snippet, so the image tag is along the lines of:

&lt;img src="myblobimage.php?myblonid=1234...

fredf

267 posts

263 months

Monday 25th September 2006
quotequote all
Can you inline images into the html file like this. I always assumed (and I could be wrong) that the browser needed to do a second request to the address given in the <img src= part.
The way I have done similar is to create a second php script which is called by the browser from the <img src= bit which prints the image blob

index.php
... stuff
<img src="image.php?img=pic2">
...

image.php
... get pic2
print $row_hot_photo['picture1'];

beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
BliarsGoing said:
So you're getting what you ask for... You're just not performing the conversion by the looks of it scratchchin


Yup. That's the bit I don't get since I've given it the image/jpg header yet it doesn't seem to acknowledge it as shown in the first bit of code..... Is it the way I have it written or as somebody suggested, should I create another page for processing the image?

Again, I tried this but got pretty much the same result:

Processing page said:
<?php require_once('Connections/Casalink.php'); ?>
<?php
mysql_select_db($database_Casalink, $Casalink);
$query_hot_photo = "SELECT picture1 FROM properties WHERE top_pick = 'yes'";
$hot_photo = mysql_query($query_hot_photo, $Casalink) or die(mysql_error());
$row_hot_photo = mysql_fetch_assoc($hot_photo);
$totalRows_hot_photo = mysql_num_rows($hot_photo);

header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename=hot_photo.jpg');
echo $row_hot_photo['picture1']

mysql_free_result($hot_photo);
?>


So the text in the main PHP HTML content should be:

HTML PHP Content said:

<img src="photo.php">


But again that doesn't work

Edited by beanbag on Monday 25th September 16:14

Tripps

5,814 posts

302 months

Monday 25th September 2006
quotequote all
[quote=beanbag
Processing page said:
<?php require_once('Connections/Casalink.php'); ?>
<?php
mysql_select_db($database_Casalink, $Casalink);
$query_hot_photo = "SELECT picture1 FROM properties WHERE top_pick = 'yes'";
$hot_photo = mysql_query($query_hot_photo, $Casalink) or die(mysql_error());
$row_hot_photo = mysql_fetch_assoc($hot_photo);
$totalRows_hot_photo = mysql_num_rows($hot_photo);

header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename=hot_photo.jpg');
echo $row_hot_photo['picture1']

mysql_free_result($hot_photo);
?>
I've not done much PHP so may be barking up the wrong tree... But one thing I've noticed is that you're returning the image content using echo, which might not like the high ASCII characters that will be contained in the data.

BliarsGoing

72,863 posts

269 months

Monday 25th September 2006
quotequote all
Can't you define a variable first such as

$MyPic=$row_hot_photo['picture1'];

And also define

$MyPic="images/filename.jpg" width="384" height="229";

Use double backslashes to comment out the BLOB line out till you get the image displayed correctly using something like

img src="<?php echo $MyPic;?>"

Then swing the code to use the MySQL variable instead.


beanbag

Original Poster:

7,346 posts

271 months

Monday 25th September 2006
quotequote all
BliarsGoing said:
Can't you define a variable first such as

$MyPic=$row_hot_photo['picture1'];

And also define

$MyPic="images/filename.jpg" width="384" height="229";

Use double backslashes to comment out the BLOB line out till you get the image displayed correctly using something like

img src="<?php echo $MyPic;?>"

Then swing the code to use the MySQL variable instead.




Cheers BliarsGoing! I defined the new var and it works. Also thanks to Tripps. I also used the print statement and that was part of the problem too!

Very happy now! thumbup

Thanks again all for your help!