corrade-http-templates – Diff between revs 12 and 77
?pathlinks?
Rev 12 | Rev 77 | |||
---|---|---|---|---|
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 | require_once('config.php'); |
|
18 | require_once('functions.php'); |
18 | require_once('vendor/was/utilities/src/formats/kvp/kvp.php'); |
|
- | 19 | require_once('vendor/was/utilities/src/mathematics/algebra.php'); |
||
19 | |
20 | |
|
20 | ########################################################################### |
21 | ########################################################################### |
|
21 | ## INTERNALS ## |
22 | ## INTERNALS ## |
|
22 | ########################################################################### |
23 | ########################################################################### |
|
23 | |
24 | |
|
24 | #### |
25 | #### |
|
25 | # I. Get the terrain height. |
26 | # I. Get the terrain height. |
|
26 | $params = array( |
27 | $params = array( |
|
27 | 'command' => 'getterrainheight', |
28 | 'command' => 'getterrainheight', |
|
28 | 'group' => $GROUP, |
29 | 'group' => $GROUP, |
|
29 | 'entity' => 'region', |
30 | 'entity' => 'region', |
|
30 | 'password' => $PASSWORD |
31 | 'password' => $PASSWORD |
|
31 | ); |
32 | ); |
|
32 | |
33 | |
|
33 | # We now escape each key and value: this is very important, because the |
34 | # We now escape each key and value: this is very important, because the |
|
34 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
35 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
|
35 | # in this example though but this will not hurt anyone). |
36 | # in this example though but this will not hurt anyone). |
|
36 | array_walk($params, |
37 | array_walk($params, |
|
37 | function(&$value, $key) { |
38 | function(&$value, $key) { |
|
38 | $value = urlencode($key)."=".urlencode($value); |
39 | $value = urlencode($key)."=".urlencode($value); |
|
39 | } |
40 | } |
|
40 | ); |
41 | ); |
|
41 | $postvars = implode('&', $params); |
42 | $postvars = implode('&', $params); |
|
42 | |
43 | |
|
43 | # Set the options, send the request and then display the outcome |
44 | # Set the options, send the request and then display the outcome |
|
44 | if (!($curl = curl_init())) { |
45 | if (!($curl = curl_init())) { |
|
45 | echo "Could not initialise CURL".PHP_EOL; |
46 | echo "Could not initialise CURL".PHP_EOL; |
|
46 | } |
47 | } |
|
47 | |
48 | |
|
48 | curl_setopt($curl, CURLOPT_URL, $URL); |
49 | curl_setopt($curl, CURLOPT_URL, $URL); |
|
49 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
50 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
50 | curl_setopt($curl, CURLOPT_POST, true); |
51 | curl_setopt($curl, CURLOPT_POST, true); |
|
51 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
52 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
|
52 | $return = curl_exec($curl); |
53 | $return = curl_exec($curl); |
|
53 | curl_close($curl); |
54 | curl_close($curl); |
|
54 | |
55 | |
|
55 | $success = urldecode( |
56 | $success = urldecode( |
|
56 | wasKeyValueGet( |
57 | wasKeyValueGet( |
|
57 | "success", |
58 | "success", |
|
58 | $return |
59 | $return |
|
59 | ) |
60 | ) |
|
60 | ); |
61 | ); |
|
61 | |
62 | |
|
62 | // Unable to get the region heights? |
63 | // Unable to get the region heights? |
|
63 | if($success == 'False') return -1; |
64 | if($success == 'False') return -1; |
|
64 | |
65 | |
|
65 | $map = str_getcsv( |
66 | $map = str_getcsv( |
|
66 | urldecode( |
67 | urldecode( |
|
67 | wasKeyValueGet( |
68 | wasKeyValueGet( |
|
68 | "data", |
69 | "data", |
|
69 | $return |
70 | $return |
|
70 | ) |
71 | ) |
|
71 | ) |
72 | ) |
|
72 | ); |
73 | ); |
|
73 | |
74 | |
|
74 | #### |
75 | #### |
|
75 | # II. If we did not have exactly 256 x 256 array elements, then abort. |
76 | # II. If we did not have exactly 256 x 256 array elements, then abort. |
|
76 | if(count($map) != 65536) return -1; |
77 | if(count($map) != 65536) return -1; |
|
77 | |
78 | |
|
78 | // Find the maximal height of the terrain. |
79 | // Find the maximal height of the terrain. |
|
79 | $max = max($map); |
80 | $max = max($map); |
|
80 | |
81 | |
|
81 | #### |
82 | #### |
|
82 | # III. Create a new image by encoding the elevations to the red channel. |
83 | # III. Create a new image by encoding the elevations to the red channel. |
|
83 | $im = imagecreatetruecolor(256, 256); |
84 | $im = imagecreatetruecolor(256, 256); |
|
84 | foreach(range(0, 255) as $x) { |
85 | foreach(range(0, 255) as $x) { |
|
85 | foreach(range(0, 255) as $y) { |
86 | foreach(range(0, 255) as $y) { |
|
86 | $red = mapValueToRange( |
87 | $red = mapValueToRange( |
|
87 | $map[256 * $x + $y], |
88 | $map[256 * $x + $y], |
|
88 | 0, |
89 | 0, |
|
89 | $max, |
90 | $max, |
|
90 | 0, |
91 | 0, |
|
91 | 256 |
92 | 256 |
|
92 | ); |
93 | ); |
|
93 | imagesetpixel( |
94 | imagesetpixel( |
|
94 | $im, |
95 | $im, |
|
95 | $x, |
96 | $x, |
|
96 | 256-$y, |
97 | 256-$y, |
|
97 | imagecolorallocate( |
98 | imagecolorallocate( |
|
98 | $im, |
99 | $im, |
|
99 | $red, |
100 | $red, |
|
100 | 0, |
101 | 0, |
|
101 | 0 |
102 | 0 |
|
102 | ) |
103 | ) |
|
103 | ); |
104 | ); |
|
104 | } |
105 | } |
|
105 | } |
106 | } |
|
106 | |
107 | |
|
107 | |
108 | |
|
108 | |
109 | |
|
109 | #### |
110 | #### |
|
110 | # IV. Output the image as Base64 encoded data. |
111 | # IV. Output the image as Base64 encoded data. |
|
111 | ob_start(); |
112 | ob_start(); |
|
112 | imagepng($im); |
113 | imagepng($im); |
|
113 | $png = ob_get_contents(); |
114 | $png = ob_get_contents(); |
|
114 | imagedestroy($im); |
115 | imagedestroy($im); |
|
115 | ob_end_clean(); |
116 | ob_end_clean(); |
|
116 | |
117 | |
|
117 | echo base64_encode($png); |
118 | echo base64_encode($png); |
|
118 | |
119 | |
|
119 | ?> |
120 | ?> |
|
120 | |
121 | |
|
121 |
|
122 |
|
|
122 | |
123 | |
|
123 | |
124 | |
|
124 | |
125 | |