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