2012-07-26 39 views
1

我有下面的代码,使用GD的图像上绘制绿色方块:PHP GD在foreach循环绘制

$img = imagecreatefrompng("images/dota.png"); 
$radiantcolor = imagecolorallocate($img, 7, 251, 15); 
$direcolor = imagecolorallocate($img, 250, 2, 0); 

$x = array(
0 => 38, 
1 => 45, 
2 => 25, 
3 => 29, 
4 => 34, 
5 => 62, 
6 => 83, 
7 => 116, 
8 => 76, 
9 => 135, 
10 => 232, 
); 

$y = array(
0 => 234, 
1 => 240, 
2 => 205, 
3 => 161, 
4 => 116, 
5 => 219, 
6 => 198, 
7 => 171, 
8 => 256, 
9 => 260, 
10 => 257, 
); 

foreach ($towerstatus_radiant as $key => $tower){ 
if ($tower == 1){ 
    $x = $x[$key]; 
    $y = $y[$key]; 
    imagefilledrectangle($img, $x, $y, $x+8, $y+8, $radiantcolor); 
} 
} 

header('Content-Type: image/png'); 
imagepng($img); 

它的工作原理只是第一方阵罚款,但在此之后,该商场仅似乎是放置在左上角,这样的:

为什么会这样?

+0

'$ towerstatus_radiant'没有在你的代码中定义,所以没有人应该能够告诉你,只能猜测。调试时应避免猜测。 – hakre 2012-07-26 15:59:47

回答

2

您将要覆盖$ x和$ y的变量在这里:

$x = $x[$key]; 
$y = $y[$key]; 

这里需要使用不同的变量名。

+0

非常感谢!完全错过了。 – Lazze 2012-07-26 16:15:23