How to draw a filled polygon using the imagefilledpolygon() function in PHP?

王林
Release: 2023-09-14 17:46:02
forward
977 people have browsed it

imagefilledpolygon() is a built-in PHP function for drawing filled polygons.

Syntax

bool imagefilledpolygon($image, $points, $num_points, $color)
Copy after login

Parameters

imagefilledpolygon()Takes four different parameters - $image, $ points, $ num_points and $color.

  • $Image - Use the imagecreatetruecolor() function to create a blank image of the given dimensions.

  • $points - Saves consecutive vertices of a polygon.

  • $num_points - Contains the total number of vertices in the polygon. The total number of points/vertices must be at least three to create a polygon.

  • $color - Contains color identifiers populated using the imagecolorallocate() function.

Return value

Returns True on success and False on failure.

Example 1

<?php
   // set up array of points for a polygon
   $values = array(
      40, 50, // Point 1 (x, y)
      20, 240, // Point 2 (x, y)
      60, 60, // Point 3 (x, y)
      240, 20, // Point 4 (x, y)
      50, 40, // Point 5 (x, y)
      10, 10 // Point 6 (x, y)
   );
   // create the image using imagecreatetruecolor function
   $img = imagecreatetruecolor(700, 350);

   // allocated the blue and gray colors
   $bg = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // filled the background
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // draw a polygon
   imagefilledpolygon($img, $values, 6, $blue);

   // flush image
   header(&#39;Content-type: image/png&#39;);
   imagepng($img);
   imagedestroy($img);
?>
Copy after login

Output

How to draw a filled polygon using the imagefilledpolygon() function in PHP?

Example 2

<?php
   // Set the vertices of the polygon
   $values = array(
      150, 50, // Point 1 (x, y)
      55, 119, // Point 2 (x, y)
      91, 231, // Point 3 (x, y)
      209, 231, // Point 4 (x, y)
      245, 119 // Point 5 (x, y)
   );
   // It creates the size of the image or blank image.
   $img = imagecreatetruecolor(700, 350);

   // Set the gray background image color
   $bg = imagecolorallocate($img, 122, 122, 122);

   // Set the red image color
   $red = imagecolorallocate($img, 255, 0, 0);

   // fill the background
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // Draw the polygon image
   imagefilledpolygon($img, $values, 5, $red);

   // Output of the image.
   header(&#39;Content-type: image/png&#39;);
   imagepng($img);
   imagedestroy($img);
?>
Copy after login

Output

How to draw a filled polygon using the imagefilledpolygon() function in PHP?

The above is the detailed content of How to draw a filled polygon using the imagefilledpolygon() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template