2012-09-04 10 views
1

我使用content-type:image/jpeg创建了一个页面“student_picture.php”。有问题,当我想在同一页面中添加其他文本..如何正确使用content-type:image/jpeg?

IM这里是我的代码示例:

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

的图像显示成功,但回声$ StudentNo没有显示..任何人都可以帮我解决我的问题吗?提前致谢。

+0

请参阅相关的问题http://stackoverflow.com/questions/2428004/how-can-i-use-multiple-php-header-content-类型在同一页上,这是可能的 –

回答

0

您必须exit您的脚本,因为header被发送到客户端浏览器。如果$_SESSION["StudentNo"]不存在,脚本将处理并将尝试输出您的图像。

<?php 
    session_start(); 
    if(!isset($_SESSION["StudentNo"])){ 
     header("location:login.php"); 
     die(); 
    } 

而且这不是一个正确的方式来输出图像,echo "<img src='img/default_pic.gif'>";header('Content-Type: image/jpeg');也不行,要么你回到没有这个头,或者你阅读的图像:

if ($img == null){ 
    $img = 'img/default_pic.gif'; 
} else { 
    $img = trim($img); 
} 

header('Content-Type: image/jpeg'); 
readfile($img); 

,或者你失去了头标签

if ($img == null){ 
    $img = "<img src='img/default_pic.gif'>"; 
} else { 
    $img = "<img src='".trim($img)."'>"; 
} 
echo $img; 

所以,你的脚本可以是:

<?php 
    session_start(); 
    if(!isset($_SESSION["StudentNo"])){ 
     header("location:login.php"); 
     die(); 
    } 
    $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){ 
      $img = "<img src='img/default_pic.gif'>"; 
     } else { 
      $img = "<img src='".trim($img)."'>"; 
     } 
     echo $img; 
     echo $StudentNo; 
    } 
?> 

我不知道你的数据库连接里面的功能,但我认为你应该使用类似$row = sqlsrv_fetch_row($stmt, SQLSRV_FETCH_ASSOC);并没有这种while

+0

谢谢你的建议。我会尝试你所说的。感谢很多 – user1645213

+0

我不能输出图像没有内容类型:image/jpeg ..我只会得到没有它的原始数据.. 我也有你的代码与readfile($ img)的错误; – user1645213

+0

我清楚地解释了你应该如何输出图像,'readfile'必须有一个有效的图像路径 –

0

当它在数据库中找到您的输出图像,你要么需要通过

<img src="anotherscript.php"> 

包括动态IMG或生成使用内置图像库中并存储在该数据库文件名,而不是保存原始数据的一个jpg文件

1

我认为在使用,image/jpeg或我知道的任何其他类型的图像格式时,您不能显示文字。文本只能用text/?或其他例外(如application/pdf)显示。

如果你不知道如何在单独的页面上显示图像,同时使用PHP文件,只需使用:

<img src="path/to/yourphpfile.php" /> 

就像任何其他的图像。

希望有帮助。

+0

是的,我认为是这样。我在使用content-type:image/jpeg时无法添加文本。我想我需要调用显示来自另一个页面的图像的页面。问题是我不知道该怎么做:( – user1645213

+0

@ user1645213我更新了我的答案,看一看 – think123

3

您可能需要使用下面的代码:

<?php 

$image = 'http://www.example.com/image.jpg'; 

$info = getimagesize($image); 

header('Content-Type: '.$info['mime']); 

echo file_get_contents($image); 

exit; 

?>