简体   繁体   中英

Image from getImage.php won't display in img src

So basically if I go to the getImage link, the image that comes from the database is displayed, but if I use it in a .php file where it will display the image but will resize it to fit the card, it won't show and the alt (which is avatar) shows.

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

Then the code for the 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']."'>";
?>

Anything wrong with what I am doing?

Problem

HTML trys to get an valid image file like jpg or png but gets an php/text file with the content <img src="..."> .

Solution

You need to change the php files content type to an image and output the image files data:

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

Code

<?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);  

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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