class WeatherPlugin implements IPlugin
{
private $dbconn = NULL;
private $config = NULL;
public function __construct($dbconn, $config) {
$this->dbconn = $dbconn;
$this->config = $config;
}
public function doUpdate() {
// download google weather XML into local file for caching
$city = urlencode( $this->config['city'] );
$curl = curl_init();
$file = fopen("cache/weather.xml", "w");
if ($file)
{
curl_setopt($curl, CURLOPT_URL, utf8_encode("http://www.google.de/ig/api?weather=$city&oe=UTF8"));
curl_setopt($curl, CURLOPT_FILE, $file);
curl_exec($curl);
curl_close($curl);
fclose($file);
}
}
public function doOutput($image, $style, $updateData, &$yoffset) {
$filename = 'cache/weather.xml';
if ($updateData || !file_exists($filename))
$this->doUpdate();
if(file_exists($filename) && (filesize($filename) > 0)) {
$xml = simplexml_load_file($filename);
if($xml) {
// parse weather data
// ===================
// current conditions
$current_condition = $xml->weather->current_conditions->condition['data'];
$current_temp = $xml->weather->current_conditions->temp_c['data'];
$current_humidity = $xml->weather->current_conditions->humidity['data'];
$current_wind_condition = $xml->weather->current_conditions->wind_condition['data'];
$night = !isDaylight();
$current_icon = $this->getLocalWeatherImage($xml->weather->current_conditions->icon['data'], $night);
$wicon = ImageCreateFromPNG ( $current_icon );
ImageCopy($image, $wicon, imagesx($image)-190, 5, 0, 0, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 280,
'align' => ALIGN_RIGHT
);
//$text = $current_temp."°C";
//imagettftextboxopt($image, 24, 0, imagesx($image)-300, 105, $style['textcolor'], $style['font'], $text, $opt);
//
//$text = "Wetter für ";
//imagettftextboxopt($image, 15, 0, imagesx($image)-300, 148, $style['textcolor'], $style['font'], $text, $opt);
// Google
$text = "Aktuell: $current_condition\n$current_humidity\n".str_replace('Windgeschwindigkeiten von ', '', $current_wind_condition);
imagettftextboxopt($image, 15, 0, imagesx($image)-300, 145, $style['textcolor'], $style['font'], $text, $opt);
// display sunrise and sunset
date_default_timezone_set('Europe/Berlin');
$longitude = 52.17611111; // longitude
$latitude = 8.12027778; // latitude
$sunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, $longitude, $latitude, 90);
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, $longitude, $latitude, 90);
$text = "Sonnenaufgang: ".$sunrise." - Sonnenuntergang: ".$sunset;
$opt = array(
'width' => 270,
'align' => ALIGN_RIGHT
);
imagettftextboxopt($image, 10, 0, imagesx($image)-280, 575, $style['textcolor'], $style['font'], $text, $opt);
// forecast for today and next 3 days
for ($i = 0; $i <= 3; $i++) {
// pixel offset for placing day 0-3 in different rows from top to bottom
if (imagesy($image) <= 500) {
$offset = 225+(85*$i); // smaller spacing for low resolution displays (vertical=480px)
} else {
$offset = 235+90*$i; // normal spacing for high resolution diplays (vertical=600px)
}
if ($offset > (imagesy($image)-80))
break; // offset out of range, skip output of further weather forecast days
// format data
$day = $xml->weather->forecast_conditions[$i]->day_of_week['data'];
if ($i == 0)
$day = 'Heute';
if ($i == 1)
$day = 'Morgen';
$low = $xml->weather->forecast_conditions[$i]->low['data'];
$high = $xml->weather->forecast_conditions[$i]->high['data'];
$condition = $xml->weather->forecast_conditions[$i]->condition['data'];
$icon = $this->getLocalWeatherImage($xml->weather->forecast_conditions[$i]->icon['data'], false);
// output
$wicon = ImageCreateFromPNG ( $icon );
ImageCopyResampled($image, $wicon, imagesx($image)-95, $offset+5, 0, 0, imagesx($wicon)/2, imagesy($wicon)/2, imagesx($wicon), imagesy($wicon));
ImageDestroy($wicon);
$opt = array(
'width' => 150,
'align' => ALIGN_RIGHT
);
$text = $day."\n".$high."° | ".$low."°\n".$condition;
imagettftextboxopt($image, 15, 0, imagesx($image)-240, $offset, $style['textcolor'], $style['font'], $text, $opt);
}
}
}
}
// replace google specific path with local path and image file names
private function getLocalWeatherImage($googleWeatherImage, $night) {
$localImagePath = 'resources/weather/' . substr(strrchr($googleWeatherImage, "/"), 1 ); // get all chars after last slash
$localImagePath = str_replace('-40.gif', '.png', $localImagePath);
$localImagePath = str_replace('.gif', '.png', $localImagePath);
$localImagePathNight = str_replace('.png', '_night.png', $localImagePath);
// if parameter $night is true and night image exist use it
if ($night && file_exists($localImagePathNight))
$localImagePath = $localImagePathNight;
else if (!file_exists($localImagePath))
// if daylight image doesn't exist display N/A image
$localImagePath = 'resources/weather/na.png';
return $localImagePath;
}
}