在php中绘制矩形不起作用

时间:2016-03-05 02:40:16

标签: php image drawing

我在php中创建一个图像,并用填充了颜色的10x10像素矩形填充它。

$image = imagecreate(150,150);
$background = imagecolorallocate($image, 0, 0, 0); //black background
for ($row=0; $row < 15; $row++) { 
    for ($col=0; $col < 15; $col++) { 
        $x1 = 10* $col;
        $y1 = 10*$row;
        $x2 = 10*($col + 1);
        $y2 = 10*($row + 1);

        imagefilledrectangle($image, $x1,$y1,$x2,$y2, imagecolorallocate($image, 100,100,100)); //grey rectangle
    }
}

imagepng($image, "ciph.png");

这适用于不大于150x150像素的小图像,我得到一个完全灰色的矩形。但是当我尝试更大的图像时它只为图像的一部分添加了矩形。知道是什么导致了这个吗?似乎我可以绘制的单个对象的数量有限制。

enter image description here

15×15

enter image description here

18×18

我已经计算了,它似乎只绘制了256个矩形...看起来似乎是2到8次幂的巧合。

任何帮助将不胜感激!感谢。

1 个答案:

答案 0 :(得分:1)

问题在于你是如何创建图像的。如果您更改第一行:

$image = imagecreate(150,150);

到:

$image = imagecreatetruecolor(150,150);

它将允许将超过256个矩形绘制到图像中。

imagecreatetruecolor()默认情况下也为图片提供黑色背景,而不是imagecreate()给出的空白背景,因此您也不需要第二行。

相关问题