草庐IT

PHP GD : The image cannot be displayed because it contains errors

coder 2024-04-27 原文

我尝试通过 PHP GD 创建验证码。但不幸的是我遇到了一个问题! PHP 告诉我:

The image “http://127.0.0.1/par.php” cannot be displayed because it contains errors.

我的代码是这样的

<?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);
  $red = imagecolorallocate($im, 255, 0, 0); 
  for ($i=0;i<=120;$i=$i+1){
      for ($j=0;$j<=20;$j=$j+1){
          imagesetpixel($im, $i,$j,$red);
      }
  }
  imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
  imagepng($im);
  imagedestroy($im);
?>

最佳答案

$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');

你首先隐藏真正的错误并尝试显示一些东西......

因为你不去寻找它而无法显示,

并公开图像,无论它是否真的生成。

然后你继续使用 stackoverflow,希望有人能猜出你可能只是使用 @ 抑制的错误。运营商。

确保<?php之前没有任何内容如果有,请删除 ?>在最后。

为确保您已安装 GD,请在新的 php 文件中尝试此操作:

<?php
if (extension_loaded('gd') && function_exists('gd_info')) {
    echo "PHP GD library is installed on your web server";
}
else {
    echo "PHP GD library is NOT installed on your web server";
}

关于PHP GD : The image cannot be displayed because it contains errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493957/

有关PHP GD : The image cannot be displayed because it contains errors的更多相关文章

随机推荐