-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
133 lines (116 loc) · 3.93 KB
/
index.php
File metadata and controls
133 lines (116 loc) · 3.93 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
<?php
/**
* Created by PhpStorm.
* User: Felix
* Date: 18.11.2018
* Time: 19:59
*/
$time = microtime(true);
ini_set('precision', 10);
include('config.php');
/**
* Lowercase all Parameters
*/
$get = [];
foreach($_GET as $key => $val){
$get[strtolower($key)] = $val;
}
if (($get['request'] == 'GetMap') && ($get['layers'] == 'cache')) {
//Filter relevant Map parameters
$requestData = [];
foreach($relevantParameters as $rp){
if(array_key_exists($rp, $get))
$requestData[$rp] = $get[$rp];
}
//calculate Cache Keys and Path
ksort($requestData);
$cacheKey = md5(implode($requestData));
$cacheSubKey = substr($cacheKey, 0, 2);
$cachePath = CACHE_PATH.$cacheSubKey.'/';
$cacheFile = $cacheKey . '.png';
$cacheFilePath = $cachePath.$cacheFile;
if(!is_dir($cachePath))
mkdir($cachePath);
$cacheHit = true;
//Check if File is cached, otherwise Load from Backend
if (!file_exists($cacheFilePath)) {
$cacheHit = false;
//Setup the combined image
$dest_image = imagecreatetruecolor($_GET['WIDTH'], $_GET['HEIGHT']);
imagesavealpha($dest_image, true);
$trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
imagefill($dest_image, 0, 0, $trans_background);
//Initialize multiple CURL Instances to the Backend
$mh = curl_multi_init();
$handles = [];
$fhandles = [];
foreach ($wmsLayers as $wl) {
$requestData['LAYERS'] = $wl;
$cacheTmpKey = md5(implode($requestData));
$cacheTmpPath = TEMP_PATH . $cacheTmpKey . '.png';
$fhandles[$cacheTmpKey] = fopen ($cacheTmpPath, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, REMOTE_WMS.'?' . http_build_query($requestData));
curl_setopt($ch, CURLOPT_FILE, $fhandles[$cacheTmpKey]);
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
//run all the Curl & wait for them to finish
$running = 0;
do
curl_multi_exec($mh, $running);
while ($running > 0);
foreach($handles as $h)
curl_multi_remove_handle($mh, $h);
curl_multi_close($mh);
//Combine all images
foreach ($wmsLayers as $wl) {
$requestData['LAYERS'] = $wl;
$cacheTmpKey = md5(implode($requestData));
$cacheTmpPath = TEMP_PATH . $cacheTmpKey . '.png';
fclose($fhandles[$cacheTmpKey]);
$img = imagecreatefrompng($cacheTmpPath);
imagecopy($dest_image, $img, 0, 0, 0, 0, $_GET['WIDTH'], $_GET['HEIGHT']);
unlink($cacheTmpPath);
}
//Render Image to Disk
imagepng($dest_image, $cacheFilePath);
}
//PreCache
if(!($get['cachemode'] === 'prefetch')){
bcscale(10);
$preFetchGet = $get;
$preFetchGet['cachemode'] = 'prefetch';
list($x1,$y1,$x2,$y2) = explode(",",$get['bbox']);
//Left BBOX
$x1n = str_replace(",",".",bcsub($x1, bcsub($x2,$x1)));
$y1n = str_replace(",",".",$y1);
$x2n = str_replace(",",".",bcsub($x2, bcsub($x2,$x1)));
$y2n = str_replace(",",".",$y2);
$preFetchGet['bbox'] = $x1n.",".$y1n.",".$x2n.",".$y2n;
$preCacheURL = LOCAL_WMS . '?' . http_build_query($preFetchGet);
//file_get_contents($preCacheURL);
}
if(!DEBUG){
header('Content-Type: image/png');
echo file_get_contents($cacheFilePath);
}else{
header('Content-Type: image/png');
$dbgImage = imagecreatefrompng($cacheFilePath);
imageAlphaBlending($dbgImage, true);
imageSaveAlpha($dbgImage, true);
$black = imagecolorallocate($dbgImage, 0, 0, 0);
$text = "DEBUG MODE ENABLED\n";
$text .= "Cache.....: ". PHP_EOL . (($cacheHit)?"HIT":"MISS") . PHP_EOL;
$text .= "CacheKey..: ". PHP_EOL . $cacheKey . PHP_EOL;
$text .= "LoadTime..: ". PHP_EOL . ((microtime(true) - $time) * 1000) . 'ms' . PHP_EOL;
$text .= "BBOX......: " . PHP_EOL . str_replace(",",PHP_EOL,$get['bbox']) . PHP_EOL;
$text .= "BBOX Left.: " . PHP_EOL . implode(PHP_EOL, [$x1n,$y1n,$x2n,$y2n]) . PHP_EOL;
whitespaces_imagestring($dbgImage, 2, 10, 10, $text, $black);
imagepng($dbgImage);
imagedestroy($dbgImage);
}
} elseif ($_GET['REQUEST'] !== 'GetMap') {
header('Content-Type: image/png');
echo file_get_contents(REMOTE_WMS.'?' . http_build_query($_GET));
}