Re: Re: Output Image from Db to Browser
| From: | graeme | Date: | Tue, 18 Jan 2005 08:31:59 +0000 |
| Subject: | Re: Re: Output Image from Db to Browser | ||
| References: | 1 2 3 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-25375@lists.php.net to get a copy of this message | ||
I've found a book that might help... "PHP graphics Handbook" from Wrox. The example they have to get an image from a MySQL table is as follows:
after a successful query...
$buffer = stripslashes(mysql_result($result,0,0);
header("Content-type: image/jpg");
echo $buffer;
... and then close the database connection and other tidying up tasks...
As I said it's straight from the book, so I hope it helps!
graeme
MikeA wrote:
I had tried that once but then I was requested the download the file. But I gave your suggestion a shot and I am getting an error message saying there is an error in the JPG file. BTW, looking back at my original post I accidentally left out how I was calling the function. I used this first. <img src="get_image('sub','s_sub_code', $row['s_sub_code'], 's_map')"> Then I tried get_image('sub','s_sub_code', $row['s_sub_code'], 's_map'); But I get that error in JPG message each time. So I am getting a little more closer. Here is how the function looks now. The things I tried are commented out so I won't forget what I did. function get_image($table, $pk_name, $pk_value, $blobfield) { login(); $query = 'SELECT '.$blobfield; $query .= ' FROM '.$table; $query .= ' WHERE '.$pk_name." = '".$pk_value."'"; //echo "<P>q=".$query."<P>"; $result = mysql_query( $query ) or die ("<P>Could not get image<P>"); if(mysql_num_rows($result) == 1 ) { header('Content-type: image/jpeg'); //header('Content-type: image/*'); $fileContent = mysql_result($result,0); //,$blobfield); $theimage = imagecreatefromstring ($fileContent); echo imagejpeg ($theimage); //echo $fileContent; //header('Content-type: ' . image_type_to_mime_type(IMAGETYPE_WBMP)); //image2wbmp($image); // output the stream directly } But I am trying to put it in a table in the browser. If I can get the JPG to at least show up I should be able to adjust it using a STYLE setting or TABLE. It is just getting it to output that has got me stumped so far. Thanks every so much for your help so far. Hope you can find the rest of it. Thanks. Mike "graeme" <graeme_foster@sherubtse.edu.bt> wrote in message news:41EC9DC3.4060506@sherubtse.edu.bt... Sorry running from memory here. You need to set your output headers to display a jpg not text. So you need something like: header("Content-type: image/jpg"); This will display the image on its own, if you need to embed the image in a web page you need a little more trickery. I'll try to look it up for you. But this should at least get you going on testing getting your image from the database. graeme MikeA wrote:-- Experience is a good teacher, but she sends in terrific bills. Minna AntrimI am trying to output a JPG image to the browser without creating a file. I have tried several things but nothing seems to work. I sure could use a whole lot of help right now! LOL Going crazy trying to figure this out. But I know there are PHP gurus out there that will have the answer in 2 seconds. Hopefully I'll be at that level someday too! The image comes from a MySQL Blob. The closest I get is a string of garbage on the browser display. I cannot get it to display a pictures. Suggestions, comments, help, almost anything is appreciated. PLEASE help me! Project due in a week. Thanks in advance. Mike if( mysql_num_rows($result) == 1 ) { //header('Content-type: image/*'); $fileContent = mysql_result($result,0); //,$blobfield); echo $fileContent; //$theimage = imagecreatefromstring ($fileContent); //echo imagejpeg ( $theimage);