简体   繁体   English

无法显示PHP中数据库中的图像

[英]Cannot display Image from database in php

I am using smarty, mysql and I am just trying to display image. 我正在使用smarty,mysql,而我只是试图显示图像。 This is the error i am getting - 这是我得到的错误-

Resource interpreted as Image but transferred with MIME type text/html 资源被解释为图像,但以MIME类型text / html传输

My index.php file 我的index.php文件

<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 我的image2.php文件

$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. while循环内的此回声工作正常。

And when i inspect element and open this img link in new tab, Image is displaying. 当我检查元素并在新选项卡中打开此img链接时,将显示图像。 whereas its not displaying in current page. 而它不会显示在当前页面中。

Issue is that you are again delivering the html tags from the image file. 问题是您再次从图像文件传递了html标签。 You just need to output the image data with proper image content-type. 您只需要输出具有适当图像内容类型的图像数据即可。

Edit image2.php like 像编辑image2.php

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. 进行以上更改,并在浏览器中请求http://localhost/admin/image2.php?id=VALID_ID_HERE并检查其是否重新调整了图像。 Then you can use it in the src tag of index.php to show it there. 然后,可以在index.php的src标记中使用它以在此处显示它。 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. 如果在渲染图像时遇到错误,请在请求图像时确保在数据库中请求正确的图像ID,并使用所看到的错误消息更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM