PHP,使用Header()显示图像 [英] PHP, display image with Header()

查看:326
本文介绍了PHP,使用Header()显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

内容类型:image / png是让我迷惑的。



其他人帮我解决了这个问题,但我注意到并非所有的图片都是PNG。很多都是jpg或gif。

并且它们仍然显示成功。



有人知道为什么?

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

switch($ file_extension){
casegif:$ ctype =image / gif;打破;
casepng:$ ctype =image / png;打破;
casejpeg:
casejpg:$ ctype =image / jpeg;打破;
default:
}

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

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


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.

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.

does anyone know why?

解决方案

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;
    default:
}

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

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

这篇关于PHP,使用Header()显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆