-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.php
More file actions
219 lines (158 loc) · 7.28 KB
/
example.php
File metadata and controls
219 lines (158 loc) · 7.28 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
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
<?php
error_reporting(E_ALL | E_STRICT);
// An example with include tile map by using Leaflet
class Example {
private $mode;
private $root;
private $mapFile = 'test';
private $mapDir = 'tiles/'; // back slash at the end is important
// information about current or old uploaded map
private $mapZoomMax = false;
private $mapExist = false;
private $log = '';
public function __construct() {
$this->root = dirname(__FILE__) . '/';
require_once($this->root . 'GDMapTileGenerator.class.php');
// restore tiles info - I usually save info about map zoom in database
$mapDirAbs = $this->root . $this->mapDir;
if (is_dir($mapDirAbs)) {
$zoomMax = 0;
while(is_dir($mapDirAbs . $zoomMax)) $zoomMax++;
if ($zoomMax) $zoomMax--;
$this->mapZoomMax = $zoomMax;
$this->mapExist = true;
}
}
public function log($err) {
$this->log .= $err . '<br>';
}
public static function deleteDir($dirPath, $removeDir = true)
{
if (!is_dir($dirPath))
return;
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file))
self::deleteDir($file);
else
unlink($file);
}
if ($removeDir and is_dir($dirPath)) rmdir($dirPath);
}
public function exec() {
$this->mode = empty($_GET['mode']) ? 'default' : $_GET['mode'];
if ($this->mode == 'upload') {
if (!empty($_POST['image-upload'])) {
if (empty($_FILES['image-file']['tmp_name']) or !is_uploaded_file($_FILES['image-file']['tmp_name'])) $this->show('Upload file fail');
$extension = strtolower(substr($_FILES['image-file']['name'], 1 + strrpos($_FILES['image-file']['name'], ".")));
$this->deleteDir($this->root . $this->mapDir, false);
$file = $this->root . $this->mapFile . '.' . $extension;
if (file_exists($file)) unlink($file);
if (!move_uploaded_file($_FILES['image-file']['tmp_name'], $file)) $this->show('Move uploaded file fail');
chmod($file, 0664);
$generator = new Kelly\GDMapTileGenerator($file, false, true);
$generator->set('callback', array($this, 'log')); // '\Kelly\Tool::log'
$generator->set('ext', 'png');
$generator->set('storage', $this->mapDir);
ini_set("memory_limit", "-1");
ini_set("max_execution_time", 0);
if ($generator->exec()) {
$this->mapZoomMax = $generator->get('maxZoom');
if (!$this->mapZoomMax) $this->mapZoomMax = 2;
$this->mapExist = true;
} else $this->show('Tile map generation fail');
}
}
$this->show();
}
public function show($err = 'No errors') {
$showMap = false;
if ($this->mode == 'show' and $err == 'No errors' and $this->mapZoomMax !== false) {
$showMap = true;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>GDMapTileGenerator example</title>
<?php if ($showMap) { ?>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.2/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.2/dist/leaflet.js"></script>
<?php } ?>
<meta charset="UTF-8">
<style>
body, html {
padding : 0px;
margin : 0px;
width : 100%;
height : 100%;
}
form {
padding : 24px;
}
body {
background : #e2e2e2;
}
#map-container {
margin-top : 0px;
}
.notice {
display : block;
background : rgba(242, 114, 114, 0.8);
height : 32px;
line-height : 32px;
border-radius : 6px;
color : #fff;
padding-left : 12px;
}
</style>
</head>
<body>
<?php if (!$showMap) { ?>
<form action="example.php?mode=upload" method="post" enctype='multipart/form-data'>
<?php if ($this->mapExist) { ?>
<p> You uploaded some tilemap. <a href="?mode=show">SHOW</a>
<?php } ?>
<p class="notice">
This file created for testing purposes only. It shows an example of creating map tile from image with help of GDMapTileGenerator class
</p>
<p>Choose an image (Upload max size : <?php echo ini_get('upload_max_filesize'); ?> | Post data max size : <?php echo ini_get('post_max_size'); ?>)</p>
<input type="hidden" name="image-upload" value="1">
<input type="file" name="image-file">
<input type="submit" value="Upload">
<?php if ($err or $this->log) { ?>
<p>Upload errors : <?php echo $err; ?></p>
<p>Tile generator log : <br><?php echo $this->log; ?></p>
<?php } ?>
</form>
<?php } elseif ($showMap) { ?>
<div id="map-container" style="width : 100%; height : 100%;"></div>
<script>
var maxZoom = <?php echo $this->mapZoomMax; ?>;
if (maxZoom <= 0) maxZoom = 0;
var minZoom = 1;
if (maxZoom == 0) minZoom = 0;
var map = L.map('map-container', {
center: [0.0, 0.0],
zoom: maxZoom,
});
var mapLayer = L.tileLayer(
'<?php echo $this->mapDir; ?>{z}/{x}/{y}.png', {
maxZoom: maxZoom,
minZoom: minZoom,
tms: false,
crs: L.CRS.Simple,
continuousWorld: false,
noWrap: true
});
mapLayer.addTo(map);
</script>
<?php } ?>
</body>
</html>
<?
exit;
}
}
$page = new Example();
$page->exec();