0

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.

6
  • Why do you double <img> tags in index.php and as output of image2.php? <img> tag from index.php expects content-type to be image, but you are sending back from image2.php html code with list of <img> tags with data-uris and not content-type image and image's binary code. Commented Oct 9, 2014 at 4:55
  • @Cheery. I was just checking whether image is displaying or not thats it. Commented Oct 9, 2014 at 4:58
  • So, it not displaying because you are not sending image back to browser. Commented Oct 9, 2014 at 4:58
  • I am calling my image2.php in index.php's <img> tag using id.. Commented Oct 9, 2014 at 5:02
  • you do not understand how http requests are working. look at the answer provided below. Commented Oct 9, 2014 at 5:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. i tried.. but i wanted to display my image in index.php.
image.php? there are only image2.php and index.php in your question? which file you are refering to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.