summaryrefslogtreecommitdiffstats
path: root/library/Graphite/Web/Widget/InlineGraphImage.php
blob: 881384dd49f0f8b30ee407bdeb1f2aadf72c457b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace Icinga\Module\Graphite\Web\Widget;

use Icinga\Module\Graphite\Graphing\Chart;
use Icinga\Web\Widget\AbstractWidget;

class InlineGraphImage extends AbstractWidget
{
    /**
     * The image to be rendered
     *
     * @var GraphImage
     */
    protected $image;

    /**
     * The rendered <img>
     *
     * @var string|null
     */
    protected $rendered;

    /**
     * Constructor
     *
     * @param   Chart   $chart  The chart to be rendered
     */
    public function __construct(Chart $chart)
    {
        $this->image = new GraphImage($chart);
    }

    /**
     * Render the graph lazily
     *
     * @return string
     */
    public function render()
    {
        if ($this->rendered === null) {
            $this->rendered = '<img src="data:image/png;base64,'
                . implode("\n", str_split(base64_encode($this->image->render()), 76))
                . '">';
        }

        return $this->rendered;
    }
}