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

The imagefilledarc() function is a built-in PHP function used to draw and fill a partial arc. This function requires the GD extension and is commonly used for creating pie charts, progress indicators, and other graphical elements.

Note: To use imagefilledarc(), you need the GD extension enabled in PHP. Install it using: sudo apt-get install php-gd on Ubuntu/Debian or enable it in php.ini on Windows.

Syntax

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

Parameters

The imagefilledarc() function takes nine parameters −

  • $image − Image resource created by imagecreate() or imagecreatetruecolor()

  • $cx − X-coordinate of the arc center

  • $cy − Y-coordinate of the arc center

  • $width − Arc width in pixels

  • $height − Arc height in pixels

  • $start − Start angle in degrees (0° is at 3 o'clock position)

  • $end − End angle in degrees (drawn clockwise)

  • $color − Color identifier created with imagecolorallocate()

  • $style − Drawing style, can be one of the following −

    • IMG_ARC_PIE − Creates a pie slice with rounded edges

    • IMG_ARC_CHORD − Connects start and end points with a straight line

    • IMG_ARC_NOFILL − Draws outline only, no fill

    • IMG_ARC_EDGED − Connects start/end angles to center (used with NOFILL)

Return Value

Returns true on success or false on failure.

Example 1: Basic Pie Arc

This example creates a simple pie arc with a gray border −

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

    // Create 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);

    // Output image
    header("Content-Type: image/png");
    imagepng($image);
    imagedestroy($image);
?>

Example 2: 3D Effect Arc

This example demonstrates creating a 3D-style arc using multiple layers −

<?php
    // Create image
    $image = imagecreatetruecolor(700, 300);
    
    // Allocate colors
    $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
    $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);

    // Create 3D effect by drawing multiple layers
    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);

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

Arc Style Comparison

Style Description Fill
IMG_ARC_PIE Rounded pie slice Yes
IMG_ARC_CHORD Straight line connection Yes
IMG_ARC_NOFILL Outline only No
IMG_ARC_EDGED With center lines No (use with NOFILL)

Conclusion

The imagefilledarc() function is powerful for creating graphical elements like pie charts and progress indicators. Remember that angles are measured clockwise from the 3 o'clock position, and always call imagedestroy() to free memory after image processing.

Updated on: 2026-03-15T09:46:31+05:30

454 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements