How to draw an ellipse using imageellipse() function in PHP?

The imageellipse() function is an inbuilt PHP function used to draw an ellipse on an image resource. It returns True on success and False on failure.

Syntax

bool imageellipse($image, $cx, $cy, $width, $height, $color)

Parameters

The imageellipse() function takes six parameters −

  • $image − An image resource created by 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 identifier created by imagecolorallocate() function.

Return Value

Returns True on success or False on failure.

Example 1

Creating a white ellipse on a black background ?

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

   // Set the background color to black
   $bg = imagecolorallocate($image, 0, 0, 0);

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

   // Set ellipse color to white
   $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);
   
   // Clean up memory
   imagedestroy($image);
?>

Example 2

Creating a cyan ellipse on a gray background ?

<?php
   // Create the image canvas
   $image = imagecreatetruecolor(700, 600);

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

   // Fill background
   imagefill($image, 0, 0, $bg);

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

   // Draw the ellipse (more vertical than horizontal)
   imageellipse($image, 250, 300, 300, 550, $col_ellipse);

   // Output as PNG image
   header("Content-type: image/png");
   imagepng($image);
   
   // Free memory
   imagedestroy($image);
?>

Note: To run these examples, you need PHP with GD extension enabled on a web server. The examples will output image data directly to the browser.

Conclusion

The imageellipse() function is essential for creating elliptical shapes in PHP graphics. Remember to always use imagedestroy() to free memory after generating images.

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

847 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements