<?php

  header ("Content-type: image/png");
  
  $coords = $_GET['coords'];
  $number = $_GET['number'];
  $tiles = $_GET['tiles'];
  $layer = $_GET['layer'];

  // Achtung: Wegen Webangriffen sollten in den uebergebenen
  // Parametern keine Sonderzeichen wie .. / \ etc. enthalten
  // sein, da sonst ein Dateiname wie ../../home/hans etc.
  // erzeugt werden koennte.
  $coords = ereg_replace("[^A-Za-z0-9_,]", "", $coords);
  $number = ereg_replace("[^A-Za-z0-9_,]", "", $number);
  $tiles = ereg_replace("[^A-Za-z0-9_,]", "", $tiles);
  $layer = ereg_replace("[^A-Za-z0-9_,]", "", $layer);
    
  $coords_array = explode(",", $coords);
  $number_array = explode(",", $number);
  $tiles_array = explode(",", $tiles);

  $width = $coords_array[2] - $coords_array[0];
  $height = $coords_array[3] - $coords_array[1];

  $numberx = $number_array[0];
  $numbery = $number_array[1];

  $img = imagecreatetruecolor($numberx * 256, $numbery * 256);
  
  $index = 0;
  for ($j = 0; $j < $numbery; $j++) {
    for ($i = 0; $i < $numberx; $i++) {
      $xyz_array = explode("_", $tiles_array[$index]);
      $z = $xyz_array[0];
      $x = $xyz_array[1];
      $y = $xyz_array[2];
      if ($z <= 11) $tmp = imagecreatefromjpeg("http://maps-for-free.com/layer/relief/z".$z."/row".$y."/".$z."_".$x."-".$y.".jpg");
      else $tmp = imagecreatefrompng("http://tile.openstreetmap.org/".$z."/".$x."/".$y.".png");
      imagecopy($img, $tmp, $i * 256, $j * 256, 0, 0, 256, 256);
      imagedestroy($tmp);
      $index++;
    }
  }
  
  // layer hinzufuegen
  if ($layer != "") {
    $layer_array = explode(",", $layer);
    for ($k = 0; $k < sizeof($layer_array); $k++) {
      $index = 0;
      for ($j = 0; $j < $numbery; $j++) {
        for ($i = 0; $i < $numberx; $i++) {
          $xyz_array = explode("_", $tiles_array[$index]);
          $z = $xyz_array[0];
          $x = $xyz_array[1];
          $y = $xyz_array[2];
          if ($layer_array[$k] == "country")
            $file = "../layer/".$layer_array[$k]."/z".$z."/row".$y."/".$z."_".$x."-".$y.".png";
          // other url
          elseif ($layer_array[$k] == "city")
            $file = "../layer/".$layer_array[$k]."/".$z."/".$x."/".$y.".png";
          else 
            $file = "../layer/".$layer_array[$k]."/z".$z."/row".$y."/".$z."_".$x."-".$y.".gif";
          if ($handle = @fopen ($file, "rb")) { // test ob file existiert...
            if ($layer_array[$k] == "country" || $layer_array[$k] == "city")
              $tmp = imagecreatefrompng($file);
            else 
              $tmp = imagecreatefromgif($file);
            imagecopy($img, $tmp, $i * 256, $j * 256, 0, 0, 256, 256);
            imagedestroy($tmp);
          }
          $index++;
        }
      }
    }
  }
  
  $offsetx = $coords_array[0] - floor($coords_array[0] / 256) * 256;
  $offsety = $coords_array[1] - floor($coords_array[1] / 256) * 256;
  
  $res = imagecreatetruecolor($width, $height);  
  imagecopy($res, $img, 0, 0, $offsetx, $offsety, $width, $height);  
  imagepng($res);
  
  imagedestroy($img);
  imagedestroy($res);

?>