user3918082
user3918082

Reputation: 25

Cannot display Image from database in php

I am using smarty, mysql and I am just trying to display image. This is the error i am getting -

Resource interpreted as Image but transferred with MIME type text/html

My index.php file

<img src="http://localhost/admin/image2.php?id={$editWine[0].id}" width="150" height="260" border="0" class="bottle-img" id="smallImageWineEdit" />

My image2.php file

$id=$_REQUEST['id'];        
$sql="SELECT * FROM table WHERE id=$id";        
$result=mysql_query($sql) or die(mysql_error());
while($row=@mysql_fetch_assoc($result))
{
   echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image_data'] ).'" width="150" height="150" /> &nbsp';  
   echo $row['image_data'];                     
}

This echo inside while loop is working fine.

And when i inspect element and open this img link in new tab, Image is displaying. whereas its not displaying in current page.

Upvotes: 0

Views: 1633

Answers (1)

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

Issue is that you are again delivering the html tags from the image file. You just need to output the image data with proper image content-type.

Edit image2.php like

if($row=@mysql_fetch_assoc($result))
{
   header('Content-type: image/jpg');
   echo $row['image_data'];
   exit();                   
}

update to comments below

Do the above changes and request http://localhost/admin/image2.php?id=VALID_ID_HERE in browser and check if its retuning an image. Then you can use it in the src tag of index.php to show it there. If you get errors in rendering the image, whn you request it, make sure you are asking for the correct image id in the db and update the question with the error messages you are seeing.

Upvotes: 1

Related Questions