Create simple image metacode upcast class
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / html / metacode / upcast / WsmMetacodeUpcast.class.php
1 <?php
2
3 namespace wcf\system\html\metacode\upcast;
4
5 use wcf\data\media\Media;
6 use wcf\system\cache\runtime\MediaRuntimeCache;
7 use wcf\util\DOMUtil;
8 use wcf\util\StringUtil;
9
10 /**
11 * @author Olaf Braun
12 * @copyright 2001-2024 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @since 6.1
15 */
16 final class WsmMetacodeUpcast extends ImageMetacodeUpcast
17 {
18 #[\Override]
19 public function upcast(\DOMElement $element, array $attributes): void
20 {
21 /**
22 * @var string $alignment
23 * @var string|int $width
24 * @var string $thumbnail
25 */
26 $mediaID = \intval($attributes[0]);
27 $thumbnail = $attributes[1] ?? 'original';
28 $alignment = $attributes[2] ?? 'none';
29 $width = $attributes[3] ?? 'auto';
30 $media = MediaRuntimeCache::getInstance()->getObject($mediaID);
31 $parentLink = $element->parentNode;
32 /** @var \DOMElement|null $parentLink */
33 if ($parentLink !== null && $parentLink->nodeName !== 'a') {
34 $parentLink = null;
35 }
36
37 $imgElement = $element->ownerDocument->createElement('img');
38 if ($thumbnail === 'original') {
39 $imgElement->setAttribute('src', StringUtil::decodeHTML($media->getLink()));
40 } else {
41 $imgElement->setAttribute('src', StringUtil::decodeHTML($media->getThumbnailLink($thumbnail)));
42 }
43 if ($width !== 'auto') {
44 $imgElement->setAttribute('width', \intval($width));
45 $imgElement->setAttribute('data-width', \intval($width) . 'px');
46 }
47 $imgElement->setAttribute('data-media-id', $mediaID);
48 $imgElement->setAttribute('data-media-size', StringUtil::decodeHTML($thumbnail));
49 $imgElement->setAttribute('style', $this->getStyle($media, $width, $thumbnail));
50 if ($alignment === 'none') {
51 $imgElement->setAttribute('class', 'image woltlabSuiteMedia');
52 DOMUtil::replaceElement($element, $imgElement);
53 return;
54 }
55 $imgElement->setAttribute('class', 'woltlabSuiteMedia');
56
57 $this->createFigure($element, $imgElement, $alignment, $parentLink);
58 }
59
60 #[\Override]
61 public function hasValidAttributes(array $attributes): bool
62 {
63 // 1-4 attributes
64 if (\count($attributes) < 1 || \count($attributes) > 4) {
65 return false;
66 }
67 $media = MediaRuntimeCache::getInstance()->getObject($attributes[0]);
68 if ($media === null) {
69 return false;
70 }
71 if (!$media->isAccessible()) {
72 return false;
73 }
74 // Other media types must be converted to the text [wsm…][/wsm]
75 return (bool)$media->isImage;
76 }
77
78 #[\Override]
79 public function cacheObject(array $attributes): void
80 {
81 MediaRuntimeCache::getInstance()->cacheObjectID($attributes[0] ?? 0);
82 }
83
84 private function getStyle(Media $media, string|int $width, string $thumbnail): string
85 {
86 if ($thumbnail === 'original') {
87 $maxWidth = $media->width;
88 } else {
89 $maxWidth = $media->getThumbnailWidth($thumbnail);
90 }
91 return \sprintf(
92 'max-width: %dpx; width: %s;',
93 $maxWidth,
94 \is_numeric($width) && $width > 0 ? \intval($width) . 'px' : 'auto'
95 );
96 }
97 }