GD图像未显示

时间:2014-03-26 14:42:29

标签: php gd

我正在尝试测试一个简单的GD php文件,所以我使用的是我从网上得到的代码,这里是我的索引主网页:

<?php
  error_reporting(E_ALL);
  $image = @imagecreate(110, 20) or die("Cannot Initialize new GD image stream");
  $background_color = imagecolorallocate($image, 0, 0, 0);
  $color = 4252460;
  $thick = 4;
  $x1    = 50;
  $y1    = 1;
  $y2    = 19;
  $x2    = 50;

  if ($thick == 1) {
    $img2 = imageline($image, $x1, $y1, $x2, $y2, $color);
  }
  else{
    $t = $thick / 2 - 0.5;

    if ($x1 == $x2 || $y1 == $y2) {
      $img2 = imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    else {
      $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
      $a = $t / sqrt(1 + pow($k, 2));
      $points = array(
                  round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
                  round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
                  round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
                  round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
                  );
      imagefilledpolygon($image, $points, 4, $color);
      $img2 = imagepolygon($image, $points, 4, $color);
    }
  }
  header("Content-Type: image/png");
  imagepng($image);
  imageDestroy($image);
?>

代码应该在图像的几乎中间绘制一个绿色矩形 在我的服务器上启用了GD,我在chrome上获得了一个空白页面,并在firefox上显示此消息:图片“我的php页面的链接”无法显示,因为它包含错误。

1 个答案:

答案 0 :(得分:0)

只需替换

$color = 4252460;

通过

$color = imagecolorallocate($image,0,255,0);

imagecolorallocate 手册。

您的代码将生成此图片: enter image description here