diff --git a/library/Icinga/Application/webrouter.php b/library/Icinga/Application/webrouter.php index 71b48b2c7..b00034871 100644 --- a/library/Icinga/Application/webrouter.php +++ b/library/Icinga/Application/webrouter.php @@ -92,7 +92,7 @@ if (in_array($path, $special)) { header('Content-Type: image/svg+xml'); $pie = new PieChart(); $pie->initFromRequest(); - echo $pie->render(); + $pie->toSvg(); } elseif ($path === 'png/chart.php') { if (!array_key_exists('data', $_GET)) { diff --git a/library/Icinga/Chart/Chart.php b/library/Icinga/Chart/Chart.php index 3cfa4b39a..5b545d4de 100644 --- a/library/Icinga/Chart/Chart.php +++ b/library/Icinga/Chart/Chart.php @@ -4,7 +4,7 @@ namespace Icinga\Chart; -use Exception; +use Imagick; use Icinga\Chart\Legend; use Icinga\Chart\Palette; use Icinga\Chart\Primitive\Drawable; @@ -110,12 +110,35 @@ abstract class Chart implements Drawable return $this->renderer->render(); } + /** + * Return this graph rendered as PNG + * + * @param int $width The width of the PNG in pixel + * @param int $height The height of the PNG in pixel + * + * @return string A PNG binary string + * + * @throws IcingaException In case ImageMagick is not available + */ + public function toPng($width, $height) + { + if (! class_exists('Imagick')) { + throw new IcingaException('Cannot render PNGs without ImageMagick'); + } + + $image = new Imagick(); + $image->readImageBlob($this->render()); + $image->setImageFormat('png24'); + $image->resizeImage($width, $height, imagick::FILTER_LANCZOS, 1); + return $image; + } + /** * Align the chart to the top left corner instead of centering it * * @param bool $align */ - public function alignTopLeft ($align = true) + public function alignTopLeft($align = true) { $this->align = $align; } diff --git a/library/Icinga/Chart/Inline/PieChart.php b/library/Icinga/Chart/Inline/PieChart.php index f42034ebd..fdffd9acd 100644 --- a/library/Icinga/Chart/Inline/PieChart.php +++ b/library/Icinga/Chart/Inline/PieChart.php @@ -5,17 +5,13 @@ namespace Icinga\Chart\Inline; use Icinga\Chart\PieChart as PieChartRenderer; -use Imagick; -use Exception; -use Icinga\Exception\IcingaException; /** * Draw an inline pie-chart directly from the available request parameters. */ class PieChart extends Inline { - - public function render($output = true) + protected function getChart() { $pie = new PieChartRenderer(); $pie->alignTopLeft(); @@ -23,23 +19,24 @@ class PieChart extends Inline $pie->drawPie(array( 'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels )); + return $pie; + } + + public function toSvg($output = true) + { if ($output) { - echo $pie->render(); + echo $this->getChart()->render(); } else { - return $pie->render(); + return $this->getChart()->render(); } } - public function toPng() + public function toPng($output = true) { - if (! class_exists('Imagick')) { - // TODO: This is quick & dirty. 404? - throw new IcingaException('Cannot render PNGs without Imagick'); + if ($output) { + echo $this->getChart()->toPng($this->width, $this->height); + } else { + return $this->getChart()->toPng($this->width, $this->height); } - $image = new Imagick(); - $image->readImageBlob($this->render(false)); - $image->setImageFormat('png24'); - $image->resizeImage($this->width, $this->height, imagick::FILTER_LANCZOS, 1); - echo $image; } } diff --git a/library/Icinga/File/Pdf.php b/library/Icinga/File/Pdf.php index 44d1a107a..a372cbfdf 100644 --- a/library/Icinga/File/Pdf.php +++ b/library/Icinga/File/Pdf.php @@ -49,7 +49,6 @@ class Pdf extends DOMPDF $html = $layout->render(); $imgDir = Url::fromPath('img'); $html = preg_replace('~src="' . $imgDir . '/~', 'src="' . Icinga::app()->getBootstrapDirectory() . '/img/', $html); - $html = preg_replace('~src="/svg/chart.php(.*)"~', 'class="icon" src="http://master1.com/png/chart.php$1"', $html); $this->load_html($html); $this->render(); $this->stream( diff --git a/library/Icinga/Web/Widget/Chart/InlinePie.php b/library/Icinga/Web/Widget/Chart/InlinePie.php index 3ff97b2df..90cf3e875 100644 --- a/library/Icinga/Web/Widget/Chart/InlinePie.php +++ b/library/Icinga/Web/Widget/Chart/InlinePie.php @@ -4,10 +4,12 @@ namespace Icinga\Web\Widget\Chart; +use Icinga\Chart\PieChart; use Icinga\Web\Widget\AbstractWidget; use Icinga\Web\Url; use Icinga\Util\Format; use Icinga\Application\Logger; +use Icinga\Exception\IcingaException; /** * A SVG-PieChart intended to be displayed as a small icon next to labels, to offer a better visualization of the @@ -375,6 +377,22 @@ EOD; */ public function render() { + if ($this->view()->layout()->getLayout() === 'pdf') { + $pie = new PieChart(); + $pie->alignTopLeft(); + $pie->disableLegend(); + $pie->drawPie(array( + 'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels + )); + + try { + $png = $pie->toPng($this->width, $this->height); + return ''; + } catch (IcingaException $_) { + return ''; + } + } + $template = $this->template; $template = str_replace('{url}', $this->url, $template); diff --git a/modules/setup/library/Setup/WebWizard.php b/modules/setup/library/Setup/WebWizard.php index 72cd4f286..53a6bb8a8 100644 --- a/modules/setup/library/Setup/WebWizard.php +++ b/modules/setup/library/Setup/WebWizard.php @@ -461,8 +461,8 @@ class WebWizard extends Wizard implements SetupWizard mt('setup', 'PHP Module: GD'), mt( 'setup', - 'In case you want icons and graphs being exported to PDF' - . ' as well, you\'ll need the GD extension for PHP.' + 'In case you want icons being exported to PDF as' + . ' well, you\'ll need the GD extension for PHP.' ), Platform::extensionLoaded('gd'), Platform::extensionLoaded('gd') ? mt('setup', 'The PHP module GD is available') : ( @@ -470,6 +470,19 @@ class WebWizard extends Wizard implements SetupWizard ) ); + $requirements->addOptional( + mt('setup', 'PHP Module: Imagick'), + mt( + 'setup', + 'In case you want graphs being exported to PDF as well' + . ', you\'ll need the ImageMagick extension for PHP.' + ), + Platform::extensionLoaded('imagick'), + Platform::extensionLoaded('imagick') ? mt('setup', 'The PHP module Imagick is available') : ( + mt('setup', 'The PHP module Imagick is missing') + ) + ); + $requirements->addOptional( mt('setup', 'PHP Module: PDO-MySQL'), mt(