corrade-http-templates – Diff between revs 2 and 12
?pathlinks?
Rev 2 | Rev 12 | |||
---|---|---|---|---|
1 | <?php |
1 | <?php |
|
2 | |
2 | |
|
3 | ########################################################################### |
3 | ########################################################################### |
|
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
|
5 | ########################################################################### |
5 | ########################################################################### |
|
6 | ## This is a script that uses Corrade's "getterrainheight" command in ## |
6 | ## This is a script that uses Corrade's "getterrainheight" command in ## |
|
7 | ## order to genereate a red-channel height-map of the terrain without ## |
7 | ## order to genereate a red-channel height-map of the terrain without ## |
|
8 | ## the need to be an estate / region owner. Essentially this script can ## |
8 | ## the need to be an estate / region owner. Essentially this script can ## |
|
9 | ## be used to give an overview of the terrain height for any region. ## |
9 | ## be used to give an overview of the terrain height for any region. ## |
|
10 | ########################################################################### |
10 | ########################################################################### |
|
11 | |
11 | |
|
12 | ########################################################################### |
12 | ########################################################################### |
|
13 | ## CONFIGURATION ## |
13 | ## CONFIGURATION ## |
|
14 | ########################################################################### |
14 | ########################################################################### |
|
15 | |
15 | |
|
16 | // The configuration file for this script containing the settings. |
16 | # The configuration file for this script containing the settings. |
|
- | 17 | require_once("config.php"); |
||
17 | include_once("config.php"); |
18 | require_once('functions.php'); |
|
18 | |
19 | |
|
19 | ########################################################################### |
20 | ########################################################################### |
|
20 | ## INTERNALS ## |
21 | ## INTERNALS ## |
|
21 | ########################################################################### |
22 | ########################################################################### |
|
22 | |
- | ||
23 | ########################################################################### |
- | ||
24 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
25 | ########################################################################### |
- | ||
26 | function wasKeyValueGet($key, $data) { |
- | ||
27 | return array_reduce( |
- | ||
28 | explode( |
- | ||
29 | "&", |
- | ||
30 | $data |
- | ||
31 | ), |
- | ||
32 | function($o, $p) { |
- | ||
33 | $x = explode("=", $p); |
- | ||
34 | return array_shift($x) != $o ? $o : array_shift($x); |
- | ||
35 | }, |
- | ||
36 | $key |
- | ||
37 | ); |
- | ||
38 | } |
- | ||
39 | |
- | ||
40 | ########################################################################### |
- | ||
41 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
- | ||
42 | ########################################################################### |
- | ||
43 | function mapValueToRange($value, $xMin, $xMax, $yMin, $yMax) { |
- | ||
44 | return $yMin + ( |
- | ||
45 | ( |
- | ||
46 | $yMax - $yMin |
- | ||
47 | ) |
- | ||
48 | * |
- | ||
49 | ( |
- | ||
50 | $value - $xMin |
- | ||
51 | ) |
- | ||
52 | / |
- | ||
53 | ( |
- | ||
54 | $xMax - $xMin |
- | ||
55 | ) |
- | ||
56 | ); |
- | ||
57 | } |
- | ||
58 | |
23 | |
|
59 | #### |
24 | #### |
|
60 | # I. Get the terrain height. |
25 | # I. Get the terrain height. |
|
61 | $params = array( |
26 | $params = array( |
|
62 | 'command' => 'getterrainheight', |
27 | 'command' => 'getterrainheight', |
|
63 | 'group' => $GROUP, |
28 | 'group' => $GROUP, |
|
64 | 'entity' => 'region', |
29 | 'entity' => 'region', |
|
65 | 'password' => $PASSWORD |
30 | 'password' => $PASSWORD |
|
66 | ); |
31 | ); |
|
67 | |
32 | |
|
68 | # We now escape each key and value: this is very important, because the |
33 | # We now escape each key and value: this is very important, because the |
|
69 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
34 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
|
70 | # in this example though but this will not hurt anyone). |
35 | # in this example though but this will not hurt anyone). |
|
71 | array_walk($params, |
36 | array_walk($params, |
|
72 | function(&$value, $key) { |
37 | function(&$value, $key) { |
|
73 | $value = urlencode($key)."=".urlencode($value); |
38 | $value = urlencode($key)."=".urlencode($value); |
|
74 | } |
39 | } |
|
75 | ); |
40 | ); |
|
76 | $postvars = implode('&', $params); |
41 | $postvars = implode('&', $params); |
|
77 | |
42 | |
|
78 | # Set the options, send the request and then display the outcome |
43 | # Set the options, send the request and then display the outcome |
|
79 | if (!($curl = curl_init())) { |
44 | if (!($curl = curl_init())) { |
|
80 | echo "Could not initialise CURL".PHP_EOL; |
45 | echo "Could not initialise CURL".PHP_EOL; |
|
81 | } |
46 | } |
|
82 | |
47 | |
|
83 | curl_setopt($curl, CURLOPT_URL, $URL); |
48 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
84 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
49 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
85 | curl_setopt($curl, CURLOPT_POST, true); |
50 | curl_setopt($curl, CURLOPT_POST, true); |
|
86 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
51 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
87 | $return = curl_exec($curl); |
52 | $return = curl_exec($curl); |
|
88 | curl_close($curl); |
53 | curl_close($curl); |
|
89 | |
54 | |
|
90 | $success = urldecode( |
55 | $success = urldecode( |
|
91 | wasKeyValueGet( |
56 | wasKeyValueGet( |
|
92 | "success", |
57 | "success", |
|
93 | $return |
58 | $return |
|
94 | ) |
59 | ) |
|
95 | ); |
60 | ); |
|
96 | |
61 | |
|
97 | // Unable to get the region heights? |
62 | // Unable to get the region heights? |
|
98 | if($success == 'False') return -1; |
63 | if($success == 'False') return -1; |
|
99 | |
64 | |
|
100 | $map = str_getcsv( |
65 | $map = str_getcsv( |
|
101 | urldecode( |
66 | urldecode( |
|
102 | wasKeyValueGet( |
67 | wasKeyValueGet( |
|
103 | "data", |
68 | "data", |
|
104 | $return |
69 | $return |
|
105 | ) |
70 | ) |
|
106 | ) |
71 | ) |
|
107 | ); |
72 | ); |
|
108 | |
73 | |
|
109 | #### |
74 | #### |
|
110 | # II. If we did not have exactly 256 x 256 array elements, then abort. |
75 | # II. If we did not have exactly 256 x 256 array elements, then abort. |
|
111 | if(count($map) != 65536) return -1; |
76 | if(count($map) != 65536) return -1; |
|
112 | |
77 | |
|
113 | // Find the maximal height of the terrain. |
78 | // Find the maximal height of the terrain. |
|
114 | $max = max($map); |
79 | $max = max($map); |
|
115 | |
80 | |
|
116 | #### |
81 | #### |
|
117 | # III. Create a new image by encoding the elevations to the red channel. |
82 | # III. Create a new image by encoding the elevations to the red channel. |
|
118 | $im = imagecreatetruecolor(256, 256); |
83 | $im = imagecreatetruecolor(256, 256); |
|
119 | foreach(range(0, 255) as $x) { |
84 | foreach(range(0, 255) as $x) { |
|
120 | foreach(range(0, 255) as $y) { |
85 | foreach(range(0, 255) as $y) { |
|
121 | $red = mapValueToRange( |
86 | $red = mapValueToRange( |
|
122 | $map[256 * $x + $y], |
87 | $map[256 * $x + $y], |
|
123 | 0, |
88 | 0, |
|
124 | $max, |
89 | $max, |
|
125 | 0, |
90 | 0, |
|
126 | 256 |
91 | 256 |
|
127 | ); |
92 | ); |
|
128 | imagesetpixel( |
93 | imagesetpixel( |
|
129 | $im, |
94 | $im, |
|
130 | $x, |
95 | $x, |
|
131 | 256-$y, |
96 | 256-$y, |
|
132 | imagecolorallocate( |
97 | imagecolorallocate( |
|
133 | $im, |
98 | $im, |
|
134 | $red, |
99 | $red, |
|
135 | 0, |
100 | 0, |
|
136 | 0 |
101 | 0 |
|
137 | ) |
102 | ) |
|
138 | ); |
103 | ); |
|
139 | } |
104 | } |
|
140 | } |
105 | } |
|
141 | |
106 | |
|
142 | |
107 | |
|
143 | |
108 | |
|
144 | #### |
109 | #### |
|
145 | # IV. Output the image as Base64 encoded data. |
110 | # IV. Output the image as Base64 encoded data. |
|
146 | ob_start(); |
111 | ob_start(); |
|
147 | imagepng($im); |
112 | imagepng($im); |
|
148 | $png = ob_get_contents(); |
113 | $png = ob_get_contents(); |
|
149 | imagedestroy($im); |
114 | imagedestroy($im); |
|
150 | ob_end_clean(); |
115 | ob_end_clean(); |
|
151 | |
116 | |
|
152 | echo base64_encode($png); |
117 | echo base64_encode($png); |
|
153 | |
118 | |
|
154 | ?> |
119 | ?> |
|
155 | |
120 | |
|
156 |
|
121 |
|
|
157 | |
122 | |
|
158 | |
123 | |
|
159 | |
124 | |