search for in the  
<imagefilledarcimagefilledpolygon>
Last updated: Thu, 19 May 2005

imagefilledellipse

(PHP 4 >= 4.0.6, PHP 5)

imagefilledellipse -- Draw a filled ellipse

Description

bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )

imagefilledellipse() draws an ellipse centered at cx, cy (top left is 0, 0) in the image represented by image. W and h specifies the ellipse's width and height respectively. The ellipse is filled using color. Returns TRUE on success or FALSE on failure.

Note: This function was added in PHP 4.0.6 and requires GD 2.0.1 or later

Example 1. imagefilledellipse() example

<?php

// create a blank image
$image = imagecreate(400, 300);

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

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

// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);

// output the picture
header("Content-type: image/png");
imagepng($image);

?>

Note: This function requires GD 2.0.1 or later.

See also imageellipse() and imagefilledarc().



User Contributed Notes
imagefilledellipse
richard at mf2fm dot co dot uk
09-Feb-2005 04:25
Here is a piece of code using imagefilledellipse which creates a simulation of the current phase of the moon...

Usage is <img src="moon.php?size=100"> which produces an image 100px by 100px wide.  If size is left out, the default is 24px by 24px.

<?php

$mps
=2551442.8; // moon phase in seconds (29 days, 12 hours, 44 mins, 2.8 secs)
$position=time()-mktime(10, 32, 0, 1, 25, 2005); // seconds since full moon at 10:32GMT on 25 Jan 2005
$position=($position-$mps*intval($position/$mps))/$mps; // phase from 0 to 1

$position=2*(0.5-$position);
## revised to produce easier to work with...
## $position=1 - full moon
## $position=0 - new moon
## $position=-1 - full moon

$size=$_GET['size'];
if (!
is_numeric($size)) $size=24; // width/height in pixels
$moon=imagecreate($size, $size);
$dark=imagecolorallocate($moon, 0, 34, 68); // background colour for moon
$light=imagecolorallocate($moon, 238, 238, 255); // foreground colour for moon
$corona=imagecolorallocatealpha($moon, 153, 153, 153, 64); // edge of moon (semi-transparent)

##
## Make transparent background
##
$background=imagecolorallocatealpha($moon, 0, 0, 0, 127);
imagefill($moon, 0, 0, $background);

##
## Make the moon!
##
imagefilledellipse($moon, round($size/2), round($size/2), $size, $size, $corona);
if (
$position>-1/$size AND $position<1/$size) imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $dark); // new moon
elseif (abs($position)>1-1/$size) imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $light); // full moon
elseif ($position>0) {
  
imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $light);
   for (
$i=0; $i<$size-2; $i++) {
      
$xpos=($size-2)/2;
      
$xpos=1-($i/$xpos);
      
$xpos=sqrt(1-($xpos*$xpos));
      
$xpos=($size/2)+($position-0.5)*$xpos*($size-2);
      
imagesetpixel($moon, round($xpos), $i+1, $dark);
   }
   for (
$i=0; $i<$size; $i++) {
      
$set=0;
       for (
$j=0; $j<$size; $j++) {
           if (!
$set AND imagecolorat($moon, $j, $i)==$dark) $set=1;
           elseif (
$set AND imagecolorat($moon, $j, $i)==$light) imagesetpixel($moon, $j, $i, $dark);
       }
   }
}
else {
  
imagefilledellipse($moon, round($size/2), round($size/2), $size-2, $size-2, $dark);
   for (
$i=0; $i<$size-2; $i++) {
      
$xpos=($size-2)/2;
      
$xpos=1-($i/$xpos);
      
$xpos=sqrt(1-($xpos*$xpos));
      
$xpos=($size/2)+($position+0.5)*$xpos*($size-2);
      
imagesetpixel($moon, round($xpos), $i+1, $light);
   }
   for (
$i=0; $i<$size; $i++) {
      
$set=0;
       for (
$j=0; $j<$size; $j++) {
           if (!
$set AND imagecolorat($moon, $j, $i)==$light) $set=1;
           elseif (
$set AND imagecolorat($moon, $j, $i)==$dark) imagesetpixel($moon, $j, $i, $light);
       }
   }
}

##
## And output the picture
##
header ("Content-Type: image/png");
imagepng($moon);
imagedestroy($moon);
?>
dave at corecommunications dot us
10-Oct-2002 08:56
I needed to draw translucent circles on an existing image, using imagealphablending($image,true);. Turns out that imagefilledellipse seems to do the ellipse by drawing a series of lines from the centroid out to the circumference. Problem with this is that pixels near the center very visibly get drawn multiple times, producing a kind of moire effect. Also, the 0-degree (center to right) line is drawn twice and so is twice as dark the pixel below it.

My workaround was to draw the ellipse solid in a copy of the source image and do an imagecopymerge back into the original.
ivank at 2xtreme dot net
30-Mar-2002 06:31
Sometimes, you have x1, y1, x2, y2 parameters, but not the center, width, and height. Use this to draw a filled ellipse from your x1, y1, x2, and y2:

ImageFilledEllipse(
$im,
($x1 + round(($x2 - $x1) / 2)),
($y1 + round(($y2 - $y1) / 2)),
($x2 - $x1),
($y2 - $y1),
$color);
harry dot wood at ic dot ac dot uk
19-Jun-2001 06:09
This draws a rotated ellipse. If you don't want filled ellipses, then you don't need the triangle function.

function triangle($x1,$y1, $x2,$y2, $x3,$y3, $colour) {
   global $im;
   $coords = array($x1,$y1, $x2,$y2, $x3,$y3);
   imagefilledpolygon($im, $coords, 3, $colour);
}

function rotatedellipse($cx, $cy, $width, $height, $rotateangle, $colour, $filled=true) {
   global $im;
   $step=15;
   $cosangle=cos(deg2rad($rotateangle));
   $sinangle=sin(deg2rad($rotateangle));

   $squishratio = $height/$width;
   $nopreviouspoint = true;
   for ($angle=0; $angle<=(180+$step); $angle+=$step) {
      
     $ox = ($width * cos(deg2rad($angle)));
     $oy = ($width * sin(deg2rad($angle))) * $squishratio;

     $x = ($ox * $cosangle) - ($oy * $sinangle);
     $y = ($ox * $sinangle) + ($oy * $cosangle);
 
     if ($nopreviouspoint) {
       $px=$x;
       $py=$y;
       $nopreviouspoint=false;
     }

     if ($filled) {
         triangle($cx, $cy, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
         triangle($cx, $cy, $cx-$px, $cy-$py, $cx-$x, $cy-$y, $colour);
     } else {
         imageline($im, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour);
         imageline($im, $cx-$px, $cy-$py, $cx-$x, $cy-$y, $colour);
     }
     $px=$x;
     $py=$y;
   }
}

<imagefilledarcimagefilledpolygon>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT