Page 1 of 1

Problem with dynamic polygon drawing with GLOBALS array

Posted: Sun Dec 27, 2009 10:59 am
by cdeiwiks
Hi,
I am intrigued by the following problem, and I am not sure whether it has to do with the GD package or with something else...

I want to draw about 80 polygons, whose coordinates I store (two-dimensionally) in the $GLOBALS array, which is initialized in the file 'globals.inc.php':

Code: Select all

 
<?php
$GLOBALS['polygons'] = array (
    array (
    327,386, 333,389,338,392,344,394,350,397,350,394,350,390,355,388,355,385,356,382,358,379,364,378,368, 377,375,374,378,371,379,369,380,367,385,363,390,363,388,361,384,357,379,354,374,353,370,353,365,354,363,357,356,362,349,362,342,362,337,364,332,367,329,368,326,371,326,378,326,383),
    array (479,368,481,364,483,361,484,358,484,356,480,355,476,355,473,359,470,362,469,364,467,366,465,368,466,371,467,376,467,378,469,377,471,372,473,370,474,369,475,368),
    array (480,370,485,373,486,373,488,370,491,367,494,365,495,363,495,361,490,358,487,357,486,356,485,359,483,363,480,367),
...
...
);
 
 
In the main file, map.php, I create the image:

Code: Select all

 
<?php
include ("globals.inc.php") ;
// create a blank image
$image = imagecreate(700, 700) ;
// background color
imagecolorallocate($image, 0, 255, 0) ;  //first call to imagecolorallocate defines bg
// color
$bordercolor = imagecolorallocate($image,0, 0, 0) ;
 
/*-- this works: 
$polygon = $GLOBALS['polygons'][0];
imagepolygon($image, $polygon, count($polygon)/2, $bordercolor);
*/
 
//-- this doesn't:
$polygons = $GLOBALS['polygons'];
for($i = 0; $i < sizeof($polygons); $i++) {
    $polygon = $GLOBALS["polygons"][$i];
    imagepolygon($image, $polygon, count($polygon)/2, $bordercolor);
}
                     
// output the picture
header("Content-type: image/png") ;
imagepng($image) ;
?>
 

and later I include this with

Code: Select all

 
<img src="map.php" />
 
in my html file. After trying out a lot, it seems that the 'bad' line is the first line in the for-loop. When I replace the index $i with any integer (within the bounds of the array size), it works fine.

Any ideas of what I'm doing wrong? I need to loop over this set of polygons to dynamically draw them (different colors e.g.) , if you have another idea of how to implement this please let me know as well.

Thanks!

Christa

Re: Problem with dynamic polygon drawing with GLOBALS array

Posted: Sun Dec 27, 2009 11:04 am
by cdeiwiks
Just to be more clear: The posted code just does not create the image. The main html file which refers to map.php does not display anything.

Re: Problem with dynamic polygon drawing with GLOBALS array

Posted: Mon Dec 28, 2009 1:51 am
by manohoo
Try this instead:

Code: Select all

<?php
include ("globals.inc.php") ;
 
$image = imagecreate(700, 700) ;
imagecolorallocate($image, 0, 255, 0) ; 
$bordercolor = imagecolorallocate($image,0, 0, 0) ;
 
foreach ($GLOBALS['polygons'] as $polygon)
{imagepolygon($image, $polygon, count($polygon)/2, $bordercolor);}
 
header("Content-type: image/png") ;
imagepng($image) ;
?>