douyuben9434 2012-09-04 06:37
浏览 2383

如何正确使用content-type:image / jpeg?

I've created a page "student_picture.php" using the content-type:image/jpeg. im having problem when I want to add other text in that same page..

here is the sample of my code:

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

The image is successfully displaying but the echo $StudentNo is not displaying.. can anyone help me with my prob? thanks in advance.

  • 写回答

4条回答 默认 最新

  • doubo6658 2012-09-04 06:43
    关注

    You have to exit your script, because the header is sent to the client browser. If the $_SESSION["StudentNo"] is not present the script will process and will try to ouput your image.

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

    And that is not a proper way to output an image, echo "<img src='img/default_pic.gif'>"; with header('Content-Type: image/jpeg'); is not ok, either you return without that header or you read the image:

    if ($img == null){
        $img = 'img/default_pic.gif';
    } else {
        $img =  trim($img);
    }
    
    header('Content-Type: image/jpeg');
    readfile($img);
    

    or you lose the header tag

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

    So your script could be:

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

    I don't know the functions inside your DB connection, but I think you should use something like $row = sqlsrv_fetch_row( $stmt, SQLSRV_FETCH_ASSOC); and make without that while

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办