来自getImage.php的图片不会显示在img src中

时间:2017-10-11 09:27:51

标签: php mysql

所以基本上如果我转到getImage链接,会显示来自数据库的图像,但如果我在.php文件中使用它,它将显示图像但会调整大小为了适应卡片,它不会显示,而alt(这是头像)显示。

<center>
<img src="getImage.php" class="w3-circle" style="position:absolute; bottom:-20%; left:35.5%; width:30%" alt="Avatar">
</center>

然后是getImage.php的代码:

<?php
session_start();
require './Database.php';

// do some validation here to ensure id is safe

$sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
echo "<img src='".$row['IMAGE']."'>";
?>

我正在做的事情有什么问题吗?

1 个答案:

答案 0 :(得分:1)

<强>问题

HTML试图获取有效的图像文件,如jpg或png,但获取内容为<img src="...">的php / text文件。

<强>解决方案

您需要将php文件内容类型更改为图像并输出图像文件数据:

header("Content-Type: image/jpeg");//or image/png
echo file_get_contents("$imagepath");//file path not url!!!

<强>代码

<?php
  session_start();
  require './Database.php';

  header("Content-Type: image/jpeg");    

  // do some validation here to ensure id is safe

  $sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
  $result = mysqli_query($con, $sql);
  $row = mysqli_fetch_assoc($result);
  $path = $row['IMAGE'];//Maybe you need to change this if you only save an url in the database  
  echo file_get_contents($path);  

?>