PHP,使用 Header() 显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2633908/
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 07:10:14  来源:igfitidea点击:

PHP, display image with Header()

phpheader

提问by coffeemonitor

I'm displaying images from outside my web root, like this:

我正在显示来自我的网络根目录之外的图像,如下所示:

header('Content-type:image/png');
readfile($fullpath);

The content-type: image/png is what confuses me.

content-type: image/png 使我感到困惑。

Someone else helped me out with this code, but I noticed that not all images are PNG. Many are jpg or gif.
And still they are displayed successfully.

其他人帮我解决了这段代码,但我注意到并非所有图像都是 PNG。许多是jpg或gif。
并且它们仍然显示成功。

does anyone know why?

有谁知道为什么?

回答by paullb

The best solution would be to read in the file, then decide which kind of image it is and send out the appropriate header

最好的解决方案是读入文件,然后决定它是哪种图像并发送适当的标题

$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpeg"; break;
    case "svg": $ctype="image/svg+xml"; break;
    default:
}

header('Content-type: ' . $ctype);

(Note: the correct content-type for JPG files is image/jpeg)

(注意:JPG 文件的正确内容类型是image/jpeg

回答by aliqandil

There is a better why to determine type of an image. with exif_imagetype

有更好的理由来确定图像的类型。使用exif_imagetype

If you use this function, you can tell image's real extension.

如果您使用此功能,您可以告诉图像的真实扩展名。

with this function filename's extension is completely irrelevant, which is good.

与此功能文件名的扩展名完全无关,这很好。

function set-header-content-type($file)
{
    //Number to Content Type
    $ntct = Array( "1" => "image/gif",
                   "2" => "image/jpeg", #Thanks to "Swiss Mister" for noting that 'jpg' mime-type is jpeg.
                   "3" => "image/png",
                   "6" => "image/bmp",
                   "17" => "image/ico");

    header('Content-type: ' . $ntct[exif_imagetype($file)]);
}

You can add more types from the link.

您可以从链接添加更多类型。

Hope it helps.

希望能帮助到你。

回答by cletus

Browsers make their best guess with the data they receive. This works for markup (which Websites often get wrong) and other media content. A program that receives a file can often figure out what its received regardless of the MIME content type it's been told.

浏览器根据接收到的数据做出最佳猜测。这适用于标记(网站经常出错)和其他媒体内容。无论被告知的 MIME 内容类型如何,接收文件的程序通常可以确定它接收到的内容。

This isn't something you should rely on however. It's recommended you always use the correct MIME content.

然而,这不是你应该依赖的东西。建议您始终使用正确的 MIME 内容。

回答by karim79

Browsers can often tell the image type by sniffing out the meta information of the image. Also, there should be a space in that header:

浏览器通常可以通过嗅探图像的元信息来判断图像类型。此外,该标题中应该有一个空格:

header('Content-type: image/png');

回答by Martijn

Though weirdly named, you can use the getimagesize()function. This will also give you mime information:

尽管名称很奇怪,但您可以使用该getimagesize()函数。这也将为您提供 mime 信息:

Array
(
    [0] => 295 // width
    [1] => 295 // height
    [2] => 3 // http://php.net/manual/en/image.constants.php
    [3] => width="295" height="295" // width and height as attr's
    [bits] => 8
    [mime] => image/png
)

回答by d.raev

if you know the file name, but don't know the file extention you can use this function:

如果您知道文件名,但不知道文件扩展名,则可以使用此功能:

public function showImage($name)
    {

         $types = [
             'gif'=> 'image/gif',
             'png'=> 'image/png',
             'jpeg'=> 'image/jpeg',
             'jpg'=> 'image/jpeg',
         ];
         $root_path  = '/var/www/my_app'; //use your framework to get this properly ..
         foreach($types as $type=>$meta){
             if(file_exists($root_path .'/uploads/'.$name  .'.'. $type)){
                 header('Content-type: ' . $meta);
                 readfile($root_path .'/uploads/'.$name .'.'. $type);
                 return;
             }
         }
    }

Note: the correct content-type for JPG files is image/jpeg.

注意:JPG 文件的正确内容类型是image/jpeg.