-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtile.php
More file actions
188 lines (139 loc) · 5.45 KB
/
tile.php
File metadata and controls
188 lines (139 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
error_reporting(E_ALL);
define('TILE_SIZE', 256);
define('TILE_FILENAME', "tile_%s_%s-%s.png");
$mapName = $_POST['mapname'];
define('OUTPUT_DIR', $mapName);
function SplitTilesRecursive($image, $level){
//echo "<br/>SplitTilesRecursive - level: " .$level;
$mapWidth = GetMapWidth($level);
//echo "<br/>MapWidth:". $mapWidth;
$tilesOnSide = $mapWidth / TILE_SIZE;
//echo "<br/>tiles on side:". $tilesOnSide."<br/>";
$resized = ResizeImage($image, $mapWidth);
imagedestroy($image);
for ($x = 0; $x < $tilesOnSide; $x++)
for ($y = 0; $y < $tilesOnSide; $y++)
CropAndSaveTile($resized, $x, $y, $level);
if ($level > 0)
SplitTilesRecursive($resized, $level - 1);
}
function GetMapWidth($level){
return TILE_SIZE * (int)pow(2,$level);
}
function CropAndSaveTile($image, $x, $y, $level)
{
//echo "<br/>level " . $level;
$dst_image = imagecreatetruecolor(TILE_SIZE, TILE_SIZE);
imagecopyresampled($dst_image, $image, 0, 0, $x * TILE_SIZE, $y * TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE);
$filename = sprintf(TILE_FILENAME, $level, $x, $y);
// the Portable Network Graphics (PNG) encoder is used implicitly
imagepng($dst_image, OUTPUT_DIR."/".$filename);
imagedestroy($dst_image);
//echo "<br/>Processed " . $filename;
//echo "<br/><img src='".OUTPUT_DIR."/".$filename."'/>";
}
function ResizeImage($toResize, $size)
{
$dst_image = imagecreatetruecolor($size, $size);
imagecopyresampled($dst_image, $toResize, 0, 0, 0, 0, $size, $size, imagesx($toResize), imagesy($toResize));
return $dst_image;
}
if(empty($_POST['levels']) || $_POST['levels'] < 1 || $_POST['levels'] > 5 || empty($_FILES))
{
echo "Invalid Input";
}else{
$output = "tile generator";
$levels = $_POST['levels'];
$output .= "<br/>level: " . $levels;
//echo "<img src='".$_POST['filename']."'/>";
if(!file_exists('./'.OUTPUT_DIR))
mkdir('./'.OUTPUT_DIR);
else
{
if ($handle = opendir('./'.OUTPUT_DIR)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != ".DS_STORE") {
unlink('./'.OUTPUT_DIR.'/'.$file);
}
}
closedir($handle);
}
}
$original = imagecreatefrompng($_FILES['filename']['tmp_name']);
SplitTilesRecursive($original, $levels);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Maps API Demos</title>
</head>
<body>
<?php $output; ?>
<div id="croft-map" style="width:100%;height:600px;margin:10px auto;border:2px solid #000;"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false"></script>
<script type="text/javascript">
/* <![CDATA[ */
// Google Maps Demo
//////////////////////////////////
var Demo = Demo || {};
Demo.ImagesBaseUrl = 'http://localhost/maptiles/';
// CroftMap class
//////////////////////////////////
Demo.CroftMap = function (container) {
// Create map
this._map = new google.maps.Map(container, {
zoom: 0,
center: new google.maps.LatLng(0, -20),
mapTypeControl: false
});
// Set custom tiles
this._map.mapTypes.set('<?php echo $mapName; ?>', new Demo.ImgMapType('<?php echo $mapName; ?>', '#4E4E4E'));
this._map.setMapTypeId('<?php echo $mapName; ?>');
};
// ImgMapType class
//////////////////////////////////
Demo.ImgMapType = function (theme, backgroundColor) {
this.name = this._theme = theme;
this._backgroundColor = backgroundColor;
};
Demo.ImgMapType.prototype.tileSize = new google.maps.Size(256, 256);
Demo.ImgMapType.prototype.minZoom = 0;
Demo.ImgMapType.prototype.maxZoom = <?php echo $_POST['levels']; ?>;
Demo.ImgMapType.prototype.getTile = function (coord, zoom, ownerDocument) {
var tilesCount = Math.pow(2, zoom);
if (coord.x >= tilesCount || coord.x < 0 || coord.y >= tilesCount || coord.y < 0) {
var div = ownerDocument.createElement('div');
div.style.width = this.tileSize.width + 'px';
div.style.height = this.tileSize.height + 'px';
div.style.backgroundColor = this._backgroundColor;
return div;
}
var img = ownerDocument.createElement('IMG');
img.width = this.tileSize.width;
img.height = this.tileSize.height;
var randomnumber=Math.floor(Math.random()*11);
img.src = Demo.Utils.GetImageUrl(this._theme + '/tile_' + zoom + '_' + coord.x + '-' + coord.y + '.png?v='+randomnumber);
return img;
};
// Other
//////////////////////////////////
Demo.Utils = Demo.Utils || {};
Demo.Utils.GetImageUrl = function (image) {
return Demo.ImagesBaseUrl + image;
};
Demo.Utils.SetOpacity = function (obj, opacity /* 0 to 100 */ ) {
obj.style.opacity = opacity / 100;
obj.style.filter = 'alpha(opacity=' + opacity + ')';
};
// Map creation
//////////////////////////////////
google.maps.event.addDomListener(window, 'load', function () {
var croftMap = new Demo.CroftMap(document.getElementById('croft-map'));
});
/* ]]> */
</script>
</body>
</html>
<?php } ?>