Jump to content

retrieving images from a database.


Axil

Recommended Posts

I get gibberish instead of an image with this code

 

First off, keep in mind i'm pulling a binary stream from my SQL database *not* the path to some picture directory.

Secondly it's not possible that i not output anything else before the image, i know the problem has somthing to do with PHP not wanting to change the header, i also know there's supposed to be away around that by dumping the buffer before and after the header change but when i tried to do that i ran into more issues, can anyone tell me how to fix this?

 

function postBunny($name,$father,$mother,$litter,$dob,$breed,$color,$quality,$comments,$pic_name,$pic_size,$pic_type,$pic_content)

{

  echo <<<END

  <TABLE BORDER=1 height = 300px width = 550px>

  <TH colspan=3>

    <P align=center> $name  - Litter# $litter</P>

  </TH>

  <TR>

  <TD width=50%><DIV>

END;

header("Content-type: image/png");

echo ($pic_content);

header("Content-type:text/html");

echo <<<END

      </TD>

      <TD>

        <TABLE border=1 height=100% width=100%>

            <TR height=10%><TD><B>Mother: </B>$mother</TD>

            <TD><B>Father: </B>$father</TD> </TR>

            <TR height=10%><TD><B>Breed: </B>$breed</TD>

            <TD><B>Color: </B>$color</TD> </TR>

            <TR height=10%><TD><B>Quality: </B>$quality</TD>

            <TD><B> Born:</B> $dob</TD></TR>

            <TR height=10%><TD colspan=2 align=center>

            <B><U>Comments</U></B></DIV> </TD></TR>

            <TR><TD colspan=2 valign=top> $comments</TD></TR>

        </TABLE>

      </TD>

  </TR>

</TABLE>

END;

 

}

Link to comment
Share on other sites

I get gibberish instead of an image with this code

 

I am not surprised..

 

Create a sctipt that only displays the image (as an image)

ie

<?php
$ID = $_GET['ID'];
//Get Bin Info from database where ID=1 and set to $img
header("Content-Type: image/jpeg");
imagejpeg($img)
exit;
?>

 

now call the above script like an image, ie <img src="image.php?id=1">

 

that should work..

Link to comment
Share on other sites

I get gibberish instead of an image with this code

 

I am not surprised..

 

Create a sctipt that only displays the image (as an image)

ie

<?php
$ID = $_GET['ID'];
//Get Bin Info from database where ID=1 and set to $img
header("Content-Type: image/jpeg");
imagejpeg($img)
exit;
?>

 

now call the above script like an image, ie <img src="image.php?id=1">

 

that should work..

 

There's absolutely no way to do this within the function, without having to create another file to handle it?

Link to comment
Share on other sites

HTML can not convert bin data into an image.. so no..

HTML will need to read a link to an image, so for a dynamic image your need to create a file that generates an image.. this is common pratice

well can i at least but it in the same file, but in a different function?  sorry, i'm new to PHP all my experience is in high level languages like C++ and Java, so some of the restrictions are strange to me.

Link to comment
Share on other sites

[quote author=Axil link=topic=164156.msg720144#msg720144

well can i at least but it in the same file, but in a different function? 

you could.. just have an if statement from the start.. but in the long run it would be easier to do it as a independant file..

 

[quote author=Axil link=topic=164156.msg720144#msg720144

sorry, i'm new to PHP all my experience is in high level languages like C++ and Java, so some of the restrictions are strange to me.

this is a HTML restriction.. not PHP..

 

Link to comment
Share on other sites

Well i put it in another file, but now i get a blank image.

 

When i rightclick the image and select display image i get these errors

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/axil/public_html/image.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/axil/public_html/image.php:10) in /home/axil/public_html/image.php on line 11

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/axil/public_html/image.php on line 12

 

my code is

<?php
   $id= $_GET['id'];
   $db = *****;
   $host = *****;
   $user = ****;
   $pass =  *****;
   $link = mysql_connect ($host, $user, $pass) or die('connection to server failed');
   mysql_select_db($db,$link) or die('connection to database failed');
   $query= ('SELECT * FROM my_db WHERE id = $id');   
   $img =   mysql_result($query,0,"my_db.pic_content");
   header("Content-Type: image/jpeg");
   imagejpeg($img);
   exit;
?>

Link to comment
Share on other sites

whiups, made a mistake with my sql, and that's why the query didn't work right.. fixed it.... now i still get a blank image but when i rightclick it gives me the name of the page like "mydomain/images?id=1" etc

my code is

<?php
   $id= $_GET['id'];
   $db = *****;
   $host = *****;
   $user = ****;
   $pass =  *****;
   $link = mysql_connect ($host, $user, $pass) or die('connection to server failed');
   mysql_select_db($db,$link) or die('connection to database failed');
   $query= ('SELECT * FROM test WHERE bunny_id = ' . $id);
   $result = mysql_query($query,$link)or die('query failed');    
   $img =   mysql_result($result,0,"my_db.pic_content");
   header("Content-Type: image/jpeg");
   imagejpeg($img);
   exit;
?>

Link to comment
Share on other sites

i fixed it, the problem was with the imagejpg($img) function, for whatever reason it was giving me the problem i mentioned before but when i just used echo($img) it worked, i had thought that i'd read somewhere you could echo() binary if you set the headers appropriately, which leaves me wondering what the use of imagejpg() is... is there a way i could use imagejpg to output images *without* having to change the header, and thus avoid all these problems?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.