Page 1 of 1

Display blob as image

Posted: Tue Aug 16, 2011 12:49 pm
by karphp
Hello All,

I read previous posts and followed the advice about mime types. Yet when I try to display a blob from a mysql database, php prints binary data on the page instead of rendering the data as in image.

This is a phonebook data base. And the entry is the users profile picture. The data structure for the image is a blob. Here is how I am printing the output:

Code: Select all

echo "<table cellspacing=\"0\" border=\"0px\" bordercolor=\"#aaaacc\" width=\"100%\" style=\"position: static; z-index: auto; \">";

		echo "<tr> <td><b>First Name</b></td> <td><b>Last Name</b></td> <td><b>Extension</b></td><td></td></tr>";

		//while($row = mysql_fetch_array($result))
		while($row = mysql_fetch_assoc($result))
		{
			echo '<tr>';
			echo '<td>'. $row['First'] .'</td>';
			echo '<td>'. $row['Last']  .'</td>';
			echo '<td> '. $row['Extn']  .'</td>';
			$content = 'header(\'Content-type: image/png\')'.$row['Image'];
			echo '<td>'. $content .'</td>';
			echo '</tr>';
		}
		echo "</table>";
I tried double quotes and single quotes in the mime type to avail. I still get binary data on the page intead of an image.

Thanks
KP

Re: Display blob as image

Posted: Tue Aug 16, 2011 1:23 pm
by phphelpme
Are you storing the whole image breakdown as a value in the database and then getting php to render the image for you so you actually never have an image on your server its just code in your database?

If so should your header statement not be this:

Code: Select all

 $content = "header('Content-type: image/png')".$row['Image']."";
An example would be:

Code: Select all

// select our database
        mysql_select_db("blob") or die(mysql_error());
 
        // get the image from the db
        $sql = "SELECT image FROM blob WHERE image_id=0";
 
        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
        // set the header for the image
        header("Content-type: image/png");
        echo mysql_result($result, 0);
 
Best wishes

Re: Display blob as image

Posted: Tue Aug 16, 2011 2:36 pm
by AbraCadaver
You can only issue the header once. The way to do it is call a PHP page that outputs the image using an img tag:

viewtopic.php?f=1&t=110171#p582727

Re: Display blob as image

Posted: Tue Aug 16, 2011 2:44 pm
by phphelpme
Yes, you are totally correct such as:

Code: Select all

while($row=mysql_fetch_array($result)) {
            echo 'This is '.$row['image_name'].' from the database<br />';
            echo '<img '.$row['image_size'].' src="blob.php?image_id='.$_GET['image_id'].'">';
Works like a charm :)

Thanks for the heads up.

Best wishes