note 49813 added to function.imagefilledellipse
| From: | richard at mf2fm dot co dot uk | Date: | Wed, 09 Feb 2005 10:25:18 +0000 |
| Subject: | note 49813 added to function.imagefilledellipse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84708@lists.php.net to get a copy of this message | ||
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);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagefilledellipse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49813
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49813&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49813&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49813&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49813&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49813&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49813&report=yes
Search -- http://master.php.net/manage/user-notes.php