-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGDMapTileGenerator.class.php
More file actions
357 lines (282 loc) · 11.5 KB
/
GDMapTileGenerator.class.php
File metadata and controls
357 lines (282 loc) · 11.5 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
namespace Kelly;
/**
* GDMapTileGenerator v1.0 (С) 2014 NC22 | License : GPLv3
*
* check extension_loaded('gd') before use
*
* Helpfull config options<br>
* temporary set unlimited memory limit<br>
* ini_set("memory_limit", "-1");<br>
* temporary set unlimited execution time<br>
* ini_set("max_execution_time", 0);<br>
*/
class GDMapTileGenerator
{
private $map = null;
private $deleteBuffer = false;
private $skipExist = false;
private $extended = false;
private $tileSize = 256;
private $storage = './tiles/';
private $struct = '%d/%d/%d';
private $ext = 'png'; // output extension
private $quality = 0; // auto by maximal quality for extension
private $minZoom = 0;
private $maxZoom = 0; // auto by map image size, also auto corrects if map is smaller
private $filler = array(255, 255, 255, 127);
private $callback = false;
private $extend = true; // extend map if map size is incorrect to max zoom pixels, if false cut map by smaller edge
/**
* RU<br>
* Инвертировать ось Y<br>
* По протоколу TMS (Tile Map Service) вывод тайлов идет снизу вверх (для него координата 0,0 находится снизу)<br>
* <br>
* EN<br>
* If true, inverses Y axis numbering for tiles (turn this on for TMS services)<br>
* http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
* @var boolean
*/
private $tms = false;
private $lvls = array();
/**
*
* @param string $mapFile
* @param boolean $skipExist skip file if zoom level file already generated
* @param boolean $deleteBuffer delete zoom level image after generate tiles, any way it recreates after generateBaseZoomImages()
*/
public function __construct($mapFile, $skipExist = false, $deleteBuffer = false)
{
if (!file_exists($mapFile)) {
$this->log('File ' . $mapFile . ' unexist or unable to read');
return;
}
$this->map = $mapFile;
$this->deleteBuffer = (bool) $deleteBuffer;
$this->skipExist = (bool) $skipExist;
}
private function getDir($dir)
{
$result = true;
if (!is_dir($dir)) {
$back = umask(0);
$result = mkdir($dir, 0775, true);
umask($back);
}
return $result ? $dir : false;
}
public function set($param, $value)
{
$this->{$param} = $value;
}
public function get($param)
{
return $this->{$param};
}
private function cleanUp()
{
foreach ($this->lvls as $file) {
if (is_file($file)) unlink($file);
}
}
public function exec()
{
if (!$this->map) {
return false;
}
if (!$this->quality) {
if ($this->ext === 'jpg') {
$this->quality = 90;
} elseif ($this->ext === 'png') {
$this->quality = 9;
}
}
$this->lvls = array();
if ($this->generateBaseZoomImages()) {
for ($i = $this->minZoom; $i <= $this->maxZoom; $i++) {
if (!$this->generateTiles($i)) {
$this->cleanUp();
return false;
}
}
}
return true;
}
public function generateTiles($zoom)
{
$file = $this->storage . $zoom . '.' . $this->ext;
if (!is_file($file) || !is_readable($file)) {
$this->log('Cannot read image ' . $file . ', for zoom ' . $zoom);
return false;
}
$base = $this->loadImage($file);
$baseW = imagesx($base);
$baseH = imagesy($base);
// num of tiles in this zoom level, ceil round up so we need fill empty space after, for tiles with size 256 that not needed
$x = ceil($baseW / $this->tileSize);
$y = ceil($baseH / $this->tileSize);
$w = $this->tileSize;
$h = $w;
$cutX = 0;
$cutY = 0;
for ($ix = 0; $ix < $x; $ix++) {
$cutX = $ix * $w;
for ($iy = 0; $iy < $y; $iy++) {
$lvlFile = $this->storage . sprintf($this->struct, $zoom, $ix, $iy) . '.' . $this->ext;
$dir = pathinfo($lvlFile, PATHINFO_DIRNAME) . '/';
if (!$this->getDir($dir)) {
$this->log('generateTiles : Create dir fail : ' . $dir);
}
if ($this->skipExist && is_file($lvlFile)) {
continue;
}
$cutY = $this->tms ? $baseH - ($iy + 1) * $h : $iy * $h;
$tile = $this->createImage($w, $h, $this->extended);
imagecopy($tile, $base, 0, 0, $cutX, $cutY, $w, $h);
if (!$this->saveImage($tile, $lvlFile)) {
return false;
}
imagedestroy($tile);
}
}
if ($this->deleteBuffer) unlink($file);
imagedestroy($base);
$this->log('Generate tiles finished ' . $zoom);
return true;
}
public function generateBaseZoomImages()
{
$map = $this->loadImage($this->map);
if (!$map) return false;
$this->log('Map image file loaded');
$wMap = imagesx($map);
$hMap = imagesy($map);
if ($this->extend) $mapSize = max($wMap, $hMap);
else $mapSize = min($wMap, $hMap);
$mapMaxZoom = log($mapSize / $this->tileSize, 2);
$mapMaxZoom = $this->extend ? ceil($mapMaxZoom) : floor($mapMaxZoom);
if ($mapMaxZoom <= 0) $mapMaxZoom = 0;
$mapZoomSize = pow(2, $mapMaxZoom) * $this->tileSize; // pixels
if ($this->extend and ($mapZoomSize > $hMap or $mapZoomSize > $wMap)) {
$this->extended = true;
$this->log('map size need to be extended');
}
if (!$this->maxZoom or $this->maxZoom > $mapMaxZoom) {
$this->log('Set auto zoom (zoom not set by default or maxZoom > map actual size) - ' . $this->maxZoom . ' | ' . $mapMaxZoom . ' | ' . $mapSize);
$this->maxZoom = $mapMaxZoom;
}
if ($mapZoomSize != $hMap or $mapZoomSize != $wMap) {
$this->log('Map is not quad or need to be cutted or extended '
. '(map size not match with correct max zoom size) - ' . $mapZoomSize . ' | ' . $hMap . ' | ' . $wMap);
$newMap = $this->createImage($mapZoomSize, $mapZoomSize, $this->extended);
if ($this->extended) { // need to extend
$mapHalfPoint = floor($mapZoomSize / 2);
$halfByX = floor($wMap / 2);
$halfByY = floor($hMap / 2);
$pointX = $mapHalfPoint - $halfByX;
$pointY = $mapHalfPoint - $halfByY;
imagecopyresampled($newMap, $map, $pointX, $pointY, 0, 0,
$wMap, $hMap, $wMap, $hMap);
$this->log('Extend map size. Base image copyed to ' . $pointX . ' | ' . $pointY);
} else { // copy map to mapZoomSize image (cut if it bigger)
imagecopyresampled($newMap, $map, 0, 0, 0, 0,
$mapZoomSize, $mapZoomSize, $mapZoomSize, $mapZoomSize);
}
imagedestroy($map);
$map = $newMap;
}
// $requiredMemoryMB = ( $imageInfo[0] * $imageInfo[1] * ($imageInfo['bits'] / 8) * $imageInfo['channels'] * 2.5 ) / 1024;
$prevLvlImg = $map;
for ($i = $this->maxZoom; $i >= $this->minZoom; $i--) {
$lvlFile = $this->storage . $i . '.' . $this->ext;
$dir = pathinfo ($lvlFile, PATHINFO_DIRNAME);
if (!$this->getDir($dir)) {
$this->log('generateBaseZoomImages : Create dir fail : ' . $dir);
}
if (!$this->skipExist && is_file($lvlFile)) {
continue;
}
$lvlW = pow(2, $i) * $this->tileSize;
$lvlH = $lvlW;
$newLvlImg = $this->createImage($lvlW, $lvlH, $this->extended);
imagecopyresampled($newLvlImg, $prevLvlImg, 0, 0, 0, 0,
$lvlW, $lvlH, imagesx($prevLvlImg), imagesy($prevLvlImg));
imagedestroy($prevLvlImg);
$prevLvlImg = $newLvlImg;
if (!$this->saveImage($prevLvlImg, $lvlFile)) {
return false;
}
$this->log('Create base zoom image file ' . $i);
$this->lvls[] = $lvlFile;
}
if ($prevLvlImg) {
imagedestroy($prevLvlImg);
}
return true;
}
private function createImage($x, $y, $filler = true)
{
$img = imagecreatetruecolor($x, $y);
if ($filler) {
if ($this->filler[3]) {
imagealphablending( $img, false );
imagesavealpha( $img, true );
$filler = imagecolorallocatealpha($img, $this->filler[0], $this->filler[1], $this->filler[2], $this->filler[3]);
} else {
$filler = imagecolorallocate($img, $this->filler[0], $this->filler[1], $this->filler[2]);
}
imagefill($img, 0, 0, $filler);
}
return $img;
}
private function loadImage($imgWay)
{
if (!is_file($imgWay) || !is_readable($imgWay)) {
$this->log('Cannot read image file ' . $imgWay . ' - unexist or unreadable');
return false;
}
$size = getimagesize($imgWay);
if ($size === false) {
$this->log('Cannot read image file ' . $imgWay . ' - bad data');
return false;
}
switch ($size[2]) {
case 2: $img = imagecreatefromjpeg($imgWay);
break;
case 3: $img = imagecreatefrompng($imgWay);
break;
case 1: $img = imagecreatefromgif($imgWay);
break;
default : return false;
}
if (!$img) {
$this->log('Cannot read image file ' . $imgWay . ' - imagecreate function return false');
return false;
}
if ($size[2] == 3) imagesavealpha($img, true);
imagealphablending($img, true);
return $img;
}
private function saveImage($img, $imgWay)
{
switch ($this->ext) {
case 'jpg':
case 'jpeg': $result = imagejpeg($img, $imgWay, $this->quality); break;
case 'png': $result = imagepng($img, $imgWay, $this->quality); break;
case 'gif': $result = imagegif($img, $imgWay); break;
default : return false;
}
if (!$result) {
$this->log('Cannot write image file ' . $imgWay);
return false;
}
chmod($imgWay, 0664);
return true;
}
private function log($text)
{
if ($this->callback) {
call_user_func_array($this->callback, array('GDMapTileGenerator : ' . $text));
}
}
}