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

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

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

php

提问by user3260816

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 支持启用 GD 版本 2.0 FreeType 支持启用 FreeType 与 freetype 版本 2.4.8 的链接

UPDATEIn IE even if i have header, i got bunch of data...

更新在 IE 中,即使我有标题,我也得到了一堆数据......

回答by Niet the Dark Absol

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 headerline, see what you get. If you get something that starts with PNGfollowed by a bunch of data, then you have a successful PNG image and you can uncomment the headerline to see it. Otherwise, you may see a PHP error.

注释掉这一header行,看看你得到了什么。如果你得到的东西PNG以一串数据开头,那么你就有了一个成功的 PNG 图像,你可以取消注释该header行来查看它。否则,您可能会看到 PHP 错误。

回答by dharmabook.ru

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 <?phpand no php output before headers and image data output.

需要检查 php 文件之前没有任何空格和换行符,<?php并且在标题和图像数据输出之前没有 php 输出。

回答by Tuckbros

When I tried, my problem was I didn't have the gd library. Once installed and set up, it worked fine.

当我尝试时,我的问题是我没有 gd 库。安装和设置后,它工作正常。

回答by David Spector

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;

回答by ?ükür Ata?

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

你的问题会解决

回答by AzCraft

Try with:

尝试:

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

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

instead of

代替

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

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

And check the file encoding. It has to be ANSI.

并检查文件编码。它必须是ANSI。