标题('内容类型:image / png');不再工作了?

时间:2014-03-21 14:05:30

标签: php string image png

我在描述中尝试了几次字符串。但它不断输出字符串而不是图像。链接在这里http://wenti.de/resize.php

源代码如下,

$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 );

是否有没有base64转换的解决方法?

1 个答案:

答案 0 :(得分:12)

在输出PNG图像内容之前,您的PHP脚本会发出UTF-8字节顺序标记(EF BB BF)。此标记可能导致PHP将内容类型默认为text / html。随后调用设置标头将被忽略,因为PHP已经发送了BOM。

BOM可能已经被文本编辑器放在开始标记之前的PHP脚本中,因为它以UTF-8格式保存文件。将编辑器中的格式更改为ANSI / ASCII,以便不写入BOM。

或者,你可以尝试调用PHP的ob_clean()函数来清除输出缓冲区,然后再更改标题。

相关问题