Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Hàm imagepolygon() trong PHP

imagepolygon() vẽ đa giác (polygon) trên ảnh GD từ mảng tọa độ đỉnh.

Cú pháp

imagepolygon(resource $image, array $points, int $num_points, int $color): bool

Tham số

  • points: Mảng tọa độ [x1, y1, x2, y2, ...].
  • num_points: Số đỉnh (count($points)/2).
  • color: Color identifier.

Ví dụ: Vẽ tam giác

<?php
$img = imagecreatetruecolor(400, 300);
$color = imagecolorallocate($img, 120, 160, 190);
$bg = imagecolorallocate($img, 10, 100, 50);
imagefill($img, 0, 0, $bg);
imagepolygon($img, [50,50, 150,200, 340,200], 3, $color);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>