W, 'now' marker @ N // may be overridden by eg. php?showTime=false (or true) $dial = true; // if $time_tick = true and $dial = true hours markers will be drawn // may be overridden by eg. php?showDial=false (or true) $scheme = "new"; // "new" = yellow, cyan, mid-blue, dark blue, black // "old" = cyan, mid-blue, dark blue, near black, black $pc_cacheFile = './cache/heavenpie.png'; // Relative path and name of cached image file $pc_cacheTime = 2; // Cache time for 'heavens.png' in minutes $transparency = true; // true / false - attempts to make background transparent, not totally acceptable ! // date / time is default 'now' for locale set - override 'now' by php?event=[unix timestamp] // if you want to change the background colour look for $color_bg about line 158 (R, B, G values) //----------------------------------------------------------------------------\\ // SETTINGS: only customise if you have no "Settings.php" \\ //----------------------------------------------------------------------------\\ // Uncomment following line and as below to automatically detect lat and long //if (!@(include_once "Settings.php")) { $SITE['latitude' ] = 40.790541; // N+, S- $SITE['longitude'] = -96.631311; // W-, E+ // Uncomment following line to automatically detect lat and long //} // may be overridden by php?lat=103.652&lon=-74.123 //----------------------------------------------------------------------------\\ // *** END user settings *** $uts = date("U"); // current unix time stamp $updateCache = true; // update cache file(s) $updateTime = $pc_cacheTime * 60; // change cache time minutes to seconds if(file_exists($pc_cacheFile)) { // IF the data cache file exists $cft = filemtime($pc_cacheFile); // get cache file last modified time $cfAge = $uts - $cft; // get cache file age in seconds if($cfAge < $updateTime) { // IF cache file has not expired $updateCache = false; // don't update echo "\n"; // display notice } } if (!$updateCache) { // echo 'Cache File Fresh - Exiting
'; // Debug Code return; // Leave if the cache file isn't stale } // check for posted overrides if(isset($_REQUEST['lat'])){ $lat = $_REQUEST['lat']; } else { $lat = $SITE['latitude']; } if(isset($_REQUEST['lon'])){ $lng = $_REQUEST['lon']; } else { $lng = $SITE['longitude']; } if(isset($_REQUEST['locale'])){ $time_locale = $_REQUEST['locale']; } date_default_timezone_set($time_locale); $today = time(); $todayStart = mktime(0, 0, 0, date("m") , date("d"), date("Y")); $tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y")); if(isset($_REQUEST['event'])){ $when = $_REQUEST['event']; } else { $when = time(); } $whenStart = mktime(0, 0, 1, date("m", $when) , date("d", $when), date("Y", $when)); $whenEnd = mktime(23, 59, 59, date("m", $when) , date("d", $when), date("Y", $when)); $gmtoffset = date("Z", $when) / 3600; $gmtoffset1 = date("Z", $whenStart) / 3600; $gmtoffset2 = date("Z", $whenEnd) / 3600; $offset = $gmtoffset1 - $gmtoffset2; // hours if(isset($_REQUEST['suninfo'])){ $use_sun_info = ($_REQUEST['suninfo'] == 'true' ? true : false); } if(isset($_REQUEST['showTime'])){ $showTime = ($_REQUEST['showTime'] == 'true' ? true : false); } else { $showTime = $time_tick; } if(isset($_REQUEST['showDial'])){ $dial = ($_REQUEST['showDial'] == 'true' ? true : false); } $markers = ($showTime && $dial ? true : false); $drawHeight = $height * 4; // internally draw 4x the size, then we resample down at the end to antialias $drawWidth = $width * 4; $img = @imagecreatetruecolor($drawWidth,$drawHeight); $imgFinal = @imagecreatetruecolor($width,$height); // image colours $color_bg = imagecolorallocate($img, 0x00, 0x00, 0x00); // 'default' background fill if($transparency){ imagecolortransparent($img, $color_bg); // Make the background transparent imagecolortransparent($imgFinal, $color_bg); // Make the background transparent } imagefill($img, 0, 0, $color_bg); $color_black = imagecolorallocate($img, 24, 7, 70); // why isn't black er black ? $color_white = imagecolorallocate($img, 255, 255, 255); // used by hour markers if required if($scheme == "new"){ // shift default colours so astro is more obvious $color_astro = imagecolorallocate($img, 65, 86, 166); // was nautical $color_nautical = imagecolorallocate($img, 88, 118, 226); // was civil $color_civil = imagecolorallocate($img, 159, 244, 254); // was day $color_day = imagecolorallocate($img, 255, 255, 0); // yellow - new } else { // original colours $color_astro = imagecolorallocate($img, 39, 52, 80); $color_nautical = imagecolorallocate($img, 65, 86, 166); $color_civil = imagecolorallocate($img, 88, 118, 226); $color_day = imagecolorallocate($img, 159, 244, 254); } // next colour is 'now' time marker or 24hr gnoman $color_tick_time = imagecolorallocate($img, 255, 0, 0); // Red // initalise some vars $done = false; $sunDraw = false; $civilDraw = false; $nautDraw = false; $astroDraw = false; // if earlier than PHP 5.2 date_sun_info won't be available - so do an override check ! $use_sun_info = ($use_sun_info && function_exists('date_sun_info') ? true : false); if($use_sun_info) $sun_info = date_sun_info($when, $lat, $lng); // Official Sunrise if(!$use_sun_info){ $eventRise = date_sunrise($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90.583333, $gmtoffset); $eventSet = date_sunset ($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90.583333, $gmtoffset); } else { $eventRise = $sun_info['sunrise']; if($eventRise == 1) $eventRise = null; // imitate date_sunrise 'format' if($eventRise !=null) $eventRise = ($eventRise - $whenStart) /3600; $eventSet = $sun_info['sunset']; if($eventSet == 1) $eventSet = null; // imitate date_sunset 'format' if($eventSet !=null) $eventSet = ($eventSet - $whenStart) /3600; } if($eventRise == null && $eventSet == null){ // event 'spans' current day, but is it 'up' or 'down' ? $checkDate = $when; for($x = 1; $x < 366 ; $x++){ // step backwards to find last event transition $checkDate = $when - ($x * 24 * 60 * 60); // day(s) before if(!$use_sun_info){ $checkRise = date_sunrise($checkDate, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90.583333, $gmtoffset); $checkSet = date_sunset ($checkDate, SUNFUNCS_RET_DOUBLE, $lat, $lng, 90.583333, $gmtoffset); } else { $check_info = date_sun_info($checkDate, $lat, $lng); $checkRise = $check_info['sunrise']; if($checkRise == 1) $checkRise = null; // imitate date_sunrise 'format' if($checkRise !=null) $checkRise = (date("H", $checkRise) *1) + (date("i", $checkRise) /60); $checkSet = $check_info['sunset']; if($checkSet == 1) $checkSet = null; // imitate date_sunset 'format' if($checkSet !=null) $checkSet = (date("H", $checkSet) *1) + (date("i", $checkSet) /60); } if($checkRise != null || $checkSet != null) { // we have found last event transition // what was it ? if($checkSet == null || $checkRise > $checkSet || $checkSet - $checkRise > 12){ // Sun is 'up' 24 hr and MUST be drawn // $sunRise = 1; $sunSet = 0; $sunRise = 24; $sunSet = 0; $done = true; $sunDraw = true; } break; } } } else { // at least one of the transitions occurs 'today' $sunRise = $eventRise ; // returns 0 if event = null $sunSet = $eventSet ; // returns 0 if event = null $sunDraw = true; } // END Sun rise / set // civil twilight if(!$done){ if(!$use_sun_info){ $eventRise = date_sunrise($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 96, $gmtoffset); $eventSet = date_sunset ($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 96, $gmtoffset); } else { $eventRise = $sun_info['civil_twilight_begin']; if($eventRise == 1) $eventRise = null; // imitate date_sunrise 'format' if($eventRise !=null) $eventRise = ($eventRise - $whenStart) /3600; $eventSet = $sun_info['civil_twilight_end']; if($eventSet == 1) $eventSet = null; // imitate date_sunset 'format' if($eventSet !=null) $eventSet = ($eventSet - $whenStart) /3600; } if($eventRise == null && $eventSet == null){ // event 'spans' current day // $civilRise = 1; $civilSet = 0; $civilRise = 24; $civilSet = 0; if($sunDraw){ $civilDraw = true; $done = true; } } else { // at least one of the transitions occurs 'today' $civilRise = $eventRise; // returns 0 if event = null $civilSet = $eventSet; // returns 0 if event = null $civilDraw = true; } } // END civil twilight // nautical twilight if(!$done){ if(!$use_sun_info){ $eventRise = date_sunrise($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 102, $gmtoffset); $eventSet = date_sunset ($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 102, $gmtoffset); } else { $eventRise = $sun_info['nautical_twilight_begin']; if($eventRise == 1) $eventRise = null; // imitate date_sunrise 'format' if($eventRise !=null) $eventRise = ($eventRise - $whenStart) /3600; $eventSet = $sun_info['nautical_twilight_end']; if($eventSet == 1) $eventSet = null; // imitate date_sunset 'format' if($eventSet !=null) $eventSet = ($eventSet - $whenStart) /3600; } if($eventRise == null && $eventSet == null){ // event 'spans' current day // $nautRise = 1; $nautSet = 0; $nautRise = 24; $nautSet = 0; if($civilDraw){ $nautDraw = true; $done = true; } } else { // at least one of the transitions occurs 'today' $nautRise = $eventRise; // returns 0 if event = null $nautSet = $eventSet; // returns 0 if event = null $nautDraw = true; } } // END nautical twilight // astronomical twilight if(!$done){ if(!$use_sun_info){ $eventRise = date_sunrise($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 108, $gmtoffset); $eventSet = date_sunset ($when, SUNFUNCS_RET_DOUBLE, $lat, $lng, 108, $gmtoffset); } else { $eventRise = $sun_info['astronomical_twilight_begin']; if($eventRise == 1) $eventRise = null; // imitate date_sunrise 'format' if($eventRise !=null) $eventRise = ($eventRise - $whenStart) /3600; $eventSet = $sun_info['astronomical_twilight_end']; if($eventSet == 1) $eventSet = null; // imitate date_sunset 'format' if($eventSet !=null) $eventSet = ($eventSet - $whenStart) /3600; if($eventSet >= 24) $eventSet = $eventSet-24; // this is TOMORROW ! } if($eventRise == null && $eventSet == null){ // event 'spans' current day // $astroRise = 1; $astroSet = 0; $astroRise = 24; $astroSet = 0; if($nautDraw) $astroDraw = true; } else { // at least one of the transitions occurs 'today' $astroRise = $eventRise; // returns 0 if event = null $astroSet = $eventSet; // returns 0 if event = null $astroDraw = true; } } // END astronomical twilight // now we need to 'layer' events from dark to light // draw ellipse ******************** (-1 values reduces rough 'shadow' effect) imagefilledellipse($img, round($drawWidth /2), round($drawHeight /2), $drawWidth-1, $drawHeight-1, $color_black); if($markers) do_dial(0, 359, $color_white, $color_black); $gnoman = round(((date("H", $today) *1) + (date("i", $today) /60)) *360 /24) +90; if($astroDraw === true){ if($showTime === false){ // heavens revolve, marker stationary $from = round($astroRise *360 /24) - $gnoman; $to = round($astroSet *360 /24) - $gnoman; }else{ // heavens static, 24hr hand sweep $from = round($astroRise *360 /24) +90; $to = round($astroSet *360 /24) +90 ; } imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight, $from, $to, $color_astro, IMG_ARC_PIE); // from 3:00 > 5:30 if($markers) do_dial($from, $to, $color_white, $color_astro); } if($nautDraw === true){ if($showTime === false){ // heavens revolve, marker stationary $from = round($nautRise *360 /24) - $gnoman; $to = round($nautSet *360 /24) - $gnoman; }else{ // heavens static, 24hr hand sweep $from = round($nautRise *360 /24) +90; $to = round($nautSet *360 /24) +90 ; } imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight, $from, $to, $color_nautical, IMG_ARC_PIE); // from 3:00 > 5:30 if($markers) do_dial($from, $to, $color_white, $color_nautical); } if($civilDraw === true){ if($showTime === false){ // heavens revolve, marker stationary $from = round($civilRise *360 /24) - $gnoman; $to = round($civilSet *360 /24) - $gnoman; }else{ // heavens static, 24hr hand sweep $from = round($civilRise *360 /24) +90; $to = round($civilSet *360 /24) +90 ; } imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight, $from, $to, $color_civil, IMG_ARC_PIE); // from 3:00 > 5:30 if($markers) do_dial($from, $to, $color_black, $color_civil); } if($sunDraw === true){ if($showTime === false){ // heavens revolve, marker stationary $from = round($sunRise *360 /24) - $gnoman; $to = round($sunSet *360 /24) - $gnoman; }else{ // heavens static, 24hr hand sweep $from = round($sunRise *360 /24) +90; $to = round($sunSet *360 /24) +90 ; } imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight, $from, $to, $color_day, IMG_ARC_PIE); // from 3:00 > 5:30 if($markers) do_dial($from, $to, $color_black, $color_day); } if($showTime){ // 24hr hand sweep on static heavens $from = $gnoman -1; $to = $gnoman +1; imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight +2, $from, $to, $color_tick_time, IMG_ARC_PIE); } // resample the image to required size imagecopyresampled($imgFinal, $img, 0, 0, 0, 0, $width, $height, $drawWidth, $drawHeight); if(!$showTime){ // draw 'now' marker on revolving heavens $x1 = round($width /2); imagerectangle($imgFinal, ($x1-1), 0, ($x1+1), 13, $color_tick_time); } // output image imagepng($imgFinal,$pc_cacheFile,0,PNG_NO_FILTER); imagedestroy($imgFinal); imagedestroy($img); function do_dial($from, $to, $tick_colour, $event_colour){ // draw hour markers global $drawWidth, $drawHeight, $img; for($x = $from; $x <= $to; $x++){ if($x%15 == 0){ $tick_from = $x - 1; $tick_to = $x + 1; imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth, $drawHeight, $tick_from, $tick_to, $tick_colour, IMG_ARC_PIE); // from 3:00 > 5:30 } } imagefilledarc($img, round($drawWidth /2), round($drawHeight /2), $drawWidth-16, $drawHeight-16, $from, $to, $event_colour, IMG_ARC_PIE); // from 3:00 > 5:30 } // END function do_dial ?>