corrade-http-templates – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | eva | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
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 ## |
||
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. ## |
||
10 | ########################################################################### |
||
11 | |||
12 | ########################################################################### |
||
13 | ## CONFIGURATION ## |
||
14 | ########################################################################### |
||
15 | |||
16 | // The configuration file for this script containing the settings. |
||
17 | include_once("config.php"); |
||
18 | |||
19 | ########################################################################### |
||
20 | ## INTERNALS ## |
||
21 | ########################################################################### |
||
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 | |||
59 | #### |
||
60 | # I. Get the terrain height. |
||
61 | $params = array( |
||
62 | 'command' => 'getterrainheight', |
||
63 | 'group' => $GROUP, |
||
64 | 'entity' => 'region', |
||
65 | 'password' => $PASSWORD |
||
66 | ); |
||
67 | |||
68 | # 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 |
||
70 | # in this example though but this will not hurt anyone). |
||
71 | array_walk($params, |
||
72 | function(&$value, $key) { |
||
73 | $value = urlencode($key)."=".urlencode($value); |
||
74 | } |
||
75 | ); |
||
76 | $postvars = implode('&', $params); |
||
77 | |||
78 | # Set the options, send the request and then display the outcome |
||
79 | if (!($curl = curl_init())) { |
||
80 | echo "Could not initialise CURL".PHP_EOL; |
||
81 | } |
||
82 | |||
83 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
84 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
85 | curl_setopt($curl, CURLOPT_POST, true); |
||
86 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
87 | $return = curl_exec($curl); |
||
88 | curl_close($curl); |
||
89 | |||
90 | $success = urldecode( |
||
91 | wasKeyValueGet( |
||
92 | "success", |
||
93 | $return |
||
94 | ) |
||
95 | ); |
||
96 | |||
97 | // Unable to get the region heights? |
||
98 | if($success == 'False') return -1; |
||
99 | |||
100 | $map = str_getcsv( |
||
101 | urldecode( |
||
102 | wasKeyValueGet( |
||
103 | "data", |
||
104 | $return |
||
105 | ) |
||
106 | ) |
||
107 | ); |
||
108 | |||
109 | #### |
||
110 | # II. If we did not have exactly 256 x 256 array elements, then abort. |
||
111 | if(count($map) != 65536) return -1; |
||
112 | |||
113 | // Find the maximal height of the terrain. |
||
114 | $max = max($map); |
||
115 | |||
116 | #### |
||
117 | # III. Create a new image by encoding the elevations to the red channel. |
||
118 | $im = imagecreatetruecolor(256, 256); |
||
119 | foreach(range(0, 255) as $x) { |
||
120 | foreach(range(0, 255) as $y) { |
||
121 | $red = mapValueToRange( |
||
122 | $map[256 * $x + $y], |
||
123 | 0, |
||
124 | $max, |
||
125 | 0, |
||
126 | 256 |
||
127 | ); |
||
128 | imagesetpixel( |
||
129 | $im, |
||
130 | $x, |
||
131 | 256-$y, |
||
132 | imagecolorallocate( |
||
133 | $im, |
||
134 | $red, |
||
135 | 0, |
||
136 | |||
137 | ) |
||
138 | ); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | |||
143 | |||
144 | #### |
||
145 | # IV. Output the image as Base64 encoded data. |
||
146 | ob_start(); |
||
147 | imagepng($im); |
||
148 | $png = ob_get_contents(); |
||
149 | imagedestroy($im); |
||
150 | ob_end_clean(); |
||
151 | |||
152 | echo base64_encode($png); |
||
153 | |||
154 | ?> |