我有一个PHP文件,通过设置mime类型为image/png
来模仿PNG文件。
由于我有一个.htaccess
文件,我可以通过以下方式访问我的图像:
- /img/img
- /img/img.png
- /img/img.php
和所有这些都在我的浏览器中运行良好,但他们不能加载我的Teamspeak服务器
我不确定你期望发生什么,这些是返回的头:
CF-Cache-Status
MISS
CF-RAY
21cddbcc942220ae-LAX
Cache-Control
no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection
keep-alive
Content-Encoding
gzip
Content-Type
text/html; charset=UTF-8
Date
Fri, 28 Aug 2015 06:11:24 GMT
Expires
Thu, 19 Nov 1981 08:52:00 GMT
Pragma
no-cache
Server
cloudflare-nginx
Transfer-Encoding
chunked
Vary
Accept-Encoding
X-Powered-By
PHP/5.4.41-0+deb7u1
首先,返回text/html。这不是.png图像的正确mime类型。
其次,脚本需要实际返回.png数据。
我假设这里您计划使用GD或ImageMagick在脚本中创建.png,但是无论您计划做什么,当期望图像时,当您没有实际提供适当的mime类型和期望在该mime类型之后的适当图像数据时,什么都不会出现。
在脚本顶部设置mime类型非常简单,如下所示:
header('Content-type: image/png');
你也正在经历Cloudflare。您应该关闭Cloudflare,直到调试完毕并确保脚本按预期运行为止。
感谢@gview的回答,我发现Cloudflare正在保护/img/
文件夹,所以我做了一个页面规则来阻止这一点。万一你需要它,现在这是我的脚本,它设置了正确的头:
# Generate $im and Stuff
# ...
# Now Output it
http_response_code(200);
$file = "./temp.png";
imagepng($im, $file);
header("Content-Type: image/png");
header("Content-Length: ".filesize($file));
readfile($file);
exit;