简体   繁体   English

标题('Content-Type:image / png'); 不再工作了?

[英]header('Content-Type: image/png'); not working anymore?

I have tried several times the string in the description. 我在描述中尝试了几次字符串。 But it keeps outputting a string instead of an image. 但它不断输出字符串而不是图像。 Link is here http://wenti.de/resize.php . 链接在这里http://wenti.de/resize.php

Source code is below, 源代码如下,

$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
  $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );

Is there a workaround without base64 conversion? 有没有base64转换的解决方法?

Your PHP script is emitting a UTF-8 byte order mark (EF BB BF) before outputting the PNG image content. 在输出PNG图像内容之前,您的PHP脚本会发出UTF-8字节顺序标记(EF BB BF)。 This marker is probably causing PHP to default the content type to text/html. 此标记可能导致PHP将内容类型默认为text / html。 Your subsequent call to set the header is ignored because PHP has already sent the BOM. 您的后续调用设置标头将被忽略,因为PHP已经发送了BOM。

The BOM has probably been placed in your PHP script just before the opening tag by your text editor, because it's saving the file in UTF-8 format. BOM可能已经由文本编辑器放在开始标记之前的PHP脚本中,因为它将文件保存为UTF-8格式。 Change the format in your editor to ANSI/ASCII so that the BOM is not written. 将编辑器中的格式更改为ANSI / ASCII,以便不写入BOM。

Alternatively, you could try calling PHP's ob_clean() function to clear the output buffer before changing the header. 或者,您可以尝试在更改标头之前调用PHP的ob_clean()函数来清除输出缓冲区。

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

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