| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\Renderer\Image; |
| 4 |
|
| 5 |
class ImageRenderer { |
| 6 |
/** |
| 7 |
* @param array<string, mixed>|null $attributes |
| 8 |
* @return string |
| 9 |
*/ |
| 10 |
public function render($attributes) { |
| 11 |
$attrs = ImageAttrs::from_array($attributes); |
| 12 |
|
| 13 |
$imageUrl = $attrs->media->url->asUrl(); |
| 14 |
|
| 15 |
$sizeSlug = $attrs->sizeSlug->asText(); |
| 16 |
if ($sizeSlug !== '') { |
| 17 |
$sizeData = $attrs->media->sizes[$sizeSlug] ?? null; |
| 18 |
|
| 19 |
if ( |
| 20 |
$sizeData instanceof MediaSizeAttrs && |
| 21 |
$sizeData->url->isNotEmpty() |
| 22 |
) { |
| 23 |
$url = $sizeData->url->asUrl(); |
| 24 |
if ($url !== '') { |
| 25 |
$imageUrl = $url; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if ($imageUrl === '') { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
$imageStyles = ['display:block']; |
| 35 |
if ($attrs->aspectRatio->isNotEmpty()) { |
| 36 |
$imageStyles[] = 'aspect-ratio:' . $attrs->aspectRatio->asAttr(); |
| 37 |
} |
| 38 |
if ($attrs->scale->isNotEmpty()) { |
| 39 |
$imageStyles[] = 'object-fit:' . $attrs->scale->asAttr(); |
| 40 |
} |
| 41 |
if ($attrs->width->isNotEmpty()) { |
| 42 |
$imageStyles[] = 'width:' . $attrs->width->asAttr(); |
| 43 |
} |
| 44 |
if ($attrs->height->isNotEmpty()) { |
| 45 |
$imageStyles[] = 'height:' . $attrs->height->asAttr(); |
| 46 |
} |
| 47 |
if ($attrs->border->top->isNotEmpty()) { |
| 48 |
$imageStyles[] = 'border-top:' . $attrs->border->top->asAttr(); |
| 49 |
} |
| 50 |
if ($attrs->border->right->isNotEmpty()) { |
| 51 |
$imageStyles[] = 'border-right:' . $attrs->border->right->asAttr(); |
| 52 |
} |
| 53 |
if ($attrs->border->bottom->isNotEmpty()) { |
| 54 |
$imageStyles[] = 'border-bottom:' . $attrs->border->bottom->asAttr(); |
| 55 |
} |
| 56 |
if ($attrs->border->left->isNotEmpty()) { |
| 57 |
$imageStyles[] = 'border-left:' . $attrs->border->left->asAttr(); |
| 58 |
} |
| 59 |
if ($attrs->borderRadius->topLeft->isNotEmpty()) { |
| 60 |
$imageStyles[] = 'border-top-left-radius:' . $attrs->borderRadius->topLeft->asAttr(); |
| 61 |
} |
| 62 |
if ($attrs->borderRadius->topRight->isNotEmpty()) { |
| 63 |
$imageStyles[] = 'border-top-right-radius:' . $attrs->borderRadius->topRight->asAttr(); |
| 64 |
} |
| 65 |
if ($attrs->borderRadius->bottomLeft->isNotEmpty()) { |
| 66 |
$imageStyles[] = 'border-bottom-left-radius:' . $attrs->borderRadius->bottomLeft->asAttr(); |
| 67 |
} |
| 68 |
if ($attrs->borderRadius->bottomRight->isNotEmpty()) { |
| 69 |
$imageStyles[] = 'border-bottom-right-radius:' . $attrs->borderRadius->bottomRight->asAttr(); |
| 70 |
} |
| 71 |
|
| 72 |
$alt = $attrs->alt->isNotEmpty() ? $attrs->alt->asAttr() : $attrs->media->alt->asAttr(); |
| 73 |
|
| 74 |
$imageStyleString = \esc_attr(implode(';', $imageStyles)); |
| 75 |
|
| 76 |
$imgHtml = |
| 77 |
"<img |
| 78 |
src='{$imageUrl}' |
| 79 |
alt='{$alt}' |
| 80 |
style='{$imageStyleString}' |
| 81 |
/>"; |
| 82 |
|
| 83 |
if ($attrs->href->isNotEmpty()) { |
| 84 |
$target = $attrs->linkTarget->asAttr(); |
| 85 |
$href = $attrs->href->asUrl(); |
| 86 |
$relAttr = $target === '_blank' ? "rel='noopener noreferrer'" : ''; |
| 87 |
|
| 88 |
$imgHtml = |
| 89 |
"<a |
| 90 |
href='{$href}' |
| 91 |
target='{$target}' |
| 92 |
{$relAttr} |
| 93 |
> |
| 94 |
{$imgHtml} |
| 95 |
</a>"; |
| 96 |
} |
| 97 |
|
| 98 |
$captionHtml = ''; |
| 99 |
if ($attrs->caption->isNotEmpty()) { |
| 100 |
$caption = $attrs->caption->asHtml(); |
| 101 |
|
| 102 |
$captionHtml = |
| 103 |
"<figcaption class='tableberg-image-caption'> |
| 104 |
{$caption} |
| 105 |
</figcaption>"; |
| 106 |
} |
| 107 |
|
| 108 |
return |
| 109 |
"<figure |
| 110 |
class='tableberg-image-element' |
| 111 |
style='margin:0;line-height:1' |
| 112 |
> |
| 113 |
{$imgHtml} |
| 114 |
{$captionHtml} |
| 115 |
</figure>"; |
| 116 |
} |
| 117 |
} |
| 118 |
|