From ff0063b515cf3a5fd0f0c3a42884032f29329ec1 Mon Sep 17 00:00:00 2001 From: Paul Heinrichs Date: Wed, 11 Apr 2018 09:02:04 -0400 Subject: [PATCH] api: Add option to output graphs as base64 (#8535) * Allow 'option' param in graph endpoint * Update lint and update docs * Uncomment api response * Add imagedestroy on 'tobias_trim' section * Add base64 to port endpoints also --- doc/API/Devices.md | 1 + html/includes/api_functions.inc.php | 13 +++++++++++ html/includes/graphs/graph.inc.php | 35 ++++++++++++++++++++++------- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/doc/API/Devices.md b/doc/API/Devices.md index 8e2a606b29..1cd2f083af 100644 --- a/doc/API/Devices.md +++ b/doc/API/Devices.md @@ -396,6 +396,7 @@ Input: - to: This is the date you would like the graph to end - See http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html for more information. - width: The graph width, defaults to 1075. - height: The graph height, defaults to 300. + - output: Set how the graph should be outputted (base64, display), defaults to display. Example: ```curl diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 3947d2c525..cfacde44e9 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -140,6 +140,7 @@ function get_graph_by_port_hostname() $vars = array(); $vars['port'] = urldecode($router['ifname']); $vars['type'] = $router['type'] ?: 'port_bits'; + $vars['output'] = $_GET['output'] ?: 'display'; if (!empty($_GET['from'])) { $vars['from'] = $_GET['from']; } @@ -164,6 +165,9 @@ function get_graph_by_port_hostname() rrdtool_initialize(false); include 'includes/graphs/graph.inc.php'; rrdtool_close(); + if ($vars['output'] === 'base64') { + api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image'); + } } @@ -212,6 +216,7 @@ function get_graph_generic_by_hostname() $sensor_id = $router['sensor_id'] ?: null; $vars = array(); $vars['type'] = $router['type'] ?: 'device_uptime'; + $vars['output'] = $_GET['output'] ?: 'display'; if (isset($sensor_id)) { $vars['id'] = $sensor_id; if (str_contains($vars['type'], '_wireless')) { @@ -244,6 +249,10 @@ function get_graph_generic_by_hostname() rrdtool_initialize(false); include 'includes/graphs/graph.inc.php'; rrdtool_close(); + + if ($vars['output'] === 'base64') { + api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image'); + } } @@ -628,6 +637,7 @@ function get_graph_by_portgroup() $group = $router['group'] ?: ''; $id = $router['id'] ?: ''; $vars = array(); + $vars['output'] = $_GET['output'] ?: 'display'; if (!empty($_GET['from'])) { $vars['from'] = $_GET['from']; } @@ -662,6 +672,9 @@ function get_graph_by_portgroup() rrdtool_initialize(false); include 'includes/graphs/graph.inc.php'; rrdtool_close(); + if ($vars['output'] === 'base64') { + api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image'); + } } diff --git a/html/includes/graphs/graph.inc.php b/html/includes/graphs/graph.inc.php index d43936b140..6a07333e43 100644 --- a/html/includes/graphs/graph.inc.php +++ b/html/includes/graphs/graph.inc.php @@ -19,7 +19,7 @@ $height = $vars['height']; $title = $vars['title']; $vertical = $vars['vertical']; $legend = $vars['legend']; - +$output = (!empty($vars['output']) ? $vars['output'] : 'default'); $from = (isset($vars['from']) ? $vars['from'] : time() - 60 * 60 * 24); $to = (isset($vars['to']) ? $vars['to'] : time()); @@ -28,7 +28,7 @@ if ($from < 0) { } $period = ($to - $from); - +$base64_output = ''; $prev_from = ($from - $period); $graphfile = $config['temp_dir'].'/'.strgen(); @@ -132,7 +132,6 @@ if ($error_msg) { if ($rrd_options) { rrdtool_graph($graphfile, $rrd_options); d_echo($rrd_cmd); - if (is_file($graphfile)) { if (!$debug) { set_image_type(); @@ -157,12 +156,32 @@ if ($error_msg) { $trans_colour = imagecolorallocatealpha($dst_im, 0, 0, 0, 127); imagefill($dst_im, 0, 0, $trans_colour); imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); - imagepng($dst_im); - imagedestroy($dst_im); + if ($output === 'base64') { + ob_start(); + imagepng($png); + $imagedata = ob_get_contents(); + imagedestroy($png); + ob_end_clean(); + + $base64_output = base64_encode($imagedata); + } else { + imagepng($dst_im); + imagedestroy($dst_im); + } } else { - $fd = fopen($graphfile, 'r'); - fpassthru($fd); - fclose($fd); + if ($output === 'base64') { + $fd = fopen($graphfile, 'r'); + ob_start(); + fpassthru($fd); + $imagedata = ob_get_contents(); + fclose($fd); + ob_end_clean(); + $base64_output = base64_encode($imagedata); + } else { + $fd = fopen($graphfile, 'r'); + fpassthru($fd); + fclose($fd); + } } } else { echo `ls -l $graphfile`;