128 lines
4.2 KiB
PHP
128 lines
4.2 KiB
PHP
<?php
|
|
|
|
class Mapbox_StaticImageApi {
|
|
|
|
public static function getImageFileContent(Array $params) : ?string {
|
|
$access_token = $params["access_token"];
|
|
$gps_lat = $params["gps_lat"];
|
|
$gps_long = $params["gps_long"];
|
|
$zoom = $params["zoom"];
|
|
$size_x = $params["size_x"];
|
|
$size_y = $params["size_y"];
|
|
$style = $params["style"];
|
|
$pin = $params["pin"];
|
|
$paths = $params["paths"];
|
|
|
|
$pin_part = "";
|
|
|
|
// https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v12/static/pin-l-embassy+ee9900(15.4200370610,46.9599897293)/15.4200370610,46.9599897293,19/640x640?access_token=pk.eyJ1Ijoic2tuZXR3b3J4IiwiYSI6ImNqdWw3eXoyMzBieWU0M284OTU2eWNxMTMifQ.GrgBYKMXVt3TiwU53OBllQ
|
|
$url = "https://api.mapbox.com/styles/v1/mapbox/$style/static";
|
|
if(is_array($pin) && $pin["gps_lat"] && $pin["gps_long"] && $pin["size"]) {
|
|
$pin_gps_lat = $pin["gps_lat"];
|
|
$pin_gps_long = $pin["gps_long"];
|
|
$pin_size = $pin["size"];
|
|
$pin_color = $pin["color"];
|
|
$pin_icon = $pin["icon"];
|
|
|
|
$pin_part .= "pin-$pin_size";
|
|
if($pin_icon) $pin_part .= "-$pin_icon";
|
|
$pin_part .= "+$pin_color($pin_gps_long,$pin_gps_lat)";
|
|
}
|
|
|
|
$path_parts = [];
|
|
if(is_array($paths) && count($paths)) {
|
|
$path_stroke_width = $paths["line_width"];
|
|
$path_stroke_color = $paths["line_color"];
|
|
$path_stroke_opacity = $paths["line_opacity"];
|
|
$path_fill_color = $paths["line_fill_color"];
|
|
$path_fill_opacity = $paths["line_fill_opacity"];
|
|
|
|
foreach($paths["coords"] as $path) {
|
|
if(!is_array($path)) {
|
|
mfLoghandler::singleton()->debug("path not array: ".print_r($path, true));
|
|
continue;
|
|
}
|
|
|
|
$path_enc_polyline = self::encodeCoordArrayToPolyline($path);
|
|
if(!$path_enc_polyline) continue;
|
|
|
|
$path_parts[] = "path-$path_stroke_width+$path_stroke_color-$path_stroke_opacity+$path_fill_color-$path_fill_opacity($path_enc_polyline)";
|
|
}
|
|
}
|
|
|
|
// build url
|
|
$url_opt_parts = [];
|
|
if($pin_part) $url_opt_parts[] = $pin_part;
|
|
|
|
if(count($path_parts)) {
|
|
foreach($path_parts as $path_part) {
|
|
$url_opt_parts[] = $path_part;
|
|
}
|
|
}
|
|
|
|
if(is_array($url_opt_parts)) {
|
|
$url .= "/".implode(",", $url_opt_parts);
|
|
}
|
|
|
|
$url .= "/auto/{$size_x}x{$size_y}?access_token=$access_token";
|
|
|
|
mfLoghandler::singleton()->debug($url);
|
|
//exit;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$response = file_get_contents($url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
return null;
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
public static function encodeCoordArrayToPolyline($coords, $precision = 5) {
|
|
//if(!is_array($coords)) return false;
|
|
|
|
$points = [];
|
|
array_walk_recursive(
|
|
$coords,
|
|
function ($current) use (&$points) {
|
|
$points[] = $current;
|
|
}
|
|
);
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": flattened path: ".print_r($points, true));
|
|
|
|
$encodedString = '';
|
|
$index = 0;
|
|
$previous = array(0,0);
|
|
foreach ( $points as $number ) {
|
|
$number = (float)($number);
|
|
$number = (int)round($number * pow(10, $precision));
|
|
$diff = $number - $previous[$index % 2];
|
|
$previous[$index % 2] = $number;
|
|
$number = $diff;
|
|
$index++;
|
|
$number = ($number < 0) ? ~($number << 1) : ($number << 1);
|
|
$chunk = '';
|
|
while ( $number >= 0x20 ) {
|
|
$chunk .= chr((0x20 | ($number & 0x1f)) + 63);
|
|
$number >>= 5;
|
|
}
|
|
$chunk .= chr($number + 63);
|
|
$encodedString .= $chunk;
|
|
}
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": encoded polyline: $encodedString");
|
|
|
|
|
|
return str_replace("?", "%3f", $encodedString);
|
|
}
|
|
} |