【问题标题】:Cannot display Image from database in php无法在php中显示数据库中的图像
【发布时间】:2014-12-03 22:07:12
【问题描述】:

我正在使用 smarty,mysql,我只是想显示图像。 这是我得到的错误 -

资源解释为图像,但使用 MIME 类型 text/html 传输

我的 index.php 文件

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

我的 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'];                     
}

while 循环中的这个 echo 工作正常。

当我检查元素并在新选项卡中打开这个 img 链接时,图像正在显示。而它没有显示在当前页面中。

【问题讨论】:

  • 为什么要在 index.php 中加倍 &lt;img&gt; 标签并作为 image2.php 的输出? index.php 中的&lt;img&gt; 标记期望内容类型为图像,但您从 image2.php 发回带有 &lt;img&gt; 标记列表的 html 代码,其中包含数据 uris 而不是内容类型图像和图像的二进制代码。
  • @Cheery。我只是在检查图像是否正在显示。
  • 所以,它没有显示,因为您没有将图像发送回浏览器。
  • 我在 index.php 的 标签中使用 id.. 调用我的 image2.php
  • 你不明白 http 请求是如何工作的。看看下面提供的答案。

标签: php mysql blob


【解决方案1】:

问题是您再次从图像文件中传递 html 标记。您只需要输出具有正确图像内容类型的图像数据。

编辑image2.php点赞

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

更新到下面的cmets

进行上述更改并在浏览器中请求http://localhost/admin/image2.php?id=VALID_ID_HERE 并检查它是否正在重新调整图像。然后你可以在index.php的src标签中使用它来显示它。如果您在渲染图像时遇到错误,当您请求它时,请确保您在数据库中请求正确的图像 ID,并使用您看到的错误消息更新问题。

【讨论】:

  • 谢谢.. 我试过了.. 但我想在 index.php 中显示我的图像。
  • image.php?您的问题中只有image2.php 和index.php?你指的是哪个文件?
  • @user3918082 : 没问题 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多