update page now

Voting

: min(nine, three)?
(Example: nine)

The Note You're Voting On

richard at mf2fm dot co dot uk
20 years ago
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);
?>

<< Back to user notes page

To Top