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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12258000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:09:45  来源:igfitidea点击:

how to use the content-type:image/jpeg properly?

phpsql-servermime-typescontent-type

提问by user1645213

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..

我使用 content-type:image/jpeg 创建了一个页面“student_picture.php”。当我想在同一页面中添加其他文本时遇到问题..

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.

图像已成功显示,但 echo $StudentNo 未显示.. 任何人都可以帮助我解决问题吗?提前致谢。

回答by Kliptu

You might need to use following code :

您可能需要使用以下代码:

<?php 

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

$info = getimagesize($image);

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

echo file_get_contents($image);

exit;

?>

回答by Lucas

I think that you cannot display text while using the Content-Typeof image/jpeg, or any other kind of image format that I know of. Text can only be displayed with text/?, or other exceptions such as application/pdf.

我认为您无法在使用Content-Typeofimage/jpeg或我所知道的任何其他类型的图像格式时显示文本。文本只能显示为text/?,或其他例外,例如application/pdf

If you don't know how to display the image on a separate page while using a php file, just use:

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

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

Just like any other image.

就像任何其他图像一样。

Hope that helped.

希望有所帮助。

回答by Mihai Iorga

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

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

<?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:

这是不以输出图像取之有道,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);

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

我不知道您的数据库连接中的功能,但我认为您应该使用类似的东西$row = sqlsrv_fetch_row( $stmt, SQLSRV_FETCH_ASSOC);并在没有它的情况下制作while

回答by Mike

You are outputting an image when it is found in the database, you would either need to include a dynamic img via

当在数据库中找到图像时,您正在输出图像,您需要通过以下方式包含动态 img

<img src="anotherscript.php">

Or generate a jpg file using one of the built in image libraries and storing its file name in that database rather than storing the raw data

或者使用内置图像库之一生成 jpg 文件并将其文件名存储在该数据库中,而不是存储原始数据