1

私は少し問題があるこの画像関数を持っています

function BuildCustomBricks($myBricksAndRatios) {

        $img = imagecreate(890,502);
        imagealphablending($img, true);
        imagesavealpha($img, true);

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

            $bricks_to_choose = rand(1,10);

            $cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key."-".$bricks_to_choose.".png"); 
            imagealphablending($cur, true);
            imagesavealpha($cur, true);
            imagecopy($img, $cur, 0, 0, 0, 0, 125, 32);

            imagedestroy($cur);
        }

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

前の画像から各画像を foreach 100 ピクセルに配置するにはどうすればよいですか?

next image in the loop:    
imagecopy($img, $cur, previous_x_coord+100, 0, 0, 0, 125, 32);
4

2 に答える 2

1

マイケルの答えはオプションですが、foreach代わりに をwhile使用しているため、配列のインデックスも使用できます。

foreach ($this->shuffle_with_keys($myBricksAndRatios) as $factor => $key)
{
    //...Multiply index by 100: 0*100,1*100,2*100 etc...
    imagecopy($img, $cur, 100*$factor, 0, 0, 0, 125, 32);
    //...
}

それは私の少し肛門ですが、2 行のコードを追加する必要はなく、追加の変数も必要ありません。批評家は、このコードは保守性が低いと言うかもしれません。その場合、私は次のように言います。

警告:
Michael が指摘したように、このコードは明らかな理由で連想配列では機能しません ( 'First_Key'*100 === ?)

于 2012-09-14T12:45:14.083 に答える
1

ゼロから始まり、すべてのループ反復の終わりに 100 を追加する変数を保存するだけです。

    // Init at zero
    $coords = 0;
    foreach ($this->shuffle_with_keys($myBricksAndRatios) as $key) {            


        $bricks_to_choose = rand(1,10);

        $cur = imagecreatefrompng("/var/www/brickmixer/bricks/". $key."-".$bricks_to_choose.".png"); 
        imagealphablending($cur, true);
        imagesavealpha($cur, true);
        // Use the variable here
        imagecopy($img, $cur, $coords, 0, 0, 0, 125, 32);

        imagedestroy($cur);

        // Add 100 at the end of the loop block
        $coords += 100;
    }
于 2012-09-14T12:42:20.573 に答える