我有下面的PHP方法,在新用户第一次注册时,在他们上传自己的配置文件之前,会生成一个配置文件映像。它所做的只是为它们创建一个颜色鲜艳的正方形——当显示没有个人资料照片的用户列表时,这会让界面看起来更有趣。
我如何调整这种方法,使其创建一个随机颜色的棋盘?类似于以下内容:http://krazydad.com/bestiary/thumbs/random_pixels.jpg
public function generate_random_image($filename, $w = 200, $h = 200, $chosen_color = NULL) {
if(!$chosen_color) {
$color_options = Array("#6f0247", "#FF0569", "#FFF478", "#BAFFC0", "#27DB2D", "#380470", "#9D69D6");
$random = rand(0,sizeof($color_options));
$chosen_color = $color_options[$random];
}
$rgb = self::hex2rgb($chosen_color);
$image = imagecreatetruecolor($w, $h);
for($row = 1; $row <= $h; $row++) {
for($column = 1; $column <= $w; $column++) {
$color = imagecolorallocate ($image, $rgb[0] , $rgb[1], $rgb[2]);
imagesetpixel($image,$column - 1 , $row - 1, $color);
}
$row_count++;
}
$filename = APP_PATH.$filename;
imagepng($image, $filename);
return $chosen_color;
}
不如直接更改
$color = imagecolorallocate ($image, $rgb[0] , $rgb[1], $rgb[2]);
至
$color = imagecolorallocate ($image, rand(0,255), rand(0,255), rand(0,255));
然后,每个像素都有自己的颜色。只需绘制一张小图像,然后按200%或300%(或其他任意数字)缩放,你就会得到漂亮、大块的像素,就像你链接的图像一样。
在$row
s和$column
s中迭代时,应将步长增加到所需的像素大小,并在每次迭代中选择另一种颜色。
像素宽度=20x20:的示例
$pixel = 20;
for($row = 0; $row <= $h / $pixel; $row++) {
for($column = 0; $column <= $w/ $pixel; $column++) {
$rgb = self::hex2rgb($color_options[rand(0,sizeof($color_options))]);
$color = imagecolorallocate ($image, $rgb[0] , $rgb[1], $rgb[2]);
imagefilledrectangle(
$image,
$column*$pixel,
$row*pixel,
$column*$pixel+$pixel,
$row*pixel+$pixel,
$color
);
}
}