forked from samsargent/Google-ImageMap-Tile-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaxTiles.php
318 lines (251 loc) · 8.5 KB
/
ajaxTiles.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
ini_set("memory_limit","128M");
error_reporting(E_ALL);
define('TILE_SIZE', 256);
define('TILE_FILENAME', "tile_%s_%s-%s.png");
$mapName = $_POST['mapname'];
define('OUTPUT_DIR', "uploads/".$mapName);
$levels = $_POST['levels'] - 1;
$tileCount = 0;
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)
{
global $tileCount;
//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);
$tileCount++;
//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($_POST['filename']))
{
echo "Invalid Input";
}else{
$output = "tile generator";
$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($_POST['filename']);
SplitTilesRecursive($original, $levels);
/*
var myLatlng = new google.maps.LatLng(0, 0);
*/
ob_start();
?>
<script>
var newMapTypeOptions = {
getTileUrl: function(coord, zoom) {
var normalizedCoord = getNormalizedCoord(coord, zoom);
if (!normalizedCoord) {
return null;
}
var bound = Math.pow(2, zoom);
return "http://unitseven.com.au/maptiles/uploads/<?php echo $mapName;?>/tile_" + zoom + "_" + normalizedCoord.x + "-" + normalizedCoord.y + ".png";
},
tileSize: new google.maps.Size(256, 256),
isPng: true,
maxZoom: <?php echo $levels;?>,
minZoom: 0,
name: "<?php echo $mapName;?>"
};
var newMapType = new google.maps.ImageMapType(newMapTypeOptions);
// Normalizes the coords that tiles repeat across the x axis (horizontally)
// like the standard Google map tiles.
function getNormalizedCoord(coord, zoom) {
var y = coord.y;
var x = coord.x;
// tile range in one direction range is dependent on zoom level
// 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
var tileRange = 1 << zoom;
// don't repeat across y-axis (vertically)
if (y < 0 || y >= tileRange) {
return null;
}
// repeat across x-axis
if (x < 0 || x >= tileRange) {
x = (x % tileRange + tileRange) % tileRange;
}
return {
x: x,
y: y
};
}
function initNewMap() {
map = null;
$("#map_canvas").remove();
$("#mapContainer").html('<div id="map_canvas"></div>');
var myLatlng = new google.maps.LatLng(0, 0);
var myOptions = {
zoom: 1,
center: myLatlng,
mapTypeControlOptions: {
mapTypeIds: ["<?php echo $mapName;?>"]
}
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.mapTypes.set('<?php echo $mapName;?>', newMapType);
map.setMapTypeId('<?php echo $mapName;?>');
}
</script>
<?php
$js = ob_get_clean();
ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0,user-scalable=no" />
<meta charset="utf-8" />
<title>Google Maps - Custom ImageMap Type Demo</title>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:600px;height:360px;"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var customMapTypeOptions = {
getTileUrl: function(coord, zoom) {
var normalizedCoord = getNormalizedCoord(coord, zoom);
if (!normalizedCoord) {
return null;
}
var bound = Math.pow(2, zoom);
/*Edit this URL to where you upload your tiles...*/
return "http://unitseven.com.au/maptiles/uploads/<?php echo $mapName;?>/tile_" + zoom + "_" + normalizedCoord.x + "-" + normalizedCoord.y + ".png";
},
tileSize: new google.maps.Size(256, 256),
isPng: true,
maxZoom: <?php echo $levels;?>,
minZoom: 0,
name: "<?php echo $mapName;?>"
};
var customMapType = new google.maps.ImageMapType(customMapTypeOptions);
// Normalizes the coords that tiles repeat across the x axis (horizontally)
// like the standard Google map tiles.
function getNormalizedCoord(coord, zoom) {
var y = coord.y;
var x = coord.x;
// tile range in one direction range is dependent on zoom level
// 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
var tileRange = 1 << zoom;
// don't repeat across y-axis (vertically)
if (y < 0 || y >= tileRange) {
return null;
}
// repeat across x-axis
if (x < 0 || x >= tileRange) {
x = (x % tileRange + tileRange) % tileRange;
}
return {
x: x,
y: y
};
}
function initialize() {
var myLatlng = new google.maps.LatLng(0, 0);
var myOptions = {
zoom: 1,
center: myLatlng,
mapTypeControlOptions: {
mapTypeIds: ["<?php echo $mapName;?>"]
}
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.mapTypes.set('<?php echo $mapName;?>', customMapType);
map.setMapTypeId('<?php echo $mapName;?>');
}
</script>
</body>
</html>
<?php
$code = ob_get_clean();
$fp = fopen(OUTPUT_DIR.'/index.html', 'w+');
fwrite($fp, $code);
fclose($fp);
$path="uploads/".$mapName;
$zip = new ZipArchive;
$zip->open("uploads/".$mapName.'-files.zip', ZipArchive::CREATE);
if (false !== ($dir = opendir($path)))
{
while (false !== ($file = readdir($dir)))
{
if ($file != '.' && $file != '..')
{
$zip->addFile($path.DIRECTORY_SEPARATOR.$file);
}
}
}
else
{
die('Can\'t read dir');
}
$zip->close();
ob_start();
?>
<div id="newMap-id" stlye="width:100%; height:360px;"></div>
<h1>Done!</h1>
<h3>Your Map Tiles & Google Maps HTML & JS Code have been packaged into a zip archive for you to download and install on your own server. Download & follow the instructions in the comments of the code to start using your Custom ImageMap Google Map.</h3>
<p><a href="./uploads/<?php echo $mapName.'-files.zip' ?>" class="downloadLink"><img src="img/zip-icon.png" alt="Zip Icon"> Download Files</a> (<?php echo $tileCount; ?> png tiles & 1 html file). </p>
<p class="info">Files are removed every night at midnight. Be sure to download before then. You have:
<?php
$h = 24 - date("H")/1;
$m = 60 - date("i")/1;
$s = 60 - date("s")/1;
$h = (($h <10)?"0":"").$h;
$m = (($m <10)?"0":"").$m;
$s = (($s <10)?"0":"").$s;
echo "$h:$m:$s";
?>
</p>
<p>View your map in a <a href="./uploads/<?php echo $mapName;?>/index.html">new window</a></p>
<button id="resetBtn">Reset & Start Again</button>
<br/><br/>
<!--
<h2>Instructions for use</h2>
<p>Blah blah blah, figure it out for yourself...</p>
<div id="source"></div>
-->
<?php
$html = ob_get_clean();
$result = array('success'=>true, 'code'=> $code, 'html'=> $html, 'js'=> $js, 'id'=> "Some sort of session id...");
echo json_encode($result);
?>
<?php } ?>