在PHP中的图像的每个角上绘制正方形

时间:2015-09-09 14:27:35

标签: php

我想在图像的每个角上画一个正方形,目前我在图像的左上角画了一个正方形。

$canvas = imagecreatetruecolor(200, 200);
//rectangle coordinates
$array_rectangles = array(array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)),array('x1' => mt_rand(0, 200),'y1' => mt_rand(0, 200),'x2' => mt_rand(0, 200),'y2' => mt_rand(0, 200)));
$white = imagecolorallocate($canvas, 255, 255, 255);
$background_color = imagecolorallocate($canvas, 25, 153, 206);
//creating ten rectangles.
foreach ($array_rectangles as $key => $point) {
imagefilledrectangle($canvas, $point['x1'], $point['y1'], $point['x2'], $point['y2'], $white);
} 
//creating square
imagefilledrectangle($canvas, 0, 0, 20, 20, $green);
imagepng($canvas,'img.png'); //saving image

enter image description here

我已经尝试了imagefilledrectangle($canvas, 200, 0, 20, 20, $green);,我得到了以下结果。

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你必须尝试:

imagefilledrectangle($canvas, 180, 0, 200, 20, $green);
imagefilledrectangle($canvas, 0, 180, 20, 200, $green);
imagefilledrectangle($canvas, 180, 180, 200, 200, $green);

x1必须小于x2,否则将向后打印。 y1和y2相同