Jump to content

How to show a different image or percentage of the moon based upon the phrase?


MrTIMarshall

Recommended Posts

Hello,

 

I have found a couple of PHP Scripts which can calculate, not only the moons phrase, but the luminosity as well, snippets such as this one: https://github.com/solarissmoke/php-moon-phase/blob/master/moon-phase.php

 

However I do not know how to echo any of this code and what would be the best way to show a different phrase, my best bet, although I do not know how this script works, is to have different images, or maybe just two and one masks the other, however I am unsure about luminosity, how I could show this correctly.

 

Thank you for any help and/or advice in advanced.

 

Best Regards,

Tim

Link to comment
Share on other sites

This code below is a prototype for drawing moon phases. It needs more work to pick up values from your class and waxing and waning moons. I have attached an HTML file to test the images produced

 

moon.php

$phase = $_GET['p'];

$im = imagecreatetruecolor(200,200);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

$cx = $cy = 100;
$r = imagesx($im)/2;

imageantialias($im, 1);
imagefill($im, 0, 0, $black);
imagefilledellipse($im, $cx, $cy, 2*$r, 2*$r, $white);
imageellipse($im, $cx, $cy, 2*$r, 2*$r, $black);
imagecolortransparent($im, $black);

for ($y = $cy-$r; $y <= $cy+$r; $y++) {
    $theta = asin(($cy-$y)/$r);
    $x1 = $cx - $r*cos($theta);
    $x2 = $x1 + 2 * $phase * $r * cos($theta);
    imageline($im, $x1, $y, $x2, $y, $black);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);

 

moonSample.html

Link to comment
Share on other sites

Result on my page:

PNG IHDR":9tRNSnIDATx nH@`՗IENDB`

 

I changed the $phase variable to $phase = $age;, however I am not sure if I need to change the variable name in your code as it is already used. This is what I was thinking:

<img src="image/moonphrase/<?php echo "$age"; ?>.png" />

 

However I was failing to understand how I could use the distance to show the size or the luminosity and whatnot.

 

Here is what I have so: https://tornhq.com/Top_Menu/moonphrase.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
/**
 * Moon phase calculation class
 * Adapted for PHP from Moontool for Windows (http://www.fourmilab.ch/moontoolw/)
 * by Samir Shah (http://rayofsolaris.net)
 * Last modified August 2012
 **/

class MoonPhase {
	private $timestamp;
	private $phase;
	private $illum;
	private $age;
	private $dist;
	private $angdia;
	private $sundist;
	private $sunangdia;

	private $synmonth;

	private $quarters = null;

	function __construct( $pdate = null ) {
		if( is_null( $pdate ) )
			$pdate = time();

		/*  Astronomical constants  */
		$epoch = 2444238.5;			// 1980 January 0.0

		/*  Constants defining the Sun's apparent orbit  */
		$elonge = 278.833540;		// Ecliptic longitude of the Sun at epoch 1980.0
		$elongp = 282.596403;		// Ecliptic longitude of the Sun at perigee
		$eccent = 0.016718;			// Eccentricity of Earth's orbit
		$sunsmax = 1.495985e8;		// Semi-major axis of Earth's orbit, km
		$sunangsiz = 0.533128;		// Sun's angular size, degrees, at semi-major axis distance

		/*  Elements of the Moon's orbit, epoch 1980.0  */
		$mmlong = 64.975464;		// Moon's mean longitude at the epoch
		$mmlongp = 349.383063;		// Mean longitude of the perigee at the epoch
		$mlnode = 151.950429;		// Mean longitude of the node at the epoch
		$minc = 5.145396;			// Inclination of the Moon's orbit
		$mecc = 0.054900;			// Eccentricity of the Moon's orbit
		$mangsiz = 0.5181;			// Moon's angular size at distance a from Earth
		$msmax = 384401;			// Semi-major axis of Moon's orbit in km
		$mparallax = 0.9507;		// Parallax at distance a from Earth
		$synmonth = 29.53058868;	// Synodic month (new Moon to new Moon)
		$this->synmonth = $synmonth;
		$lunatbase = 2423436.0;		// Base date for E. W. Brown's numbered series of lunations (1923 January 16)

		/*  Properties of the Earth  */
		// $earthrad = 6378.16;				// Radius of Earth in kilometres
		// $PI = 3.14159265358979323846;	// Assume not near black hole

		$this->timestamp = $pdate;

		// pdate is coming in as a UNIX timstamp, so convert it to Julian
		$pdate =  $pdate / 86400 + 2440587.5;

		/* Calculation of the Sun's position */

		$Day = $pdate - $epoch;								// Date within epoch
		$N = $this->fixangle((360 / 365.2422) * $Day);		// Mean anomaly of the Sun
		$M = $this->fixangle($N + $elonge - $elongp);		// Convert from perigee co-ordinates to epoch 1980.0
		$Ec = $this->kepler($M, $eccent);					// Solve equation of Kepler
		$Ec = sqrt((1 + $eccent) / (1 - $eccent)) * tan($Ec / 2);
		$Ec = 2 * rad2deg(atan($Ec));						// True anomaly
		$Lambdasun = $this->fixangle($Ec + $elongp);		// Sun's geocentric ecliptic longitude

		$F = ((1 + $eccent * cos(deg2rad($Ec))) / (1 - $eccent * $eccent));	// Orbital distance factor
		$SunDist = $sunsmax / $F;							// Distance to Sun in km
		$SunAng = $F * $sunangsiz;							// Sun's angular size in degrees

		/* Calculation of the Moon's position */
		$ml = $this->fixangle(13.1763966 * $Day + $mmlong);				// Moon's mean longitude
		$MM = $this->fixangle($ml - 0.1114041 * $Day - $mmlongp);		// Moon's mean anomaly
		$MN = $this->fixangle($mlnode - 0.0529539 * $Day);				// Moon's ascending node mean longitude
		$Ev = 1.2739 * sin(deg2rad(2 * ($ml - $Lambdasun) - $MM));		// Evection
		$Ae = 0.1858 * sin(deg2rad($M));								// Annual equation
		$A3 = 0.37 * sin(deg2rad($M));									// Correction term
		$MmP = $MM + $Ev - $Ae - $A3;									// Corrected anomaly
		$mEc = 6.2886 * sin(deg2rad($MmP));								// Correction for the equation of the centre
		$A4 = 0.214 * sin(deg2rad(2 * $MmP));							// Another correction term
		$lP = $ml + $Ev + $mEc - $Ae + $A4;								// Corrected longitude
		$V = 0.6583 * sin(deg2rad(2 * ($lP - $Lambdasun)));				// Variation
		$lPP = $lP + $V;												// True longitude
		$NP = $MN - 0.16 * sin(deg2rad($M));							// Corrected longitude of the node
		$y = sin(deg2rad($lPP - $NP)) * cos(deg2rad($minc));			// Y inclination coordinate
		$x = cos(deg2rad($lPP - $NP));									// X inclination coordinate

		$Lambdamoon = rad2deg(atan2($y, $x)) + $NP;						// Ecliptic longitude
		$BetaM = rad2deg(asin(sin(deg2rad($lPP - $NP)) * sin(deg2rad($minc))));		// Ecliptic latitude

		/* Calculation of the phase of the Moon */
		$MoonAge = $lPP - $Lambdasun;									// Age of the Moon in degrees
		$MoonPhase = (1 - cos(deg2rad($MoonAge))) / 2;					// Phase of the Moon

		// Distance of moon from the centre of the Earth
		$MoonDist = ($msmax * (1 - $mecc * $mecc)) / (1 + $mecc * cos(deg2rad($MmP + $mEc)));

		$MoonDFrac = $MoonDist / $msmax;
		$MoonAng = $mangsiz / $MoonDFrac;								// Moon's angular diameter
		// $MoonPar = $mparallax / $MoonDFrac;							// Moon's parallax

		// store results
		$this->phase = $this->fixangle($MoonAge) / 360;					// Phase (0 to 1)
		$this->illum = $MoonPhase;										// Illuminated fraction (0 to 1)
		$this->age = $synmonth * $this->phase;							// Age of moon (days)
		$this->dist = $MoonDist;										// Distance (kilometres)
		$this->angdia = $MoonAng;										// Angular diameter (degrees)
		$this->sundist = $SunDist;										// Distance to Sun (kilometres)
		$this->sunangdia = $SunAng;										// Sun's angular diameter (degrees)
	}

	private function fixangle($a) {
		return ( $a - 360 * floor($a / 360) );
	}

	//  KEPLER  --   Solve the equation of Kepler.
	private function kepler($m, $ecc) {
		//double e, delta;
		$epsilon = pow(1, -6);
		$e = $m = deg2rad($m);
		do {
			$delta = $e - $ecc * sin($e) - $m;
			$e -= $delta / ( 1 - $ecc * cos($e) );
		}
		while ( abs($delta) > $epsilon );
		return $e;
	}

	/*  Calculates  time  of  the mean new Moon for a given
		base date.  This argument K to this function is the
		precomputed synodic month index, given by:
            K = (year - 1900) * 12.3685
        where year is expressed as a year and fractional year.
	*/
	private function meanphase($sdate, $k){
		// Time in Julian centuries from 1900 January 0.5
		$t = ( $sdate - 2415020.0 ) / 36525;
		$t2 = $t * $t;
		$t3 = $t2 * $t;

		$nt1 = 2415020.75933 + $this->synmonth * $k
				+ 0.0001178 * $t2
				- 0.000000155 * $t3
				+ 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		return $nt1;
	}

	/*  Given a K value used to determine the mean phase of
		the new moon, and a phase selector (0.0, 0.25, 0.5,
		0.75), obtain the true, corrected phase time.
	*/
	private function truephase($k, $phase){
		$apcor = false;

		$k += $phase;				// Add phase to new moon time
		$t = $k / 1236.85;			// Time in Julian centuries from 1900 January 0.5
		$t2 = $t * $t;				// Square for frequent use
		$t3 = $t2 * $t;				// Cube for frequent use
		$pt = 2415020.75933			// Mean time of phase
			 + $this->synmonth * $k
			 + 0.0001178 * $t2
			 - 0.000000155 * $t3
			 + 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		$m = 359.2242 + 29.10535608 * $k - 0.0000333 * $t2 - 0.00000347 * $t3;			// Sun's mean anomaly
		$mprime = 306.0253 + 385.81691806 * $k + 0.0107306 * $t2 + 0.00001236 * $t3;	// Moon's mean anomaly
		$f = 21.2964 + 390.67050646 * $k - 0.0016528 * $t2 - 0.00000239 * $t3;			// Moon's argument of latitude
		if ( $phase < 0.01 || abs( $phase - 0.5 ) < 0.01 ) {
		   // Corrections for New and Full Moon
			$pt +=  (0.1734 - 0.000393 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.4068 * sin( deg2rad( $mprime ) )
					+ 0.0161 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0104 * sin( deg2rad( 2 * $f ) )
					- 0.0051 * sin( deg2rad( $m + $mprime ) )
					- 0.0074 * sin( deg2rad( $m - $mprime ) )
					+ 0.0004 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0010 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0005 * sin( deg2rad( $m + 2 * $mprime ) );
			$apcor = true;
		} else if ( abs( $phase - 0.25 ) < 0.01 || abs( $phase - 0.75 ) < 0.01 ) {
			$pt +=  (0.1721 - 0.0004 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.6280 * sin( deg2rad( $mprime ) )
					+ 0.0089 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0079 * sin( deg2rad( 2 * $f ) )
					- 0.0119 * sin( deg2rad( $m + $mprime ) )
					- 0.0047 * sin( deg2rad ( $m - $mprime ) )
					+ 0.0003 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0021 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0003 * sin( deg2rad( $m + 2 * $mprime ) )
					+ 0.0004 * sin( deg2rad( $m - 2 * $mprime ) )
					- 0.0003 * sin( deg2rad( 2 * $m + $mprime ) );
		if ( $phase < 0.5 )		// First quarter correction
			$pt += 0.0028 - 0.0004 * cos( deg2rad( $m ) ) + 0.0003 * cos( deg2rad( $mprime ) );
		else	// Last quarter correction
			$pt += -0.0028 + 0.0004 * cos( deg2rad( $m ) ) - 0.0003 * cos( deg2rad( $mprime ) );
			$apcor = true;
		}
		if (!$apcor)	// function was called with an invalid phase selector
			return false;

		return $pt;
	}

	/* 	Find time of phases of the moon which surround the current date.
		Five phases are found, starting and
		ending with the new moons which bound the  current lunation.
	*/
	private function phasehunt() {
		$sdate = $this->utctojulian( $this->timestamp );
		$adate = $sdate - 45;
		$ats = $this->timestamp - 86400 * 45;
		$yy = (int) gmdate( 'Y', $ats );
		$mm = (int) gmdate( 'n', $ats );

		$k1 = floor( ( $yy + ( ( $mm - 1 ) * ( 1 / 12 ) ) - 1900 ) * 12.3685 );
		$adate = $nt1 = $this->meanphase( $adate, $k1 );

		while (true) {
			$adate += $this->synmonth;
			$k2 = $k1 + 1;
			$nt2 = $this->meanphase( $adate, $k2 );
			// if nt2 is close to sdate, then mean phase isn't good enough, we have to be more accurate
			if( abs( $nt2 - $sdate ) < 0.5 )
				$nt2 = $this->truephase( $k2, 0.0 );
			if ( $nt1 <= $sdate && $nt2 > $sdate )
				break;
			$nt1 = $nt2;
			$k1 = $k2;
		}

		// results in Julian dates
		$data = array(
			$this->truephase( $k1, 0.0 ),
			$this->truephase( $k1, 0.25 ),
			$this->truephase( $k1, 0.5 ),
			$this->truephase( $k1, 0.75 ),
			$this->truephase( $k2, 0.0 )
		);

		$this->quarters = array();
		foreach( $data as $v )
			$this->quarters[] = ( $v - 2440587.5 ) * 86400;	// convert to UNIX time
	}

	/*  Convert UNIX timestamp to astronomical Julian time (i.e. Julian date plus day fraction).  */
	private function utctojulian( $ts ) {
		return $ts / 86400 + 2440587.5;
	}

	private function get_phase( $n ) {
		if( is_null( $this->quarters ) )
			$this->phasehunt();

		return $this->quarters[$n];
	}

	/* Public functions for accessing results */

	function phase(){
		return $this->phase;
	}

	function illumination(){
		return $this->illum;
	}

	function age(){
		return $this->age;
	}

	function distance(){
		return $this->dist;
	}

	function diameter(){
		return $this->angdia;
	}

	function sundistance(){
		return $this->sundist;
	}

	function sundiameter(){
		return $this->sunangdia;
	}

	function new_moon(){
		return $this->get_phase( 0 );
	}

	function first_quarter(){
		return $this->get_phase( 1 );
	}

	function full_moon(){
		return $this->get_phase( 2 );
	}

	function last_quarter(){
		return $this->get_phase( 3 );
	}

	function next_new_moon(){
		return $this->get_phase( 4 );
	}
}
// create an instance of the class, and use the current time
$moon = new MoonPhase();
$age = floor(round( $moon->age(), 1 ));
$stage = $moon->phase() < 0.5 ? 'waxing' : 'waning';
$distance = round( $moon->distance(), 2 );
$next = gmdate( 'G:i:s, j M Y', $moon->next_new_moon() );
echo "The moon is currently $age days old, and is therefore $stage. ";
echo "It is $distance km from the centre of the Earth. ";
echo "The next new moon is at $next.";
$phase = $age;

$im = imagecreatetruecolor(200,200);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

$cx = $cy = 100;
$r = imagesx($im)/2;

imageantialias($im, 1);
imagefill($im, 0, 0, $black);
imagefilledellipse($im, $cx, $cy, 2*$r, 2*$r, $white);
imageellipse($im, $cx, $cy, 2*$r, 2*$r, $black);
imagecolortransparent($im, $black);

for ($y = $cy-$r; $y <= $cy+$r; $y++) {
    $theta = asin(($cy-$y)/$r);
    $x1 = $cx - $r*cos($theta);
    $x2 = $x1 + 2 * $phase * $r * cos($theta);
    imageline($im, $x1, $y, $x2, $y, $black);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
<br />
<br />
<div id='wrapper' style="width: 820px; background-color: #142FAD;">
<img src="moon.php?p=0.1" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.2" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.3" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.4" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.5" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.6" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.7" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.8" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.9" width="200" height="200" alt="moon" />
<img src="moon.php?p=0.95" width="200" height="200" alt="moon" />
</body>
</html>

 

 

Best Regards,

Tim

Edited by MrTIMarshall
Link to comment
Share on other sites

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="moon.css" type="text/css" rel="stylesheet">
</head>
<body>
<?php include 'moonphase.php'; $moon = new MoonPhase(); ?>

<?php
printf(
'The moon is currently %d days old, and is therefore %s. It is %.2f km from the centre of the Earth. The next new moon is at %s.',
$moon->age(), ($moon->phase() < 0.5 ? 'waxing' : 'waning'), $moon->distance(), gmdate('G:i:s, j M Y', $moon->next_new_moon())
);
?>

<div id="wrapper">
<img src="moon.php?p=0.1" width="200" height="200" alt="moon">
<img src="moon.php?p=0.2" width="200" height="200" alt="moon">
<img src="moon.php?p=0.3" width="200" height="200" alt="moon">
<img src="moon.php?p=0.4" width="200" height="200" alt="moon">
<img src="moon.php?p=0.5" width="200" height="200" alt="moon">
<img src="moon.php?p=0.6" width="200" height="200" alt="moon">
<img src="moon.php?p=0.7" width="200" height="200" alt="moon">
<img src="moon.php?p=0.8" width="200" height="200" alt="moon">
<img src="moon.php?p=0.9" width="200" height="200" alt="moon">
<img src="moon.php?p=0.95" width="200" height="200" alt="moon">
</div>
</body>
</html>
Is all you need, remove all other PHP code from your moonphrase.php file Edited by ignace
Link to comment
Share on other sites

I am quite sure I do not need the following;

  <div id="wrapper">
    <img src="moon.php?p=0.1" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.2" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.3" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.4" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.5" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.6" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.7" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.8" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.9" width="200" height="200" alt="moon">
    <img src="moon.php?p=0.95" width="200" height="200" alt="moon">
  </div>

 

As what I am trying to do is show the one correct moon phrase, so on the users visit to the website, they see the current moon in the sky, if there is one. This will be going in with my day and night cycle so the moon and sun will only show when they're in the sky.

 

I need to implement the distance somehow to scale the image as well. At the moment I am continuing with making the 28 moon phrase days as I am not sure if people understand waht I'm trying to do.

 

Best Regards,

Tim

Link to comment
Share on other sites

No you don't need all those img tags - that was just test data to try ou t drawing of the various phases.

 

Here's an updated version of the moon image script which uses the MoonPhase class to handle the phases, waxing and waning and scaling. All you need to pass it via the query string is the date (if other than today) and the maximum diameter EG

 

<img src='moon.php?size=100&date=2013-01-01' />

 

moon.php

<?php

include("moonphase.php");
define('SIZECONST', 0.541169);

$size = isset($_GET['size']) ? $_GET['size'] : 200;

$date = isset($_GET['date']) ? strtotime($_GET['date']) : time();

$moon = new MoonPhase($date);
$phase = $moon->phase();
$illum = $moon->illumination();
$diam = round($moon->diameter(), 6);
$r = $size/SIZECONST;
$scaled = 2*$r*sin($diam/2);

$pcent = 1 - $illum;
$waxing = $phase < 0.5;

$im = imagecreatetruecolor($size,$size);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0xF7, 0xF7, 0xF7);

$cx = $cy = imagesx($im)/2;
$r = $scaled/2;

imageantialias($im, 1);
imagefill($im, 0, 0, $black);
imagefilledellipse($im, $cx, $cy, 2*$r, 2*$r, $white);
imageellipse($im, $cx, $cy, 2*$r, 2*$r, $black);
imagecolortransparent($im, $black);
imagesetthickness($im, 2);

for ($y = $cy-$r; $y <= $cy+$r; $y++) {
    $theta = asin(($cy-$y)/$r);
    if ($waxing) {
        $x1 = $cx - $r*cos($theta);
        $x2 = $x1 + 2 * $pcent * $r * cos($theta);
    } else {
        $x2 = $cx + 2 + $r*cos($theta);
        $x1 = $x2 - 2 * $pcent * $r * cos($theta)-1;
    }
    imageline($im, $x1, $y, $x2, $y, $black);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

 

I am attaching another test script (don't paste it into yours) which will produce a lunar calendar. Invoke with "moonSample.php?month=X" where X is month number. Default is current month.

moonSample.php

Link to comment
Share on other sites

I updated my code with the one you have provided, of which still does not work; https://tornhq.com/Top_Menu/moonphrase.php

 

my code:

<?php
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$start = date('Y-m-d', mktime(0,0,0,$month,1));
$end = date('Y-m-t', strtotime($start));
$s = new DateTime($start);
$e = new DateTime($end);
$e->modify('+1 days');
$inc = new DateInterval('P1D');
$dp = new DatePeriod($s, $inc, $e);
$out = '';
foreach ($dp as $d) {
    $out .= "<span style='color:#FFF'>" . $d->format('j') . "</span>";
    $out .= "<img src='moon.php?date={$d->format('Y-m-d')}&size=100' />\n";
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="author" content="Barand">
<meta name="generator" content="PhpED 8.1">
<meta name="description" content="moon phases">
<meta name="keywords" content="moon phases">
<meta name="creation-date" content="03/16/2013">
<title>Moon</title>
</head>
<body>
<?php echo '<h3>' . $s->format('F Y') . " Moon Phases</h3>\n"?>
<div id='wrapper' style="width: 780px; background-color: #142FAD;">
    <?php echo $out?>
</div> 
</body>
</html>

 

As for the attached file, I am having no luck there to, I am not sure if I have meant to include anything for either. Am I meant to have an image of the moon somewhere, I have had it in my head that this actually somehow creates an image?

 

https://tornhq.com/Top_Menu/moonphrase2.php

 

Best Regards,

Tim

Link to comment
Share on other sites

moonphrase.php

<?php
/**
 * Moon phase calculation class
 * Adapted for PHP from Moontool for Windows (http://www.fourmilab.ch/moontoolw/)
 * by Samir Shah (http://rayofsolaris.net)
 * Last modified August 2012
 **/

class MoonPhase {
	private $timestamp;
	private $phase;
	private $illum;
	private $age;
	private $dist;
	private $angdia;
	private $sundist;
	private $sunangdia;

	private $synmonth;

	private $quarters = null;

	function __construct( $pdate = null ) {
		if( is_null( $pdate ) )
			$pdate = time();

		/*  Astronomical constants  */
		$epoch = 2444238.5;			// 1980 January 0.0

		/*  Constants defining the Sun's apparent orbit  */
		$elonge = 278.833540;		// Ecliptic longitude of the Sun at epoch 1980.0
		$elongp = 282.596403;		// Ecliptic longitude of the Sun at perigee
		$eccent = 0.016718;			// Eccentricity of Earth's orbit
		$sunsmax = 1.495985e8;		// Semi-major axis of Earth's orbit, km
		$sunangsiz = 0.533128;		// Sun's angular size, degrees, at semi-major axis distance

		/*  Elements of the Moon's orbit, epoch 1980.0  */
		$mmlong = 64.975464;		// Moon's mean longitude at the epoch
		$mmlongp = 349.383063;		// Mean longitude of the perigee at the epoch
		$mlnode = 151.950429;		// Mean longitude of the node at the epoch
		$minc = 5.145396;			// Inclination of the Moon's orbit
		$mecc = 0.054900;			// Eccentricity of the Moon's orbit
		$mangsiz = 0.5181;			// Moon's angular size at distance a from Earth
		$msmax = 384401;			// Semi-major axis of Moon's orbit in km
		$mparallax = 0.9507;		// Parallax at distance a from Earth
		$synmonth = 29.53058868;	// Synodic month (new Moon to new Moon)
		$this->synmonth = $synmonth;
		$lunatbase = 2423436.0;		// Base date for E. W. Brown's numbered series of lunations (1923 January 16)

		/*  Properties of the Earth  */
		// $earthrad = 6378.16;				// Radius of Earth in kilometres
		// $PI = 3.14159265358979323846;	// Assume not near black hole

		$this->timestamp = $pdate;

		// pdate is coming in as a UNIX timstamp, so convert it to Julian
		$pdate =  $pdate / 86400 + 2440587.5;

		/* Calculation of the Sun's position */

		$Day = $pdate - $epoch;								// Date within epoch
		$N = $this->fixangle((360 / 365.2422) * $Day);		// Mean anomaly of the Sun
		$M = $this->fixangle($N + $elonge - $elongp);		// Convert from perigee co-ordinates to epoch 1980.0
		$Ec = $this->kepler($M, $eccent);					// Solve equation of Kepler
		$Ec = sqrt((1 + $eccent) / (1 - $eccent)) * tan($Ec / 2);
		$Ec = 2 * rad2deg(atan($Ec));						// True anomaly
		$Lambdasun = $this->fixangle($Ec + $elongp);		// Sun's geocentric ecliptic longitude

		$F = ((1 + $eccent * cos(deg2rad($Ec))) / (1 - $eccent * $eccent));	// Orbital distance factor
		$SunDist = $sunsmax / $F;							// Distance to Sun in km
		$SunAng = $F * $sunangsiz;							// Sun's angular size in degrees

		/* Calculation of the Moon's position */
		$ml = $this->fixangle(13.1763966 * $Day + $mmlong);				// Moon's mean longitude
		$MM = $this->fixangle($ml - 0.1114041 * $Day - $mmlongp);		// Moon's mean anomaly
		$MN = $this->fixangle($mlnode - 0.0529539 * $Day);				// Moon's ascending node mean longitude
		$Ev = 1.2739 * sin(deg2rad(2 * ($ml - $Lambdasun) - $MM));		// Evection
		$Ae = 0.1858 * sin(deg2rad($M));								// Annual equation
		$A3 = 0.37 * sin(deg2rad($M));									// Correction term
		$MmP = $MM + $Ev - $Ae - $A3;									// Corrected anomaly
		$mEc = 6.2886 * sin(deg2rad($MmP));								// Correction for the equation of the centre
		$A4 = 0.214 * sin(deg2rad(2 * $MmP));							// Another correction term
		$lP = $ml + $Ev + $mEc - $Ae + $A4;								// Corrected longitude
		$V = 0.6583 * sin(deg2rad(2 * ($lP - $Lambdasun)));				// Variation
		$lPP = $lP + $V;												// True longitude
		$NP = $MN - 0.16 * sin(deg2rad($M));							// Corrected longitude of the node
		$y = sin(deg2rad($lPP - $NP)) * cos(deg2rad($minc));			// Y inclination coordinate
		$x = cos(deg2rad($lPP - $NP));									// X inclination coordinate

		$Lambdamoon = rad2deg(atan2($y, $x)) + $NP;						// Ecliptic longitude
		$BetaM = rad2deg(asin(sin(deg2rad($lPP - $NP)) * sin(deg2rad($minc))));		// Ecliptic latitude

		/* Calculation of the phase of the Moon */
		$MoonAge = $lPP - $Lambdasun;									// Age of the Moon in degrees
		$MoonPhase = (1 - cos(deg2rad($MoonAge))) / 2;					// Phase of the Moon

		// Distance of moon from the centre of the Earth
		$MoonDist = ($msmax * (1 - $mecc * $mecc)) / (1 + $mecc * cos(deg2rad($MmP + $mEc)));

		$MoonDFrac = $MoonDist / $msmax;
		$MoonAng = $mangsiz / $MoonDFrac;								// Moon's angular diameter
		// $MoonPar = $mparallax / $MoonDFrac;							// Moon's parallax

		// store results
		$this->phase = $this->fixangle($MoonAge) / 360;					// Phase (0 to 1)
		$this->illum = $MoonPhase;										// Illuminated fraction (0 to 1)
		$this->age = $synmonth * $this->phase;							// Age of moon (days)
		$this->dist = $MoonDist;										// Distance (kilometres)
		$this->angdia = $MoonAng;										// Angular diameter (degrees)
		$this->sundist = $SunDist;										// Distance to Sun (kilometres)
		$this->sunangdia = $SunAng;										// Sun's angular diameter (degrees)
	}

	private function fixangle($a) {
		return ( $a - 360 * floor($a / 360) );
	}

	//  KEPLER  --   Solve the equation of Kepler.
	private function kepler($m, $ecc) {
		//double e, delta;
		$epsilon = pow(1, -6);
		$e = $m = deg2rad($m);
		do {
			$delta = $e - $ecc * sin($e) - $m;
			$e -= $delta / ( 1 - $ecc * cos($e) );
		}
		while ( abs($delta) > $epsilon );
		return $e;
	}

	/*  Calculates  time  of  the mean new Moon for a given
		base date.  This argument K to this function is the
		precomputed synodic month index, given by:
            K = (year - 1900) * 12.3685
        where year is expressed as a year and fractional year.
	*/
	private function meanphase($sdate, $k){
		// Time in Julian centuries from 1900 January 0.5
		$t = ( $sdate - 2415020.0 ) / 36525;
		$t2 = $t * $t;
		$t3 = $t2 * $t;

		$nt1 = 2415020.75933 + $this->synmonth * $k
				+ 0.0001178 * $t2
				- 0.000000155 * $t3
				+ 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		return $nt1;
	}

	/*  Given a K value used to determine the mean phase of
		the new moon, and a phase selector (0.0, 0.25, 0.5,
		0.75), obtain the true, corrected phase time.
	*/
	private function truephase($k, $phase){
		$apcor = false;

		$k += $phase;				// Add phase to new moon time
		$t = $k / 1236.85;			// Time in Julian centuries from 1900 January 0.5
		$t2 = $t * $t;				// Square for frequent use
		$t3 = $t2 * $t;				// Cube for frequent use
		$pt = 2415020.75933			// Mean time of phase
			 + $this->synmonth * $k
			 + 0.0001178 * $t2
			 - 0.000000155 * $t3
			 + 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		$m = 359.2242 + 29.10535608 * $k - 0.0000333 * $t2 - 0.00000347 * $t3;			// Sun's mean anomaly
		$mprime = 306.0253 + 385.81691806 * $k + 0.0107306 * $t2 + 0.00001236 * $t3;	// Moon's mean anomaly
		$f = 21.2964 + 390.67050646 * $k - 0.0016528 * $t2 - 0.00000239 * $t3;			// Moon's argument of latitude
		if ( $phase < 0.01 || abs( $phase - 0.5 ) < 0.01 ) {
		   // Corrections for New and Full Moon
			$pt +=  (0.1734 - 0.000393 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.4068 * sin( deg2rad( $mprime ) )
					+ 0.0161 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0104 * sin( deg2rad( 2 * $f ) )
					- 0.0051 * sin( deg2rad( $m + $mprime ) )
					- 0.0074 * sin( deg2rad( $m - $mprime ) )
					+ 0.0004 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0010 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0005 * sin( deg2rad( $m + 2 * $mprime ) );
			$apcor = true;
		} else if ( abs( $phase - 0.25 ) < 0.01 || abs( $phase - 0.75 ) < 0.01 ) {
			$pt +=  (0.1721 - 0.0004 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.6280 * sin( deg2rad( $mprime ) )
					+ 0.0089 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0079 * sin( deg2rad( 2 * $f ) )
					- 0.0119 * sin( deg2rad( $m + $mprime ) )
					- 0.0047 * sin( deg2rad ( $m - $mprime ) )
					+ 0.0003 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0021 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0003 * sin( deg2rad( $m + 2 * $mprime ) )
					+ 0.0004 * sin( deg2rad( $m - 2 * $mprime ) )
					- 0.0003 * sin( deg2rad( 2 * $m + $mprime ) );
		if ( $phase < 0.5 )		// First quarter correction
			$pt += 0.0028 - 0.0004 * cos( deg2rad( $m ) ) + 0.0003 * cos( deg2rad( $mprime ) );
		else	// Last quarter correction
			$pt += -0.0028 + 0.0004 * cos( deg2rad( $m ) ) - 0.0003 * cos( deg2rad( $mprime ) );
			$apcor = true;
		}
		if (!$apcor)	// function was called with an invalid phase selector
			return false;

		return $pt;
	}

	/* 	Find time of phases of the moon which surround the current date.
		Five phases are found, starting and
		ending with the new moons which bound the  current lunation.
	*/
	private function phasehunt() {
		$sdate = $this->utctojulian( $this->timestamp );
		$adate = $sdate - 45;
		$ats = $this->timestamp - 86400 * 45;
		$yy = (int) gmdate( 'Y', $ats );
		$mm = (int) gmdate( 'n', $ats );

		$k1 = floor( ( $yy + ( ( $mm - 1 ) * ( 1 / 12 ) ) - 1900 ) * 12.3685 );
		$adate = $nt1 = $this->meanphase( $adate, $k1 );

		while (true) {
			$adate += $this->synmonth;
			$k2 = $k1 + 1;
			$nt2 = $this->meanphase( $adate, $k2 );
			// if nt2 is close to sdate, then mean phase isn't good enough, we have to be more accurate
			if( abs( $nt2 - $sdate ) < 0.5 )
				$nt2 = $this->truephase( $k2, 0.0 );
			if ( $nt1 <= $sdate && $nt2 > $sdate )
				break;
			$nt1 = $nt2;
			$k1 = $k2;
		}

		// results in Julian dates
		$data = array(
			$this->truephase( $k1, 0.0 ),
			$this->truephase( $k1, 0.25 ),
			$this->truephase( $k1, 0.5 ),
			$this->truephase( $k1, 0.75 ),
			$this->truephase( $k2, 0.0 )
		);

		$this->quarters = array();
		foreach( $data as $v )
			$this->quarters[] = ( $v - 2440587.5 ) * 86400;	// convert to UNIX time
	}

	/*  Convert UNIX timestamp to astronomical Julian time (i.e. Julian date plus day fraction).  */
	private function utctojulian( $ts ) {
		return $ts / 86400 + 2440587.5;
	}

	private function get_phase( $n ) {
		if( is_null( $this->quarters ) )
			$this->phasehunt();

		return $this->quarters[$n];
	}

	/* Public functions for accessing results */

	function phase(){
		return $this->phase;
	}

	function illumination(){
		return $this->illum;
	}

	function age(){
		return $this->age;
	}

	function distance(){
		return $this->dist;
	}

	function diameter(){
		return $this->angdia;
	}

	function sundistance(){
		return $this->sundist;
	}

	function sundiameter(){
		return $this->sunangdia;
	}

	function new_moon(){
		return $this->get_phase( 0 );
	}

	function first_quarter(){
		return $this->get_phase( 1 );
	}

	function full_moon(){
		return $this->get_phase( 2 );
	}

	function last_quarter(){
		return $this->get_phase( 3 );
	}

	function next_new_moon(){
		return $this->get_phase( 4 );
	}
}
?>

 

moon.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

include("moon-phase.php");
define('SIZECONST', 0.541169);

$size = isset($_GET['size']) ? $_GET['size'] : 200;

$date = isset($_GET['date']) ? strtotime($_GET['date']) : time();

$moon = new MoonPhase($date);
$phase = $moon->phase();
$illum = $moon->illumination();
$diam = round($moon->diameter(), 6);
$r = $size/SIZECONST;
$scaled = 2*$r*sin($diam/2);

$pcent = 1 - $illum;
$waxing = $phase < 0.5;

$im = imagecreatetruecolor($size,$size);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0xF7, 0xF7, 0xF7);

$cx = $cy = imagesx($im)/2;
$r = $scaled/2;

imageantialias($im, 1);
imagefill($im, 0, 0, $black);
imagefilledellipse($im, $cx, $cy, 2*$r, 2*$r, $white);
imageellipse($im, $cx, $cy, 2*$r, 2*$r, $black);
imagecolortransparent($im, $black);
imagesetthickness($im, 2);

for ($y = $cy-$r; $y <= $cy+$r; $y++) {
    $theta = asin(($cy-$y)/$r);
    if ($waxing) {
        $x1 = $cx - $r*cos($theta);
        $x2 = $x1 + 2 * $pcent * $r * cos($theta);
    } else {
        $x2 = $cx + 2 + $r*cos($theta);
        $x1 = $x2 - 2 * $pcent * $r * cos($theta)-1;
    }
    imageline($im, $x1, $y, $x2, $y, $black);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
<img src='moon.php?size=100&date=<?php echo date("Y/m/d"); ?>' />
</body>
</html>

 

URL: https://tornhq.com/Top_Menu/moon.php

Result: Unable to show, not allowed to use the image extension here.

Link to comment
Share on other sites

Why do you have HTML code inside of an image? The only thing that should be in your code is:

 

 

moonphrase.php

<?php
/**
 * Moon phase calculation class
 * Adapted for PHP from Moontool for Windows (http://www.fourmilab.ch/moontoolw/)
 * by Samir Shah (http://rayofsolaris.net)
 * Last modified August 2012
 **/

class MoonPhase {
	private $timestamp;
	private $phase;
	private $illum;
	private $age;
	private $dist;
	private $angdia;
	private $sundist;
	private $sunangdia;

	private $synmonth;

	private $quarters = null;

	function __construct( $pdate = null ) {
		if( is_null( $pdate ) )
			$pdate = time();

		/*  Astronomical constants  */
		$epoch = 2444238.5;			// 1980 January 0.0

		/*  Constants defining the Sun's apparent orbit  */
		$elonge = 278.833540;		// Ecliptic longitude of the Sun at epoch 1980.0
		$elongp = 282.596403;		// Ecliptic longitude of the Sun at perigee
		$eccent = 0.016718;			// Eccentricity of Earth's orbit
		$sunsmax = 1.495985e8;		// Semi-major axis of Earth's orbit, km
		$sunangsiz = 0.533128;		// Sun's angular size, degrees, at semi-major axis distance

		/*  Elements of the Moon's orbit, epoch 1980.0  */
		$mmlong = 64.975464;		// Moon's mean longitude at the epoch
		$mmlongp = 349.383063;		// Mean longitude of the perigee at the epoch
		$mlnode = 151.950429;		// Mean longitude of the node at the epoch
		$minc = 5.145396;			// Inclination of the Moon's orbit
		$mecc = 0.054900;			// Eccentricity of the Moon's orbit
		$mangsiz = 0.5181;			// Moon's angular size at distance a from Earth
		$msmax = 384401;			// Semi-major axis of Moon's orbit in km
		$mparallax = 0.9507;		// Parallax at distance a from Earth
		$synmonth = 29.53058868;	// Synodic month (new Moon to new Moon)
		$this->synmonth = $synmonth;
		$lunatbase = 2423436.0;		// Base date for E. W. Brown's numbered series of lunations (1923 January 16)

		/*  Properties of the Earth  */
		// $earthrad = 6378.16;				// Radius of Earth in kilometres
		// $PI = 3.14159265358979323846;	// Assume not near black hole

		$this->timestamp = $pdate;

		// pdate is coming in as a UNIX timstamp, so convert it to Julian
		$pdate =  $pdate / 86400 + 2440587.5;

		/* Calculation of the Sun's position */

		$Day = $pdate - $epoch;								// Date within epoch
		$N = $this->fixangle((360 / 365.2422) * $Day);		// Mean anomaly of the Sun
		$M = $this->fixangle($N + $elonge - $elongp);		// Convert from perigee co-ordinates to epoch 1980.0
		$Ec = $this->kepler($M, $eccent);					// Solve equation of Kepler
		$Ec = sqrt((1 + $eccent) / (1 - $eccent)) * tan($Ec / 2);
		$Ec = 2 * rad2deg(atan($Ec));						// True anomaly
		$Lambdasun = $this->fixangle($Ec + $elongp);		// Sun's geocentric ecliptic longitude

		$F = ((1 + $eccent * cos(deg2rad($Ec))) / (1 - $eccent * $eccent));	// Orbital distance factor
		$SunDist = $sunsmax / $F;							// Distance to Sun in km
		$SunAng = $F * $sunangsiz;							// Sun's angular size in degrees

		/* Calculation of the Moon's position */
		$ml = $this->fixangle(13.1763966 * $Day + $mmlong);				// Moon's mean longitude
		$MM = $this->fixangle($ml - 0.1114041 * $Day - $mmlongp);		// Moon's mean anomaly
		$MN = $this->fixangle($mlnode - 0.0529539 * $Day);				// Moon's ascending node mean longitude
		$Ev = 1.2739 * sin(deg2rad(2 * ($ml - $Lambdasun) - $MM));		// Evection
		$Ae = 0.1858 * sin(deg2rad($M));								// Annual equation
		$A3 = 0.37 * sin(deg2rad($M));									// Correction term
		$MmP = $MM + $Ev - $Ae - $A3;									// Corrected anomaly
		$mEc = 6.2886 * sin(deg2rad($MmP));								// Correction for the equation of the centre
		$A4 = 0.214 * sin(deg2rad(2 * $MmP));							// Another correction term
		$lP = $ml + $Ev + $mEc - $Ae + $A4;								// Corrected longitude
		$V = 0.6583 * sin(deg2rad(2 * ($lP - $Lambdasun)));				// Variation
		$lPP = $lP + $V;												// True longitude
		$NP = $MN - 0.16 * sin(deg2rad($M));							// Corrected longitude of the node
		$y = sin(deg2rad($lPP - $NP)) * cos(deg2rad($minc));			// Y inclination coordinate
		$x = cos(deg2rad($lPP - $NP));									// X inclination coordinate

		$Lambdamoon = rad2deg(atan2($y, $x)) + $NP;						// Ecliptic longitude
		$BetaM = rad2deg(asin(sin(deg2rad($lPP - $NP)) * sin(deg2rad($minc))));		// Ecliptic latitude

		/* Calculation of the phase of the Moon */
		$MoonAge = $lPP - $Lambdasun;									// Age of the Moon in degrees
		$MoonPhase = (1 - cos(deg2rad($MoonAge))) / 2;					// Phase of the Moon

		// Distance of moon from the centre of the Earth
		$MoonDist = ($msmax * (1 - $mecc * $mecc)) / (1 + $mecc * cos(deg2rad($MmP + $mEc)));

		$MoonDFrac = $MoonDist / $msmax;
		$MoonAng = $mangsiz / $MoonDFrac;								// Moon's angular diameter
		// $MoonPar = $mparallax / $MoonDFrac;							// Moon's parallax

		// store results
		$this->phase = $this->fixangle($MoonAge) / 360;					// Phase (0 to 1)
		$this->illum = $MoonPhase;										// Illuminated fraction (0 to 1)
		$this->age = $synmonth * $this->phase;							// Age of moon (days)
		$this->dist = $MoonDist;										// Distance (kilometres)
		$this->angdia = $MoonAng;										// Angular diameter (degrees)
		$this->sundist = $SunDist;										// Distance to Sun (kilometres)
		$this->sunangdia = $SunAng;										// Sun's angular diameter (degrees)
	}

	private function fixangle($a) {
		return ( $a - 360 * floor($a / 360) );
	}

	//  KEPLER  --   Solve the equation of Kepler.
	private function kepler($m, $ecc) {
		//double e, delta;
		$epsilon = pow(1, -6);
		$e = $m = deg2rad($m);
		do {
			$delta = $e - $ecc * sin($e) - $m;
			$e -= $delta / ( 1 - $ecc * cos($e) );
		}
		while ( abs($delta) > $epsilon );
		return $e;
	}

	/*  Calculates  time  of  the mean new Moon for a given
		base date.  This argument K to this function is the
		precomputed synodic month index, given by:
            K = (year - 1900) * 12.3685
        where year is expressed as a year and fractional year.
	*/
	private function meanphase($sdate, $k){
		// Time in Julian centuries from 1900 January 0.5
		$t = ( $sdate - 2415020.0 ) / 36525;
		$t2 = $t * $t;
		$t3 = $t2 * $t;

		$nt1 = 2415020.75933 + $this->synmonth * $k
				+ 0.0001178 * $t2
				- 0.000000155 * $t3
				+ 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		return $nt1;
	}

	/*  Given a K value used to determine the mean phase of
		the new moon, and a phase selector (0.0, 0.25, 0.5,
		0.75), obtain the true, corrected phase time.
	*/
	private function truephase($k, $phase){
		$apcor = false;

		$k += $phase;				// Add phase to new moon time
		$t = $k / 1236.85;			// Time in Julian centuries from 1900 January 0.5
		$t2 = $t * $t;				// Square for frequent use
		$t3 = $t2 * $t;				// Cube for frequent use
		$pt = 2415020.75933			// Mean time of phase
			 + $this->synmonth * $k
			 + 0.0001178 * $t2
			 - 0.000000155 * $t3
			 + 0.00033 * sin( deg2rad( 166.56 + 132.87 * $t - 0.009173 * $t2 ) );

		$m = 359.2242 + 29.10535608 * $k - 0.0000333 * $t2 - 0.00000347 * $t3;			// Sun's mean anomaly
		$mprime = 306.0253 + 385.81691806 * $k + 0.0107306 * $t2 + 0.00001236 * $t3;	// Moon's mean anomaly
		$f = 21.2964 + 390.67050646 * $k - 0.0016528 * $t2 - 0.00000239 * $t3;			// Moon's argument of latitude
		if ( $phase < 0.01 || abs( $phase - 0.5 ) < 0.01 ) {
		   // Corrections for New and Full Moon
			$pt +=  (0.1734 - 0.000393 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.4068 * sin( deg2rad( $mprime ) )
					+ 0.0161 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0104 * sin( deg2rad( 2 * $f ) )
					- 0.0051 * sin( deg2rad( $m + $mprime ) )
					- 0.0074 * sin( deg2rad( $m - $mprime ) )
					+ 0.0004 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0010 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0005 * sin( deg2rad( $m + 2 * $mprime ) );
			$apcor = true;
		} else if ( abs( $phase - 0.25 ) < 0.01 || abs( $phase - 0.75 ) < 0.01 ) {
			$pt +=  (0.1721 - 0.0004 * $t) * sin( deg2rad( $m ) )
					+ 0.0021 * sin( deg2rad( 2 * $m ) )
					- 0.6280 * sin( deg2rad( $mprime ) )
					+ 0.0089 * sin( deg2rad( 2 * $mprime) )
					- 0.0004 * sin( deg2rad( 3 * $mprime ) )
					+ 0.0079 * sin( deg2rad( 2 * $f ) )
					- 0.0119 * sin( deg2rad( $m + $mprime ) )
					- 0.0047 * sin( deg2rad ( $m - $mprime ) )
					+ 0.0003 * sin( deg2rad( 2 * $f + $m ) )
					- 0.0004 * sin( deg2rad( 2 * $f - $m ) )
					- 0.0006 * sin( deg2rad( 2 * $f + $mprime ) )
					+ 0.0021 * sin( deg2rad( 2 * $f - $mprime ) )
					+ 0.0003 * sin( deg2rad( $m + 2 * $mprime ) )
					+ 0.0004 * sin( deg2rad( $m - 2 * $mprime ) )
					- 0.0003 * sin( deg2rad( 2 * $m + $mprime ) );
		if ( $phase < 0.5 )		// First quarter correction
			$pt += 0.0028 - 0.0004 * cos( deg2rad( $m ) ) + 0.0003 * cos( deg2rad( $mprime ) );
		else	// Last quarter correction
			$pt += -0.0028 + 0.0004 * cos( deg2rad( $m ) ) - 0.0003 * cos( deg2rad( $mprime ) );
			$apcor = true;
		}
		if (!$apcor)	// function was called with an invalid phase selector
			return false;

		return $pt;
	}

	/* 	Find time of phases of the moon which surround the current date.
		Five phases are found, starting and
		ending with the new moons which bound the  current lunation.
	*/
	private function phasehunt() {
		$sdate = $this->utctojulian( $this->timestamp );
		$adate = $sdate - 45;
		$ats = $this->timestamp - 86400 * 45;
		$yy = (int) gmdate( 'Y', $ats );
		$mm = (int) gmdate( 'n', $ats );

		$k1 = floor( ( $yy + ( ( $mm - 1 ) * ( 1 / 12 ) ) - 1900 ) * 12.3685 );
		$adate = $nt1 = $this->meanphase( $adate, $k1 );

		while (true) {
			$adate += $this->synmonth;
			$k2 = $k1 + 1;
			$nt2 = $this->meanphase( $adate, $k2 );
			// if nt2 is close to sdate, then mean phase isn't good enough, we have to be more accurate
			if( abs( $nt2 - $sdate ) < 0.5 )
				$nt2 = $this->truephase( $k2, 0.0 );
			if ( $nt1 <= $sdate && $nt2 > $sdate )
				break;
			$nt1 = $nt2;
			$k1 = $k2;
		}

		// results in Julian dates
		$data = array(
			$this->truephase( $k1, 0.0 ),
			$this->truephase( $k1, 0.25 ),
			$this->truephase( $k1, 0.5 ),
			$this->truephase( $k1, 0.75 ),
			$this->truephase( $k2, 0.0 )
		);

		$this->quarters = array();
		foreach( $data as $v )
			$this->quarters[] = ( $v - 2440587.5 ) * 86400;	// convert to UNIX time
	}

	/*  Convert UNIX timestamp to astronomical Julian time (i.e. Julian date plus day fraction).  */
	private function utctojulian( $ts ) {
		return $ts / 86400 + 2440587.5;
	}

	private function get_phase( $n ) {
		if( is_null( $this->quarters ) )
			$this->phasehunt();

		return $this->quarters[$n];
	}

	/* Public functions for accessing results */

	function phase(){
		return $this->phase;
	}

	function illumination(){
		return $this->illum;
	}

	function age(){
		return $this->age;
	}

	function distance(){
		return $this->dist;
	}

	function diameter(){
		return $this->angdia;
	}

	function sundistance(){
		return $this->sundist;
	}

	function sundiameter(){
		return $this->sunangdia;
	}

	function new_moon(){
		return $this->get_phase( 0 );
	}

	function first_quarter(){
		return $this->get_phase( 1 );
	}

	function full_moon(){
		return $this->get_phase( 2 );
	}

	function last_quarter(){
		return $this->get_phase( 3 );
	}

	function next_new_moon(){
		return $this->get_phase( 4 );
	}
}
?>

 

moon.php

 

<?php

include("moon-phase.php");
define('SIZECONST', 0.541169);

$size = isset($_GET['size']) ? $_GET['size'] : 200;

$date = isset($_GET['date']) ? strtotime($_GET['date']) : time();

$moon = new MoonPhase($date);
$phase = $moon->phase();
$illum = $moon->illumination();
$diam = round($moon->diameter(), 6);
$r = $size/SIZECONST;
$scaled = 2*$r*sin($diam/2);

$pcent = 1 - $illum;
$waxing = $phase < 0.5;

$im = imagecreatetruecolor($size,$size);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0xF7, 0xF7, 0xF7);

$cx = $cy = imagesx($im)/2;
$r = $scaled/2;

imageantialias($im, 1);
imagefill($im, 0, 0, $black);
imagefilledellipse($im, $cx, $cy, 2*$r, 2*$r, $white);
imageellipse($im, $cx, $cy, 2*$r, 2*$r, $black);
imagecolortransparent($im, $black);
imagesetthickness($im, 2);

for ($y = $cy-$r; $y <= $cy+$r; $y++) {
    $theta = asin(($cy-$y)/$r);
    if ($waxing) {
        $x1 = $cx - $r*cos($theta);
        $x2 = $x1 + 2 * $pcent * $r * cos($theta);
    } else {
        $x2 = $cx + 2 + $r*cos($theta);
        $x1 = $x2 - 2 * $pcent * $r * cos($theta)-1;
    }
    imageline($im, $x1, $y, $x2, $y, $black);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

 

URL: https://tornhq.com/Top_Menu/moon.php

Result: Unable to show, not allowed to use the image extension here.

 

Link to comment
Share on other sites

Oh, I didn't realize it was just a white moon. If I had an image of the moon could it do the moon phrases? I have been manually doing them in Photoshop and I almost have the full moon phrase.

 

Also, how can I have the result of how far away the moon is shown on the page dynamically instead of a new value per page load?

Link to comment
Share on other sites

Sorry

 

Sorry for what? Your work is fantastic, I simply thought this might have been something that you may have overlooked.

 

Anyway, for the time being, please do not provide anymore help as I am trying to get some Javascript help to make my page dynamic and I did not know at first that you cannot achieve this via PHP.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
	body, html {
		margin:0;
		border:0;
	}
	#sky {
		width: 100%;
		height: 250px;
		margin: 0;
		padding: 0;
		background: #555599;
	}
	#sky .sunwrap {
		position: relative;
		width: 80%;
		height: 250px;
		margin: 0 auto;
		overflow: hidden;
	}
	#sun {
		position: relative;
		width: 50px;
		height: 50px;
		;
		
		border-radius:50%;
		background: #fefcea;
		background: -moz-linear-gradient(top, #fefcea 0%, #f1da36 100%);
		background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fefcea), color-stop(100%, #f1da36));
		background: -webkit-linear-gradient(top, #fefcea 0%, #f1da36 100%);
		background: -o-linear-gradient(top, #fefcea 0%, #f1da36 100%);
		background: -ms-linear-gradient(top, #fefcea 0%, #f1da36 100%);
		background: linear-gradient(to bottom, #fefcea 0%, #f1da36 100%);
		filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fefcea', endColorstr='#f1da36', GradientType=0);
		-webkit-box-shadow: 0px 0px 28px 0px #ffffff;
		box-shadow: 0px 0px 28px 0px #ffffff;
	}
</style>
</head>
<body>
<?php
	$lat = 53.3761;
	$long= -2.1897;
	
	$hour = isset($_GET['hour']) ? $_GET['hour'] : 0;
	$timenow = time();
	// var timenow = new Date().getTime() / 1000;
	
	
	$today = mktime(0,0,0);
	echo "Today = $today<br />";
	$todayInfo = date_sun_info($today, $lat, $long);
	echo "Today = $todayInfo<br />";
	$nighttime = false;
	// var nighttime = false;
	
	switch(true) {
	case $timenow < $todayInfo['sunrise']: // is it pre-sunrise
			 // get last night's sunset
			 $nighttime = true;
			 $yesterdayInfo = date_sun_info(strtotime('-1 days', $today), $lat, $long);
			 $percent = round(($timenow - $yesterdayInfo['sunset'] )*100/($todayInfo['sunrise'] - $yesterdayInfo['sunset']));
			 break;
			
	case $timenow > $todayInfo['sunset']:
			 // get tomorrow's sunrise
			 $nighttime = true;
			 $tomorrowInfo = date_sun_info(strtotime('+1 days', $today), $lat, $long);
			 $percent = round(($timenow - $todayInfo['sunset'] )*100/($tomorrowInfo['sunrise'] - $todayInfo['sunset']));
			 break;
			
	default:
			 $percent = round(($timenow - $todayInfo['sunrise'])*100/($todayInfo['sunset'] - $todayInfo['sunrise']));
			 break;
	}
	if ($nighttime) {
	$bg = '#000';
	$fg = '#FFF';
	} else {
	$bg = '#FFF';
	$fg = '#000';
	}
	

	if ($nighttime) {
			$bgcols = array (
					20  => '#CCC',
					40  => '#888',
					50  => '#2F2F4F', // Mightnight Blue
					60  => '#333',
					80  => '#000',
					100 => '#AECAD4', // Dawn Sky Blue
			);
			foreach ($bgcols as $pc => $color) {
					if ($percent <= $pc) {
							$bg = $color;
							break;
					}
			}
			$fg = '#FFF';
	} else {
			$bgcols = array (
					20  => '#000',
					40  => '#000',
					60  => '#000',
					80  => '#000',
					98  => '#000',
					100 => '#26538D', // Dusk Sky Blue
			);
			foreach ($bgcols as $pc => $color) {
					if ($percent <= $pc) {
							$bg = $color;
							break;
					}
			}
			$fg = '#FFF';
	}
?>
<div id="sky">
    <div class="sunwrap">
        <div id="sun"></div>
    </div>
</div>
<script type="text/javascript">
var domSun = document.getElementById('sun'),
    timePassed = <?php echo $percent / 100; ?>,
    origin = {
        x: 50,
        y: 110
    },
    radius = {
        x: 40,
        y: 95
    };

    var position = {
        x: origin.x + radius.x * Math.cos(timePassed * Math.PI),
        y: origin.y - radius.y * Math.sin(timePassed * Math.PI)
    };

    domSun.style.left = position.x + '%';
    domSun.style.top = position.y + '%';

</script>
<br />
<h1>Currently at: <?php echo $percent / 100; ?>,</h1><br />
</body>
</html>

 

I feel guilty as a lot of your work is in that Barand and I am not yet sure how much of it is needed to make it dynamic :(

Link to comment
Share on other sites

As far as I know it is dynamic - it picks up the information on phase, illumination, diameter and draws the moon image according to those parameters. How dynamic do you want?

 

Anyway, with a bit of masking ensuring a circular moon image and all background pixels are black, I cleaned up the images (sample attached) Never don this before. it's been interesting.

 

post-3105-0-95386500-1363615134_thumb.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.