imagearc() function in PHP

The imagearc() function in PHP is used to draw arcs and circles on images. It's part of the GD library and allows you to create curved lines with specified angles and dimensions.

Syntax

imagearc($img, $cx, $cy, $width, $height, $start, $end, $color)

Parameters

  • $img − Image resource created with imagecreatetruecolor() or similar functions.

  • $cx − x-coordinate of the arc's center.

  • $cy − y-coordinate of the arc's center.

  • $width − Width of the ellipse containing the arc.

  • $height − Height of the ellipse containing the arc.

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

  • $end − Ending angle in degrees.

  • $color − Color resource allocated with imagecolorallocate().

Return Value

Returns TRUE on success or FALSE on failure.

Example 1: Basic Arc Drawing

Here's how to create a simple arc with different colors ?

<?php
    $img = imagecreatetruecolor(200, 200);
    $one = imagecolorallocate($img, 100, 50, 255);
    $two = imagecolorallocate($img, 30, 255, 150);
    
    // Full circle
    imagearc($img, 100, 100, 200, 200, 0, 360, $one);
    // Partial arc
    imagearc($img, 130, 50, 100, 150, 25, 155, $two);
    
    header("Content-type: image/png");
    imagepng($img);
    imagedestroy($img);
?>

Example 2: Different Arc Configurations

This example demonstrates arcs with varying coordinates and angles ?

<?php
    $img = imagecreatetruecolor(250, 250);
    $one = imagecolorallocate($img, 100, 90, 255);
    $two = imagecolorallocate($img, 100, 255, 190);
    
    // Full circle at different position
    imagearc($img, 130, 100, 200, 200, 0, 360, $one);
    // Quarter arc
    imagearc($img, 140, 50, 140, 150, 95, 155, $two);
    
    header("Content-type: image/png");
    imagepng($img);
    imagedestroy($img);
?>

Key Points

  • Angles are measured in degrees, starting from 3 o'clock position (0°)

  • Use 0° to 360° for complete circles

  • Width and height determine the ellipse shape containing the arc

  • Always call imagedestroy() to free memory

Conclusion

The imagearc() function is essential for drawing curved elements in PHP image manipulation. It provides flexible control over arc position, size, and angle ranges for creating various graphical elements.

Updated on: 2026-03-15T08:05:03+05:30

402 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements