<?php

//
// copy image
//
$img= imagecreatefrompng($map);
$img_width= imagesx($img);
$img_hight= imagesy($img);

//
// colors
//
$black       = imagecolorallocate($img,   0,   0,   0);

$white       = imagecolorallocate($img, 255, 255, 255);
$yellow      = imagecolorallocate($img, 255, 255,   0);
$orange      = imagecolorallocate($img, 255, 170,   0);
$red_light   = imagecolorallocate($img, 255,  85,   0);
$red         = imagecolorallocate($img, 255,   0,   0);
$red_dark    = imagecolorallocate($img, 191,   0,   0);

$white_10    = imagecolorallocatealpha($img, 255, 255, 255, 20);
$yellow_10   = imagecolorallocatealpha($img, 255, 255,   0, 20);
$orange_10   = imagecolorallocatealpha($img, 255, 170,   0, 20);
$red_light_10= imagecolorallocatealpha($img, 255,  85,   0, 20);
$red_10      = imagecolorallocatealpha($img, 255,   0,   0, 20);
$red_dark_10 = imagecolorallocatealpha($img, 191,   0,   0, 20);

$gray        = imagecolorallocate($img, 143, 143, 143);
$black_95    = imagecolorallocatealpha($img, 0, 0, 0, 95);

$city_col    = imagecolorallocate($img,  63,  63,  63);

$active_col  = imagecolorallocate($img,  63, 191,  63);
$idle_col    = imagecolorallocate($img, 191, 191,  63);
$fault_col   = imagecolorallocate($img, 191,  63,  63);
$inactive_col= imagecolorallocate($img, 159, 159, 159);

//
// mercator projection
//
function mercator_proj ($lat)
{
  $lat= (float)$lat;
  return(log(tan(pi()/4.0+$lat/2.0)));
}

//
// times
//
$end_time= time();
$start_time= $end_time-$time_span;
$start_time_nsec= $start_time*1000000;

//
// strikes and stations path
//
$stations_filename= "https://$username:$password@data.blitzortung.org/Data/Protected/stations.json";
$strikes_linkname= "https://$username:$password@data.blitzortung.org/Data/Protected/last_strikes.php?north=$north&west=$west&east=$east&south=$south&time=$start_time_nsec&reverse=1";

//
// transform coordinates
//
function rad($a)
{
  return($a*pi()/180.0);
}
$north= rad($north);
$south= rad($south);
$west= rad($west);
$east= rad($east);

//
// copy strikes
//
file_put_contents($tmp_filename,file_get_contents($strikes_linkname));

//
// draw strikes
//
$strikes= 0;
$str_cnt= array (0, 0, 0, 0, 0, 0);
$file= @fopen($tmp_filename, 'r');
while ($line= fgets ($file)) {
  $strike= json_decode($line);
  $strike->time/=1000000000;
  if (($end_time-$time_span < $strike->time)&&($strike->time <= $end_time)&&
      ($west <= rad($strike->lon))&&(rad($strike->lon) <= $east)&&
      ($north >= rad($strike->lat))&&(rad($strike->lat) >= $south)) {

    $x= (int)(($img_width)*(rad($strike->lon)-$west)/($east-$west));
    $y= (int)($img_width*(mercator_proj($north)-mercator_proj(rad($strike->lat)))/($east-$west));

    if ($end_time-$strike->time < $time_span/6) {
      $str_cnt[0]++;
      $col= $white;
    }
    else if ($end_time-$strike->time < 2*$time_span/6) {
      $str_cnt[1]++;
      $col= $yellow;
    }
    else if ($end_time-$strike->time < 3*$time_span/6) {
      $str_cnt[2]++;
      $col= $orange;
    }
    else if ($end_time-$strike->time < 4*$time_span/6) {
      $str_cnt[3]++;
      $col= $red_light;
    }
    else if ($end_time-$strike->time < 5*$time_span/6) {
      $str_cnt[4]++;
      $col= $red;
    }
    else {
      $str_cnt[5]++;
      $col= $red_dark;
    }
//
// star
//
    imageline ($img, $x-3, $y+0, $x+3, $y+0, $col);
    imageline ($img, $x+0, $y-3, $x+0, $y+3, $col);
    imagefilledpolygon ($img, array ($x-1, $y-1, $x+1, $y-1, $x+1, $y+1, $x-1, $y+1), 4, $col);
//
// square
//
//    imagefilledpolygon ($img, array ($x-2, $y-2, $x+2, $y-2, $x+2, $y+2, $x-2, $y+2), 4, $col);

//
// ball
//
//    imagefilledpolygon ($img, array ($x-1, $y-2, $x+1, $y-2, $x+2, $y-1, $x+2, $y+1, $x+1, $y+2, $x-1, $y+2, $x-2, $y+1, $x-2, $y-1), 8, $col);

//
// flash
//
//    imagefilledpolygon ($img, array ($x, $y-10, $x+7, $y-10, $x+2, $y-3, $x+7, $y-3, $x-6, $y+10, $x-1, $y, $x-4, $y), 7, $col);
//    imagepolygon       ($img, array ($x, $y-10, $x+7, $y-10, $x+2, $y-3, $x+7, $y-3, $x-6, $y+10, $x-1, $y, $x-4, $y), 7, $black);

    $strikes++;
  }
}
fclose ($file);
unlink($tmp_filename);

//
// draw stations
//
if ($stations_type > 0) {
  $stations= json_decode(file_get_contents($stations_filename));
  foreach ($stations as $n => $station) {
    if (($west <= rad($station->longitude))&&(rad($station->longitude) <= $east)&&
        ($north >= rad($station->latitude))&&(rad($station->latitude) >= $south)) {

      $x= (int)(($img_width)*(rad($station->longitude)-$west)/($east-$west));
      $y= (int)($img_width*(mercator_proj($north)-mercator_proj(rad($station->latitude)))/($east-$west));

      $col= $fault_col;
      if ($station->controller_status == "10") { // inactive
        $col= $inactive_col;
      }
      if ($station->controller_status == "20") { // idle
        $col= $idle_col;
      }
      if ($station->controller_status == "30") { // active
        $col= $active_col;
      }

      imagefilledpolygon ($img, array ($x-3, $y-3, $x+3, $y-3, $x+3, $y+3, $x-3, $y+3), 4, $col);
      imagepolygon ($img, array ($x-3, $y-3, $x+3, $y-3, $x+3, $y+3, $x-3, $y+3), 4, $black);
      // imageline ($img, $x-1, $y, $x+1, $y, $black);
      // imageline ($img, $x, $y-1, $x, $y+1, $black);

      if ( ($stations_type == 2) && ($station->city) ) {
        imagestring ($img, 2, $x+5, $y-15, $station->city, $white);
      }
    }
  }
}

//
// draw cities
//
foreach ($cities_array as $city_line ) {
  $lat= strtok($city_line,';')*pi()/180.0;
  $lon= strtok(';')*pi()/180.0;
  $city = strtok(';');
  if (($lon >= $west)&&($lon <= $east)&&($lat <= $north)&&($lat >= $south)) {

    $x= (int)(($img_width)*($lon-$west)/($east-$west));
    $y= (int)($img_width*(mercator_proj($north)-mercator_proj($lat))/($east-$west));

    imagefilledpolygon ($img, array ($x-2, $y-2, $x-2, $y+2, $x+2, $y+2, $x+2, $y-2), 4, $city_col);
    imagepolygon ($img, array ($x-2, $y-2, $x-2, $y+2, $x+2, $y+2, $x+2, $y-2), 4, $gray);
    imagestring ($img, 2, $x+5, $y-15, $city, $city_col);
  }
}

//
// draw function
//
function draw_text ($img, $font, $x, $y, $text, $font_col, $bg_col)
{
  if ($x < 0) {
    $x+= imagesx($img)-strlen($text)*7;
  }
  if ($y < 0) {
    $y+= imagesy($img)-13;
  }
  imagefilledpolygon ($img, array ($x, $y, $x, $y+12, $x+strlen($text)*7, $y+12, $x+strlen($text)*7, $y), 4, $bg_col);
  imagestring ($img, $font, $x, $y, $text, $font_col);
}

//
// draw header lines
//
draw_text ($img, 3, 0, 0, "C Blitzortung.org contributors", $white, $black_95);
draw_text ($img, 3, -1, 0, date('Y-m-d T H:i:s',$end_time), $white, $black_95);
draw_text ($img, 3, -1, 26, sprintf ("Strikes:%5d",$strikes), $white, $black_95);

//
// draw legend
//
draw_text ($img, 3, 8, 24, sprintf ("  %3d Minutes",intval(1*$time_span/6/60)), $white, $black_95);
draw_text ($img, 3, 8, 37, sprintf ("  %3d",intval(2*$time_span/6/60)), $white, $black_95);
draw_text ($img, 3, 8, 50, sprintf ("  %3d",intval(3*$time_span/6/60)), $white, $black_95);
draw_text ($img, 3, 8, 63, sprintf ("  %3d",intval(4*$time_span/6/60)), $white, $black_95);
draw_text ($img, 3, 8, 76, sprintf ("  %3d",intval(5*$time_span/6/60)), $white, $black_95);
draw_text ($img, 3, 8, 89, sprintf ("  %3d",intval(6*$time_span/6/60)), $white, $black_95);

$x= 10;
$y= 28;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $white);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);
$y+= 13;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $yellow);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);
$y+= 13;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $orange);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);
$y+= 13;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $red_light);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);
$y+= 13;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $red);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);
$y+= 13;
imagefilledpolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $red_dark);
imagepolygon ($img, array ($x, $y, $x, $y+5, $x+5, $y+5, $x+5, $y), 4, $gray);

//
// draw statistics
//
function draw_statistic ($img, $font, $x, $y, $text, $width, $font_col, $bg_col)
{
  if ($x < 0) {
    $x+= imagesx($img);
  }
  if ($y < 0) {
    $y+= imagesy($img);
  }
  imagefilledpolygon ($img, array ($x, $y, $x, $y-$width, $x+12, $y-$width, $x+12, $y), 4, $bg_col);
  imagestringup ($img, $font, $x, $y-$width+strlen($text)*8, $text, $font_col);
}

$x= 3;
$y= imagesy($img)-4;
imageline ($img, $x, $y, $x, $y-58, $white);
imageline ($img, $x, $y, $x+81, $y, $white);
$x++;
$y--;

$max= 0;
for ($i= 0; $i < 6; $i++) {
  if ($str_cnt[$i] > $max) {
    $max= $str_cnt[$i];
  }
}

$str_cnt_disp= array ("", "", "", "", "", "");
for ($i= 0; $i < 6; $i++) {
  if ($str_cnt[$i] == $max) {
    $str_cnt_disp[$i]= $max;
  }
}

if( $max > 0) {
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[5],  55*$str_cnt[5]/$max, $black, $red_dark_10);
  $x+=13;
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[4],  55*$str_cnt[4]/$max, $black, $red_10);
  $x+=13;
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[3],  55*$str_cnt[3]/$max, $black, $red_light_10);
  $x+=13;
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[2],  55*$str_cnt[2]/$max, $black, $orange_10);
  $x+=13;
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[1],  55*$str_cnt[1]/$max, $black, $yellow_10);
  $x+=13;
  draw_statistic ($img, 3, $x, $y, $str_cnt_disp[0],  55*$str_cnt[0]/$max, $black, $white_10);
}

//
// output image
//
header("Content-type: image/png");
imagepng($img);

//
// free memory
//
imagedestroy($img);

?>