diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
commit | a0901c4b7f2db488cb4fb3be2dd921a0308f4659 (patch) | |
tree | fafb393cf330a60df129ff10d0059eb7b14052a7 /library/Icingadb/Widget/IconImage.php | |
parent | Initial commit. (diff) | |
download | icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.tar.xz icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/Icingadb/Widget/IconImage.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/library/Icingadb/Widget/IconImage.php b/library/Icingadb/Widget/IconImage.php new file mode 100644 index 0000000..fcf25c8 --- /dev/null +++ b/library/Icingadb/Widget/IconImage.php @@ -0,0 +1,74 @@ +<?php + +/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */ + +namespace Icinga\Module\Icingadb\Widget; + +use ipl\Html\BaseHtmlElement; +use ipl\Html\HtmlDocument; +use ipl\Web\Url; +use ipl\Web\Widget\Icon; + +class IconImage extends BaseHtmlElement +{ + /** @var string */ + protected $source; + + /** @var ?string */ + protected $alt; + + protected $tag = 'img'; + + /** + * Create a new icon image + * + * @param string $source + * @param ?string $alt The alternative text + */ + public function __construct(string $source, ?string $alt) + { + $this->source = $source; + $this->alt = $alt; + } + + public function renderUnwrapped() + { + if (! $this->getAttributes()->has('src')) { + // If it's an icon we don't need the <img> tag + return ''; + } + + return parent::renderUnwrapped(); + } + + protected function assemble() + { + $src = $this->source; + + if (strpos($src, '.') === false) { + $this->setWrapper((new HtmlDocument())->addHtml(new Icon($src))); + return; + } + + if (strpos($src, '/') === false) { + $src = 'img/icons/' . $src; + } + + if (getenv('ICINGAWEB_EXPORT_FORMAT') === 'pdf') { + $srcUrl = Url::fromPath($src); + $srcPath = $srcUrl->getRelativeUrl(); + if (! $srcUrl->isExternal() && file_exists($srcPath) && is_file($srcPath)) { + $mimeType = @mime_content_type($srcPath); + $content = @file_get_contents($srcPath); + if ($mimeType !== false && $content !== false) { + $src = "data:$mimeType;base64," . base64_encode($content); + } + } + } + + $this->addAttributes([ + 'src' => $src, + 'alt' => $this->alt + ]); + } +} |