How to draw a partial arc and fill it using imagefilledarc() function in PHP?


imagefilledarc() is an inbuilt function in PHP that is used to draw a partial arc and fill it.

Syntax

bool imagefilledarc($image, $cx, $cy, $width, $height, $start, $end, $color, $style)

Parameters

imagefilledarc() takes nine parameters: $image, $cx, $cy, $width, $height, $start, $end, $color, and $style.

  • $image − It is returned by the image creation function imagecreatetruecolor(). This function is used to create the size of the image.

  • $cx − Sets the x-coordinate of the center.

  • $cy − Sets the y-coordinate of the center.

  • $width − Sets the arc width.

  • $height − Sets the arc height.

  • $start − Start angle in degrees.

  • $end − Arc end angle, in degrees. 00 is located at the three o'clock position, and the arc is drawn clockwise.

  • $color − It is a color identifier created with imagecolorallocate() function.

  • $style − Suggests how to fill the image and its values can be anyone from the following list −

    • IMG_ARC_PIE

    • IMG_ARC_CHORD

    • IMG_ARC_NOFILL

    • IMG_ARC_EDGED

Both IMG_ARC_PIE and IMG_ARC_CHORD are mutually exclusive.

IMG_ARC_CHORD connects a straight line from the starting and ending angles, while IMG_ARC_PIE produces a rounded edge.

IMG_ARC_NOFILL indicates that the arc or chord should be outlined, not filled.

IMG_ARC_EDGED is used together with the IMG_ARC_NOFILL, indicates that the beginning and ending angles should be connected to the center.

Return Values

It returns True on success and False on failure.

Example 1

<?php
   define("WIDTH", 700);
   define("HEIGHT", 550);

   // Create the image.
   $image = imagecreate(WIDTH, HEIGHT);

   // Allocate colors.
   $bg = $white = imagecolorallocate($image, 0x00, 0x00, 0x80);
   $gray = imagecolorallocate($image, 122, 122, 122);

   // make pie arc.
   $center_x = (int)WIDTH/2;
   $center_y = (int)HEIGHT/2;
   imagerectangle($image, 0, 0, WIDTH-2, HEIGHT-2, $gray);
   imagefilledarc($image, $center_x, $center_y, WIDTH/2,
   HEIGHT/2, 0, 220, $gray, IMG_ARC_PIE);

   // Flush image.
   header("Content-Type: image/gif");
   imagepng($image);
?>

Output

Example 2

<?php
   // Created the image using imagecreatetruecolor function.
   $image = imagecreatetruecolor(700, 300);
   
   // Allocated the darkgray and darkred colors
   $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
   $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);

   // Make the 3D effect
   for ($i = 60; $i > 50; $i--) {
      imagefilledarc($image, 100, $i, 200, 100, 75, 360, $darkred, IMG_ARC_PIE);
   }
   imagefilledarc($image, 100, $i, 200, 100, 45, 75 , $darkgray, IMG_ARC_PIE);

   // flush image
   header('Content-type: image/gif');
   imagepng($image);
   imagedestroy($image);
?>

Output

Updated on: 09-Aug-2021

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements