下面的简单代码在给定的图像上生成点(这也是动态生成的),但由于未知的原因,我没有看到它们…
for ($i = 0; $i < $max; $i++)
{
$xcoords = mt_rand(50, 450);
$ycoords = mt_rand(50, 450);
if ($ycoords > 300)
{
drawMe($red);
}
elseif ($ycoords > 150 && $ycoords < 300)
{
drawMe($green);
}
else
{
drawMe($blue);
}
这是使用颜色的代码:
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 128, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
这是drawMe()函数:
function drawMe($color)
{
ob_start();
imagesetpixel($im, $xcoords, $ycoords - 1, $color);
imagesetpixel($im, $xcoords + 1, $ycoords - 1, $color);
imagesetpixel($im, $xcoords + 1, $ycoords, $color);
imagesetpixel($im, $xcoords + 1, $ycoords + 1, $color);
imagesetpixel($im, $xcoords, $ycoords, $color); // main dot !!!
imagesetpixel($im, $xcoords, $ycoords + 1, $color);
imagesetpixel($im, $xcoords - 1, $ycoords + 1, $color);
imagesetpixel($im, $xcoords - 1, $ycoords, $color);
imagesetpixel($im, $xcoords - 1, $ycoords - 1, $color);
return ob_get_clean();
}
如果我删除ob_start和ob_get_clean,我得到警告:
Warning: imagesetpixel() expects parameter 1 to be resource, null given in /var/www/test.php on line 75 to 83
这是$im和背景:
$im = imagecreate(640, 480);
$bg = imagecolorallocate($im, 255, 255, 255);
您需要在开始
之前将新的$im定义为新图像你可以使用imagick类
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');