我使用 content-type:image/jpeg 创建了一个页面“student_picture.php”。当我想在同一页面中添加其他文本时遇到问题..
这是我的代码示例:
<?php
session_start();
if(!isset($_SESSION["StudentNo"])){
header("location:login.php");
}
$StudentNo = $_SESSION['StudentNo'];
require("includes/connection.php");
$sql = "SELECT StudentPicture from dbo.Students where StudentNo = '$StudentNo'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$img = $row['StudentPicture'];
if ($img == null ) {
echo "<img src='img/default_pic.gif'>";
} else {
$img = trim($img);
header('Content-Type: image/jpeg');
echo $img;
}
echo $StudentNo;
}
?>
图像已成功显示,但未显示 echo $StudentNo .. 谁能帮我解决我的问题?提前致谢。
最佳答案
您可能需要使用以下代码:
<?php
$image = 'http://www.example.com/image.jpg';
$info = getimagesize($image);
header('Content-Type: '.$info['mime']);
echo file_get_contents($image);
exit;
?>
关于php - 如何使用内容类型 :image/jpeg properly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258000/