corrade-http-templates – Blame information for rev 77

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