<?php
date_default_timezone_set("EET");

$clientrawfile = "C:\WUHUOut\currdat.lst";
$clientraw = get_raw($clientrawfile);
$data=parse_ini_file($clientrawfile,true);
$font="mtcorsva.ttf";
$background = "Retkeilymaja_banneri.png";         // PNG or JPG, 520x70 px
$windfolder = "wind_icons/";
$conditionsfolder = "dotvoid_banner/";
$shadow = true;
$offset = "+2";
$summeroffset = "+3";
$lat = "60.384";
$long = "23.668";

$crage = filemtime($clientrawfile);
$dataage = (time()-$crage);
$lines = get_raw($clientrawfile);
if($dataage < 600) {
$baro = $data['pressure_relative']['hpa']." hPa";
$windimg = $data['wind_direction']['deg'];
$wind = $data['wind_speed_average']['mps']. " m/s";
$hum = round($data['outdoor_humidity']['percent'])." %";
$temp = $data['outdoor_temperature']['deg_C']." °C";
$gust = $data['wind_gust']['mps']. " m/s";
$rain = $data['rain_daily']['mm']." mm";
$barotrend = $data['weather_text']['weather_tendency'];

$now = time();
$dl = date("I", $now);
if ($dl == 1) { $offset = $summeroffset; }
$dayornight = sunstuff();
if($pic == 0 && $dew < 0) { $weatherimg = "25"; } // Snow
else if($pic == 0 && $dew > 1.8) { $weatherimg = "09"; } // Rain
else if($pic == 0) { $weatherimg = "23"; } // Sleet
else if($pic == 1) { $weatherimg = "19"; }
else if($dayornight == "night" && $pic == "2") { $weatherimg = "1"; }
else { $weatherimg = "0"; }

} else {
$temp = 'Ei tuoreita';
$rain = 'tietoja saatavilla.';
}

#$icon = $windfolder.$windimg.'.png';
#$icon2 = $conditionsfolder.$weatherimg.'.png';

$date = date('d.m.Y H:i');

$image = $background;

$ext = substr($image, -3);

if(strtolower($ext) == "jpg") {
    if (!$im2 = imagecreatefromjpeg($image)) {
        echo "Error opening $image!"; exit;
    }
} else if(strtolower($ext) == "png") {
    if (!$im2 = imagecreatefrompng($image)) {
        echo "Error opening $image!"; exit;
    }
} else {
    die('Image not found.');
}

$grey = imagecolorallocate ($im2, 230, 230, 230);
$black = imagecolorallocate ($im2, 0,0,0);

$image_p = imagecreatetruecolor(520,70);
imagecopyresampled($image_p, $im2, 0, 0, 0, 0, 520, 70, 520, 70);

if($dataage < 600) {
$windicon = imagecreatefrompng($icon);
#$weathericon = imagecreatefrompng($icon2);
imagecopyresampled($image_p, $windicon, 497, 25, 0, 0, 14, 14, 14, 14);
#imagecopyresampled($image_p, $weathericon, 310, 4, 0, 0, 77, 64, 77, 64);
if($shadow == true) {
imagettftext($image_p, 10, 0, 422, 12, $black, $font, $date);
imagettftext($image_p, 11, 0, 391, 35, $black, $font, $temp);
imagettftext($image_p, 11, 0, 391, 49, $black, $font, $rain);
imagettftext($image_p, 11, 0, 391, 63, $black, $font, $hum);
imagettftext($image_p, 11, 0, 448, 35, $black, $font, $wind);
imagettftext($image_p, 11, 0, 448, 49, $black, $font, $baro);
imagettftext($image_p, 11, 0, 448, 63, $black, $font, $barotrend);
}
imagettftext($image_p, 10, 0, 421, 11, $grey, $font, $date);
imagettftext($image_p, 11, 0, 390, 34, $grey, $font, $temp);
imagettftext($image_p, 11, 0, 390, 48, $grey, $font, $rain);
imagettftext($image_p, 11, 0, 390, 62, $grey, $font, $hum);
imagettftext($image_p, 11, 0, 447, 34, $grey, $font, $wind);
imagettftext($image_p, 11, 0, 447, 48, $grey, $font, $baro);
imagettftext($image_p, 11, 0, 447, 62, $grey, $font, $barotrend);

} else {
if($shadow == true) {
imagettftext($image_p, 12, 0, 391, 39, $black, $font, $temp);
imagettftext($image_p, 12, 0, 391, 51, $black, $font, $rain);
}
imagettftext($image_p, 12, 0, 390, 38, $grey, $font, $temp);
imagettftext($image_p, 12, 0, 390, 50, $grey, $font, $rain);
}

header('Content-Type: image/png');
imagepng($image_p);
imagedestroy($image_p);
imagedestroy($windicon);
imagedestroy($weathericon);


function sunstuff() {
global $offset,$lat,$long;
$zenith=90+33/60;
$sunri = date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$sunse = date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$sunrise_epoch = date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, $lat, $long, $zenith, $offset);
$sunset_epoch  = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $lat, $long, $zenith, $offset);
$time_epoch = time(); // time now

if ($time_epoch >= $sunset_epoch or $time_epoch <= $sunrise_epoch) {
$dayornight = 'night';
} else {
$dayornight = 'day';
}
$ret = $dayornight;
return $ret;
}

function get_raw( $rawfile ) {
        $rawdata = implode('', file($rawfile));
        $rawdata = explode("\r", $rawdata);
        return $rawdata;
}

function ktsToMph ($kts, $prec) {
          $prec = (integer)$prec;
          $inch = (float)$kts * 0.514444444;
          return round($inch, 1);
}

function windDir ($winddir)
// Given the wind direction, return the text label
// for that value.  16 point compass
{
  if (!isset($winddir)) {
    return "---";
  }
$windlabel = array ("N","NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S",
   "SSW","SW", "WSW", "W", "NWN", "NW", "NNW");
$dir = $windlabel[ (int) ($winddir / 22.5) ];
return "$dir";
}

exit;
?> 