Question

I'm trying to test a simple GD php file, so I'm using a code I got from the net supposed to be working, here is my index main website page:

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

The code is supposed to draw a green rectangle in almost the middle of the image GD is enabled on my server, I get an empty page on chrome and this message on firefox: The image "link of my php page" cannot be displayed because it contains errors.

Was it helpful?

Solution

Just replace

$color = 4252460;

by

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

imagecolorallocate manual.

your code will generate this image: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top