<?php
/*
 * Pall Thayer (no copyright)
 *
 * Much of this code is based on code from Klokan Petr Pridal's 
 * globalmaptiles.py (copyright 2008).

###############################################################################
# Copyright (c) 2008 Klokan Petr Pridal. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################

 * http://www.maptiler.org/google-maps-coordinate-system-projection-epsg-900913-3785/globalmaptiles.py.html
 *
 */
$zoom = $_GET['zoom'];
$lon = $_GET['lon'];
$lat = $_GET['lat'];
$x_tiles = $_GET['x_tiles'];
$y_tiles = $_GET['y_tiles'];
$tile_size = 256;
$pi_val = pi();
$initial_resolution = 2 * $pi_val * 6378137 / $tile_size;
$origin_shift = 2 * $pi_val * 6378137 / 2.0;
$resolution = $initial_resolution / intval(pow(2.0, floatval($zoom)));

$mx = $lon * $origin_shift / 180.0;
$my = log(tan((90 + $lat) * $pi_val / 360.0)) / ($pi_val / 180.0);
$my = $my * $origin_shift / 180.0;

$px = ($mx + $origin_shift) / $resolution;
$py = ($my + $origin_shift) / $resolution;

$tx = intval(ceil($px / $tile_size) - 1);
$ty = intval(ceil($py / $tile_size) - 1);

$ty = (intval(pow(2.0, floatval($zoom))) - 1) - $ty;
?>
<table border="0" cellspacing="0" cellpadding="0">
<?php
for($y = 0;$y < $y_tiles;$y++){
	print "<tr>";
	for($x = 0;$x < $x_tiles;$x++){
		$server = rand(0, 3);
		print "<td><img src=\"http://khm".$server.".google.com/kh?v=87&hl=en&x=".($tx + $x)."&y=".($ty + $y)."&z=".$zoom."\"/></td>";
	}
	print "</tr>";
}
?>
</table>