0

我有这个 foreach 循环

foreach ($this->shuffle_with_keys($myBricksAndRatios) as $key) {         

   $bricks_to_choose = mt_rand(1,1);

   $cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key[0]."".$bricks_to_choose.".png"); 
   imagecopy($img, $cur, $coords, 0, 0, 0, 125, 30); 

   $coords += 132;
}

如何为每 10、每 20、每 30 等图像设置新坐标?

它应该从 x = 0 和 y = 0 开始在一行中输出 10 个图像,然后第 11 个图像将从 x = 0 和 y = 37 开始

然后将第 21 张图像放置在 x = 0、y = 74 等处。

4

2 回答 2

1

应该很简单。下面的代码可能不是最优化的,但很容易理解。

$dx = 132;
$dy = 37;
$x = 0;
$y = 0;
$perrow = 20;
$cnt = 1;

foreach ($this->shuffle_with_keys($myBricksAndRatios) as $key) {         

    $bricks_to_choose = mt_rand(1,1);

    $cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key[0]."".$bricks_to_choose.".png"); 
    imagecopy($img, $cur, $x, $y, 0, 0, 125, 30);

    $x += dx;

    if(++$cnt % $perrow == 0) {
        $x = 0;
        $y += dy;
    }
}
于 2012-09-17T10:56:44.717 回答
0
$i = 0;
foreach ($this->shuffle_with_keys($myBricksAndRatios) as $key) {         

   $bricks_to_choose = mt_rand(1,1);

   $ycords = (floor(($i++) / 10) * 37);

   $cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key[0]."".$bricks_to_choose.".png"); 
   imagecopy($img, $cur, $coords, $ycords, 0, 0, 125, 30); 

   $coords += 132;
}

这应该有效。

注意$ycords = (floor(($i++) / 10) * 37);线。

编辑:当然,如果您不想要任何 X 定位,请更改$coords0

于 2012-09-17T10:54:41.187 回答