<?php
// 创建画布
$imageWidth = 400;
$imageHeight = 400;
$image = imagecreatetruecolor($imageWidth, $imageHeight);
// 创建随机背景颜色
$bgColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imagefill($image, 0, 0, $bgColor);
// 创建随机形状和颜色
$shapeTypes = ['rectangle', 'ellipse'];
$shapeType = $shapeTypes[array_rand($shapeTypes)];
$shapeColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
// 在画布上绘制形状
if ($shapeType === 'rectangle') {
$x1 = rand(0, $imageWidth / 2);
$y1 = rand(0, $imageHeight / 2);
$x2 = rand($imageWidth / 2, $imageWidth);
$y2 = rand($imageHeight / 2, $imageHeight);
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $shapeColor);
} else {
$cx = rand(0, $imageWidth);
$cy = rand(0, $imageHeight);
$width = rand(50, 150);
$height = rand(50, 150);
imagefilledellipse($image, $cx, $cy, $width, $height, $shapeColor);
}
// 生成 PNG 图像并保存
$imageFileName = 'random_shape.png';
imagepng($image, $imageFileName);
// 销毁画布资源
imagedestroy($image);
echo "随机形状已生成并保存为 $imageFileName 文件。";
?>