這段程式碼會產生一個 RGB 立方體(有無邊框皆可)。因為它只渲染可見像素,所以速度明顯很快(大約 1 到 2 秒)。透過更改 $order 變數,您可以從不同角度觀看立方體。輸入雙重或三重值(例如 rrg 或 ggg)將提供您單一通道的其他規格。如有任何建議,請寄信至我的電子郵件信箱。
<?php
$borders = true;
$order = 'rgb';
set_time_limit(0);
$img = imageCreateTrueColor(510, 510);
$bg = imageColorAllocate($img, 255, 255, 255);
$black = imageColorAllocate($img, 255, 255, 255);
for ($r=0; $r<256; $r++) {
for ($g=0; $g<256; $g++) {
for ($b=0; $b<256; $b++) {
$rN = ${$order{0}};
$gN = ${$order{1}};
$bN = ${$order{2}};
$col = imageColorAllocate($img, $rN, $gN, $bN);
imagesetpixel($img, $b+($r*0.5)+(255/4), $g+($r*0.5)+(255/4), $col);
if ($r < 255 && $g > 0) break;
}
}
if ($borders) {
imagesetpixel($img, ($r*0.5+(255/4)), ($r*0.5)+(255/4), $black);
imagesetpixel($img, ($r*0.5)+255+(255/4), ($r*0.5)+(255/4), $black);
imagesetpixel($img, ($r*0.5)+(255/4), ($r*0.5)+255+(255/4), $black);
}
}
if ($borders) {
imageline($img, 255/4, 255/4, 255+(255/4), 255/4, $black);
imageline($img, 255/4, 255/4, 255/4, 255+(255/4), $black);
imageline($img, 255*0.5+(255/4), 255*0.5+(255/4), 255*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), $black);
imageline($img, 255*0.5+(255/4), 255*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), 255*0.5+(255/4), $black);
imageline($img, 255*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), $black);
imageline($img, 255*0.5 + 509*0.5+(255/4), 255*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), 255*0.5 + 509*0.5+(255/4), $black);
}
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
?>