Display blob as image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
karphp
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 12:39 pm

Display blob as image

Post 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
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Display blob as image

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Display blob as image

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Display blob as image

Post 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
Post Reply