PHP, display image with Header()(PHP,使用 Header() 显示图像)
问题描述
我正在显示来自我的网络根目录之外的图像,如下所示:
I'm displaying images from outside my web root, like this:
header('Content-type:image/png');
readfile($fullpath);
content-type: image/png 让我很困惑.
The content-type: image/png is what confuses me.
其他人帮我编写了这段代码,但我注意到并非所有图像都是 PNG.许多是 jpg 或 gif.
而且它们仍然显示成功.
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.
有人知道为什么吗?
推荐答案
最好的解决方案是读入文件,然后决定它是哪种图像并发出适当的标题
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);
(注意:JPG 文件的正确内容类型是 image/jpeg代码>)
(Note: the correct content-type for JPG files is image/jpeg)
这篇关于PHP,使用 Header() 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP,使用 Header() 显示图像
基础教程推荐
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
