display image from mysql stored in blob format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsunitadhn
    New Member
    • Jan 2008
    • 82

    display image from mysql stored in blob format

    hi
    i am first time using blob files .When i use the blob file to diaplay data it is shown in binary format and i want to diplay that in image format.i am using the following code to extract image and show it.

    Code:
    <?php
     	$mysql=mysql_connect("localhost", "root", "");
    	mysql_select_db("skm") or die(mysql_error());
    
    	$sql = "SELECT emp_code,emp_mail,emp_picture FROM emp";
    
    
    	$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
     
    
    	
    	echo "<table><tr>  <th>Emp Code</th>  <th>Emp Name</th> </tr>";
    
    
    	        while($row=mysql_fetch_array($result)) 
    		{
    			echo "<tr>";
    			
    
    			echo "<td>{$row['emp_code']}</td>";
    			echo "<td>{$row['emp_mail']}</td>";
    			echo "<td>";
    			$image=($row['emp_picture']);
    			header("Content-type: image/jpg");
    			echo $image;
    			echo " </td>";
    			echo "</tr>";
    	 	}
    
    	echo "</table>";
    	
    	mysql_close($mysql);
    
    
    ?>
    Please help me to solve the problem
    Last edited by Atli; May 16 '10, 08:35 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The problem is that you are trying to print raw image data into a HTML page. That doesn't work. - To be able to accomplish what you are trying to do, you need to understand how HTML displays images: in short, it doesn't display them at all. It simply inserts <img> tags where an image is supposed to be displayed, and produces a location where the browser can fetch the image.

    This is how HTML handles images:
    [code=html]<img src="image1.jpe g" alt="Image 1">
    <img src="image2.jpe g" alt="Image 2">[/code]

    So to display an image from a database into a HTML document you need two documents: one for the HTML and one to fetch and print the image. The HTML should include <img> tags for each image that needs to be displayed, linking to the second file, passing along the ID or name of the image to be displayed.

    Each <img> tag should look something like this:
    [code=html]<img src="fetchImage .php?id=1" alt="Image 1">[/code]
    Where the "id=1" indicates the ID of the image to be displayed.

    The "fetchImage.php " script then uses that ID to fetch the image, set the proper headers and print the data. Nothing else can be printed, or the image will be corrupted. It essentially mimics an image; acting no different than if an actual image file had been requested.

    P.S.
    There is a way to inline the data into the tag, but I am ignoring that method on purpose. It is unreliable at the moment and tends to increase bandwidth usage way beyond the old-school method (due to the fact that binary data needs to be encoded in Base64, increases it's size by ~40%). - If you want to know the details about that method, see data URI schema on Wikipedia.

    Comment

    Working...