简体   繁体   English

PHP 标头 ('Content-Type: image/png')

[英]Php header ('Content-Type: image/png')

I have a problem with header in php.我在 php 中遇到标题问题。

I havet this simple code.我有这个简单的代码。

<?php
header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>

I think it should works fine, but it didn't.我认为它应该可以正常工作,但它没有。 There is no error, and in top-left corner of my browser a little crashed picture icon shows up.没有错误,在我的浏览器的左上角出现了一个小崩溃的图片图标。

I think header is the problem.我认为标题是问题所在。

other useful infos: PHP Version 5.3.10-1ubuntu3.9 GD Support enabled GD Version 2.0 FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.8其他有用信息: PHP 版本 5.3.10-1ubuntu3.9 GD Support enabled GD Version 2.0 FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.8

UPDATE In IE even if i have header, i got bunch of data...更新在 IE 中,即使我有标题,我也得到了一堆数据......

For draw image in browser its have to send two headers before the main image data.为了在浏览器中绘制图像,它必须在主图像数据之前发送两个标题。 First it is type of the data首先是数据类型

header ('Content-Type: image/png'); 
//or
header ('Content-Type: image/jpeg');

and second it is data length.其次是数据长度。 Without that header browser will be not know where the data ended.没有那个标题浏览器将不知道数据在哪里结束。

header("Content-length: $size");   //$size image file size in bytes

need check php file have not any spaces and new lines before <?php and no php output before headers and image data output.需要检查 php 文件在<?php之前没有任何空格和换行符,并且在标题和图像数据输出之前没有 php 输出。

The "little crashed picture" indicates that the image is invalid. “小崩溃图片”表示图片无效。 Since your code to generate it seems valid, you must have a PHP error in there.由于您生成它的代码似乎有效,因此您必须在其中存在 PHP 错误。

Comment out the header line, see what you get.注释掉header行,看看你得到了什么。 If you get something that starts with PNG followed by a bunch of data, then you have a successful PNG image and you can uncomment the header line to see it.如果你得到的东西以PNG开头,后面跟着一堆数据,那么你就有了一个成功的 PNG 图像,你可以取消对header行的注释来查看它。 Otherwise, you may see a PHP error.否则,您可能会看到 PHP 错误。

When I tried, my problem was I didn't have the gd library.当我尝试时,我的问题是我没有 gd 库。 Once installed and set up, it worked fine.安装和设置后,它工作正常。

The correct (tested) PHP code for obtaining and returning a favicon (just change the MIME content type for other images) is:用于获取和返回收藏夹图标(只需更改其他图像的 MIME 内容类型)的正确(经过测试的)PHP 代码是:

$c = file_get_contents($path,true);
$size = filesize($path);
header ('Content-Type: image/x-icon');
header ("Content-length: $size");
echo $c;
exit;

Your problem is path of folder Create a folder for image你的问题是文件夹的路径为图像创建一个文件夹

for example.: barla例如:巴拉

After change your header header("Content-type: barla/image/png");更改标题后 header("Content-type: barla/image/png");

Your problem will solve你的问题会解决

Try with:尝试:

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

instead of代替

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

And check the file encoding.并检查文件编码。 It has to be ANSI.它必须是ANSI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM