我正在嘗試創建背景由兩種顏色的正方形組成的 PNG 影像,正方形在圖片中水平或垂直重復。

這是我的代碼:
<?php
$maxwert = 300;
$size = 20;
$img = imagecreatetruecolor($maxwert, $maxwert);
imagecolorallocate($img, 0, 0, 0);
for($y=0;$y<$maxwert;$y = $size){
for($x=0;$x<$maxwert;$x =$size){
$r = rand(0,255);
$g = rand(0,255);
$b = rand(0,255);
$color = imagecolorallocate($img, $r, $g, $b);
imagefilledrectangle ($img, $x, $y, $x $size ,$y $size, $color);
}
}
// Save the image
imagepng($img, 'imagefilledrectangle.png');
imagedestroy($img);
?>
我的輸出:

uj5u.com熱心網友回復:
在回圈外創建兩種顏色,然后回圈一種然后另一種
<?php
$maxwert = 300;
$size = 20;
$img = imagecreatetruecolor($maxwert, $maxwert);
$colors = [
imagecolorallocate($img, rand(0,255), rand(0,255) rand(0,255)),
imagecolorallocate($img, rand(0,255), rand(0,255) rand(0,255))
];
for($y=0;$y<$maxwert;$y = $size){
for($x=0;$x<$maxwert;$x =$size){
imagefilledrectangle ($img, $x, $y, $x $size ,$y $size, $colors[(($x/$size)%2 ($y/$size))%2]);
}
}
// Save the image
imagepng($img, 'imagefilledrectangle.png');
imagedestroy($img);
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358841.html
上一篇:將頁面URL視為影像URL
