0

次の単純なコードは、特定の画像 (これも動的に生成されます) にドットを生成しますが、不明な理由でそれらが表示されません ...

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);
4

1 に答える 1

0

開始する前に、新しい $im を新しいイメージとして定義する必要があります

imagickクラスを使用できます

$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
于 2013-03-14T12:47:26.473 に答える