Forum Moderators: coopster

Message Too Old, No Replies

How to show BLOB image data from PHP

BLOB image data PHP

         

celebx

7:14 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



I am trying to return BLOB image data from PHP with the folowing code:

filename: image.php


<?php

// CODE FOR ACCEPTING GET VARIABLES IF SERVER DOESN'T SUPPORT IT AUTOMATICLY
if (ini_get('register_globals')!= 1) {
$supers = array('_REQUEST','_ENV','_SERVER','_COOKIE','_GET','_POST');
foreach ($supers as $__s) {
if (is_array($$__s) == true) extract($$__s, EXTR_OVERWRITE);
}
unset($supers);
}

$link = mysql_connect("localhost", "vs022_celebx", "****xx")
or die("Can't connect.");
mysql_select_db("vs022_celebx")
or die("Can't select database.");

if (!$id) { print "ERROR NO ID"; } else {

$result = mysql_query("SELECT * FROM se_keys where id='$id' LIMIT 1");
while($row = mysql_fetch_array($result)) {

if ($row[image]) {
header("Content-Type: $row[image_type]");
print "$row[image]";
}

}

}

?>

but the result is an continious loading file and no imagedata shown.

Please help!

Best Regards,
Snt

justageek

9:30 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First thing I would check is to see if you are getting anything at all from the database. If you are then echo out $row[image] directly just to see the data even though it won't be the image you expect to see. If those work then start checking the headers.

As a side note - It's usually better to store a pointer to the image instead of the actual image itself in the database. Depending on the size of the database and how you have your indexes set up it may just be taking a long time to find what your looking for and your code might be OK and you wouldn't know it.

JAG

Nikke

1:10 am on Jan 29, 2004 (gmt 0)

10+ Year Member



if a gif:
<?
Header("Content-type: image/gif");
?>

if a jpg:
<?
Header("Content-type: image/jpeg");
?>

So check what your $row["image_type"] actually contains. Also. Check the way the script refer to some of the values included in the $row values. I would move some quote chars I think.

sigg

4:19 am on Jan 29, 2004 (gmt 0)

10+ Year Member



Mine isn't working wither. Have you gotten it to work? I can pull all data except show the image. It pulls as junk...
----------start script--------------
<?
Header("Content-type: image/jpeg");
?>
<body>
<h1>test</h1>
<TABLE BORDER="1">
<?
//conect to database
$dbh=mysql_connect ("localhost", "user_name_here","password_here") or die ('I cannot connect to the database because: '. mysql_error());

mysql_select_db ("arrowhea_homes");
$result = mysql(arrowhea_homes, "select * from homes"); //results
$homes = mysql_numrows($result);
$i = 0;
while($i < $homes) {
echo "<TR>";
echo "<TD>";
echo mysql_result($result,$i,"MLS");
echo "</TD><TD>";
print mysql_result ($result,$i,"IMGSM1");
echo "</TD><TD>";
echo mysql_result($result,$i,"price");
echo "</TD>";
echo "</TR>";
$i++;}
?>
</TABLE>
</body>
--------------end -----------------------
kinda need my hand held here...

justageek

11:24 am on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sigg,

Yours cannot work because you are sending out the header command for an image and then html and then the image. If you send the header like you are doing then just send the image.

If you want to have a mix of html and images then the easiest thing to do is store a pointer to the image and where you have...

print mysql_result ($result,$i,"IMGSM1");

change it to an <img> tag where the $result is the location of the image. Then you can get rid of the header also.

JAG

celebx

11:56 am on Jan 29, 2004 (gmt 0)

10+ Year Member



Hi!

I didn't get it to work yet, I've tried any possible setup but it still doesn't work, the imagedata call via PHP does work when building real image files with it bt when calling it via an header or as direct output from PHP it just keeps loading the file...

Let me know if you know a solution.

Best Regards,
Snt

[edited by: celebx at 1:30 pm (utc) on Jan. 29, 2004]

justageek

12:06 pm on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you load it without the header() and get just the raw image stream is it complete?

JAG

celebx

1:32 pm on Jan 29, 2004 (gmt 0)

10+ Year Member



Yes, I've tried to output the data directly without the header, it gives the same unendless loading file as result...

The output of the BLOB data does seem to work since I am able to build real files with it from within PHP so it must be an other problem...

Let me know if you have an idear.

Best Regards,
Snt

justageek

3:07 pm on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried your code and it worked OK for me.

I manually assigned $id a character value of '1'.
I assigned the image_type a character value of 'image/jpeg'.
I uploaded a jpg image into the image blob.

I did get an error becuase if ($row[image]) had to be if ($row['image']) which caused the actual image not to show but once changed the image displayed just fine.

So assuming those variable are correct on your site then it should work. Double check to make sure.

JAG

celebx

6:53 pm on Jan 29, 2004 (gmt 0)

10+ Year Member



Hi!

Thanks for the support!

I tried adding the ' on the BLOBed image variable ($row['image']) but it didn't help...

Anyone else occured the same problem?

Best Regards,
Snt