<?php
$fpwe = fopen("http://webservice.weatherzone.com.au/rss/wx.php?u=13145&lt=aploc&lc=" . $_GET['lc'] . "&obs=1&fc=1","rb") or die("unable to connect to WeatherZone");

if(!$fpwe) {
    echo "Oopsies!";
}

$sourcedata = '';
while (!feof($fpwe)) {
  $sourceData .= fread($fpwe, 8192);
}

fclose($fpwe);

$obsData=$sourceData;
$forecastData=$sourceData;

if (preg_match("/<title.*>Current weather for (.*)<\/title>/smU", $obsData, $matches)) {
   $whereTime = $matches[1];
}

$obsData=substr($obsData,strpos($obsData,"<![CDATA[")+9);
$obsData=substr($obsData,0,strpos($obsData,"]]>"));

$currently=substr($obsData,strpos($obsData,"Temperature")+17);
$currently=substr($currently,0,strpos($currently,"&#176;C"));

$forecastData=substr($forecastData,strpos($forecastData,"<![CDATA[")+9);
$forecastData=substr($forecastData,strpos($forecastData,"<![CDATA[")+9);
$forecastData=substr($forecastData,0,strpos($forecastData,"]]>"));

$forecastDatadays=split("<br /><br />",$forecastData);

//Find out the next 2 days
if (preg_match("/<b.*>(.*)<\/b>/smU", $forecastDatadays[0], $matches)) {
    $wDay1=$matches[1];
}
if (preg_match("/<b.*>(.*)<\/b>/smU", $forecastDatadays[1], $matches)) {
    $wDay2=$matches[1];
}

//Get the URL of the weather icon
if (preg_match("/<img src=\"(.*)\">/smU", $forecastDatadays[0], $matches)) {
    $wIcon1URL=$matches[1];
}
if (preg_match("/<img src=\"(.*)\">/smU", $forecastDatadays[1], $matches)) {
    $wIcon2URL=$matches[1];
}

//cutout some of the data we don't need to simplify extracting forecast temps and descript
$forecastDatadays[0]=substr($forecastDatadays[0],(strpos($forecastDatadays[0],$wIcon1URL)+strlen($wIcon1URL)+8));
$forecastDatadays[1]=substr($forecastDatadays[1],(strpos($forecastDatadays[1],$wIcon2URL)+strlen($wIcon2URL)+8));

$forecast1=split("<br />",ereg_replace("[\n\r]", "", $forecastDatadays[0]));
$forecast2=split("<br />",ereg_replace("[\n\r]", "", $forecastDatadays[1]));

$wDescr1=$forecast1[0];
$wTemp1=str_replace("&#176;C","°c", $forecast1[1]);

$wDescr2=strip_tags($forecast2[0]);
$wTemp2=str_replace("&#176;C","°c", $forecast2[1]);


$width = 295;
$height = 140;

$im = imagecreatetruecolor($width, $height) or die('Cannot Initialize new GD image stream');

$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey0 = imagecolorallocate($im, 64, 64, 64);
$grey1 = imagecolorallocate($im, 128, 128, 128);
$grey2 = imagecolorallocate($im, 192, 192, 192);

imagefill($im, 0, 0, $white);
imagestring($im, 4, 5, 0,  $whereTime, $black);

$font = imageloadfont('Arial-72.gdf');
imagestring($im, $font,25, 15, $currently . "°c", $black);


$wIcon1=imagecreatefromgif($wIcon1URL);
imagestring($im, 5,50, 90, $wDay1, $black);
imagecopy($im,$wIcon1,0,90,0,0,imagesx($wIcon1),imagesy($wIcon1));
imagestring($im, 4,35, 109, $wTemp1, $black);
imagestring($im, 2,10, 126, $wDescr1, $black);

$wIcon2=imagecreatefromgif($wIcon2URL);
imagestring($im, 5,200, 90, $wDay2, $black);
imagecopy($im,$wIcon2,150,90,0,0,imagesx($wIcon2),imagesy($wIcon2));
imagestring($im, 4,185, 109, $wTemp2, $black);
imagestring($im, 2,160, 126, $wDescr2, $black);


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