在php中使用imagecreate创建png时出错

在php中使用imagecreate创建png时出错,php,imagecreatefrompng,Php,Imagecreatefrompng,我正在创建验证码,这是我创建验证码图像的脚本 header("Content-type: image/png"); $width = 100; $height = 40; $im = ImageCreate($width, $height); $bg = ImageColorAllocate($im, 250, 250, 250); $border

我正在创建验证码,这是我创建验证码图像的脚本

    header("Content-type: image/png");
$width              = 100;
$height             = 40;
$im                 = ImageCreate($width, $height);

$bg                 = ImageColorAllocate($im, 250, 250, 250);

$border             = ImageColorAllocate($im, 191, 191, 191);
ImageRectangle($im, 0, 0, $width - 1, $height - 1, $border);

$text               = base64_decode($GLOBALS["captcha_akey"]);

$textcolor          = ImageColorAllocate($im,0,5,5);

$font               = 3;

$font_width         = ImageFontWidth($font);
$font_height        = ImageFontHeight($font);

$text_width         = $font_width * strlen($text);

$position_center    = ceil(($width - $text_width) / 2);

$text_height        = $font_height;

$position_middle    = ceil(($height - $text_height) / 2);

ImageString($im, $font, $position_center, $position_middle, $text, $textcolor);

ImagePNG($im);
验证码显示正确,但在验证码顶部我发现了此错误

    �PNG  IHDRd(u�� PLTE������IDAT8�퓻 �0DU�M�R�Y"�*=���)C)T�"��pXH���$����|�˟�$�9N����$獌�@�54�i[ -Hi\ku7��&Y���АLA�h;�Ah�^u�*���5 I:;���\�pd;��;ߓ�6b�(���'P�SY���IEND�B`�

看起来您的标题(标题(“内容类型:image/png”);)已在html内容中发送。所有的头文件都必须在其他文件之前输出。

我是通过在php.ini中启用gd扩展来实现的

在php.ini文件中取消对此行的注释:
extension=php_gd2.dll
(删除前导;)

要查找php.ini文件,请创建以下文件并上载到服务器并在浏览器中运行

    <?php
phpinfo();
?>

它将显示一条像这样的线, 配置文件(php.ini)路径/usr/local/lib 及
加载的配置文件/usr/local/lib/php.ini

是否发送内容类型头,其中包含类似
头(“内容类型:image/png”)当您显示图像时?如果没有它,它将只显示为文本,就像您显示的“error”一样。是的,我已经给出了在脚本开始之前,您可以显示实际显示图像的代码吗?也许是调用
readfile()
,或者是简单的
echo
?另外,
标记上方是否有第二个调用调用了不同的图像创建代码?