data->get('attributes'); $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links'); if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl())) { $attrs['src'] = ''; } else { $attrs['src'] = $node->getUrl(); } $attrs['alt'] = $this->getAltText($node); if (($title = $node->getTitle()) !== null) { $attrs['title'] = $title; } if (str_starts_with($node->getUrl(), 'https://') === false) { $imagePath = __DIR__ . '/../../public/' . $node->getUrl(); if (file_exists($imagePath) === false) { return ''; } $imageSize = getimagesize($imagePath); if ($imageSize !== false) { $attrs['width'] = (string)$imageSize[0]; $attrs['height'] = (string)$imageSize[1]; $attrs['style'] = 'max-height: ' . min((int)$attrs['height'] + 60, 540) . 'px'; } $attrs['loading'] = 'lazy'; } return new HtmlElement('img', $attrs, '', true); } public function setConfiguration(ConfigurationInterface $configuration): void { $this->config = $configuration; } public function getXmlTagName(Node $node): string { return 'image'; } /** * @param Image $node * * @return array * * @psalm-suppress MoreSpecificImplementedParamType */ public function getXmlAttributes(Node $node): array { Image::assertInstanceOf($node); return [ 'destination' => $node->getUrl(), 'title' => $node->getTitle() ?? '', ]; } private function getAltText(Image $node): string { $altText = ''; foreach ((new NodeIterator($node)) as $n) { if ($n instanceof StringContainerInterface) { $altText .= $n->getLiteral(); } elseif ($n instanceof Newline) { $altText .= "\n"; } } return $altText; } }