Jump to content

[SOLVED] Problem with array for png imagefilledpolygon


Transistor

Recommended Posts

This has my head wrecked! :-\

I'm drawing a 4-point polygon for a png. I pass an 8-value array, $values, to imagefilledpolygon() to plot the poly. It doesn't work. I create an identical(?) array, $valuestest, and it does work. I do a var_dump for both and they appear identical.

$values     = array($X[0], $Y[0], $X[1], $Y[1], $X[3], $Y[3], $X[2], $Y[2] );
$valuestest = array(   10,    50,    31,    34,    86,    73,    64,    89);
//                      -----0----  ------1-----  ------3-----  -----2------
if ($debug) { 
  var_dump($values);echo "(values)<br>";
  var_dump($valuestest);echo "(valuestest)<br>";
} else {
  header('Content-type: image/png');
  $img = imagecreatetruecolor($imgw, $imgh);  // set earlier in the script
  $blue = imagecolorallocate($img, 0, 0, 255);
  // draw a polygon
  if (!$test) {
    imagefilledpolygon($img, $values, 4, $blue);
  } else {
    imagefilledpolygon($img, $valuestest, 4, $blue);
  }
  imagepng($img);
  imagedestroy($img);
}

 

If debug is true then I get a dump as follows:

array( { [0]=> int(10) [1]=> int(50) [2]=> int(31) [3]=> int(34) [4]=> int(86) [5]=> int(73) [6]=> int(64) [7]=> int(89) } (values)
array( { [0]=> int(10) [1]=> int(50) [2]=> int(31) [3]=> int(34) [4]=> int(86) [5]=> int(73) [6]=> int(64) [7]=> int(89) } (valuestest)

 

In normal mode (both debug and test false) I get a blank (black) image.

If I set test true then I get a blue polygon on the black background (the desired result).

 

I can't see any difference in the array contents. Why does $values fail while $valuestest works? ??? By creating the $values array with the contents of other arrays have I created a problem?

 

Many thanks for taking the time to look at this.

this code works fine

<?php
$test=$debug=0;
$X = array(10,31,64,86);
$Y = array(50,34,89,73);   
$values     = array($X[0], $Y[0], $X[1], $Y[1], $X[3], $Y[3], $X[2], $Y[2] );
$valuestest = array(   10,    50,    31,    34,    86,    73,    64,    89);
//                      -----0----  ------1-----  ------3-----  -----2------
if ($debug) { 
  var_dump($values);echo "(values)<br>";
  var_dump($valuestest);echo "(valuestest)<br>";
} else {
  header('Content-type: image/png');
  $img = imagecreatetruecolor(100,100);  // set earlier in the script
  $blue = imagecolorallocate($img, 0, 0, 255);
  // draw a polygon
  if (!$test) {
    imagefilledpolygon($img, $values, 4, $blue);
  } else {
    imagefilledpolygon($img, $valuestest, 4, $blue);
  }
  imagepng($img);
  imagedestroy($img);
}?>

 

are you only populating the $X and $Y arrays if $test is true, earlier in the script?

There are no conditions prior to populating the $X and $Y arrays. There are some calculations to fill them. The var_dump proves that my filling the array worked.

 

I'll have another look and see if I've missed something stupid. Thanks again.

 

Anyone else spot a boo-boo in my code?

OK. I think I've found it. I inverted the $debug logic and, lo and behold I found that all the array values are = 50. This will give me a polygon all on one point! No wonder I can't see it.

 

It's a good old-fashioned coding error. Thanks for the prompts.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.