Current Location: Home> Latest Articles> How to Draw an Ellipse Using the imageellipse() Function in PHP

How to Draw an Ellipse Using the imageellipse() Function in PHP

M66 2025-06-21

How to Draw an Ellipse Using the imageellipse() Function in PHP

imageellipse()

Parameters

The imageellipse() function accepts six parameters, which are:

  • $image - The image resource, which is returned by one of the image creation functions like imagecreatetruecolor().
  • $cx - The x-coordinate of the ellipse center.
  • $cy - The y-coordinate of the ellipse center.
  • $width - The width of the ellipse.
  • $height - The height of the ellipse.
  • $color - The color of the ellipse, typically obtained by the imagecolorallocate() function.

Return Value

The function returns true on success, or false on failure.

Example 1


<?php
// Create a blank image
$image = imagecreatetruecolor(700, 350);

// Select the background color
$bg = imagecolorallocate($image, 0, 0, 0);

// Fill the background with the selected color
imagefill($image, 0, 0, $bg);

// Choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);

// Draw the ellipse
imageellipse($image, 325, 175, 500, 175, $col_ellipse);

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

Output

Example 2


<?php
// Create a blank image
$image = imagecreatetruecolor(700, 600);

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

// Fill the background with the selected color
imagefill($image, 0, 0, $bg);

// Set the color of the ellipse
$col_ellipse = imagecolorallocate($image, 0, 255, 255);

// Draw the ellipse
imageellipse($image, 250, 300, 300, 550, $col_ellipse);

// Output the image
header("Content-type: image/gif");
imagepng($image);
?>

Output

The above examples show how to use the imageellipse() function in PHP to draw ellipses. This function is useful for various image drawing tasks and can be applied to web development or image processing projects.