Add BG offset

This commit is contained in:
orasche committed 2026-08-14 10:59:22 +02:00
1 parent c720696781
commit 58990ebe8d
6 files changed
+161 -5

No files matched your search

@@ -74,7 +74,7 @@ class ContainerBackgroundRenderer
return (string)json_encode([
'image' => $image,
'size' => $this->resolveSize($row),
'position' => (string)($row['tx_vitec_bg_position'] ?? 'center center'),
'position' => $this->resolvePosition($row),
]);
} catch (\Throwable $e) {
return '';
@@ -101,4 +101,54 @@ class ContainerBackgroundRenderer
$percent = (int)($row['tx_vitec_bg_size_percent'] ?? 0);
return $percent > 0 ? $percent . '%' : 'auto';
}
/**
* The stored position, again resolved into one CSS-ready value.
*
* The four custom fields are edge offsets, but CSS cannot take a top AND a
* bottom offset at once, so they are folded into the percentage form. That
* form says exactly the same thing: in `background-position` a percentage
* aligns the same point of image and container, so "10% from the right edge"
* IS `90%`. Nothing is lost in the translation.
*
* Per axis the near edge wins when both are filled - `left` over `right`,
* `top` over `bottom` - because two opposing offsets contradict each other
* and silently picking one beats emitting something invalid.
*
* @param array<string,mixed> $row
*/
private function resolvePosition(array $row): string
{
$position = (string)($row['tx_vitec_bg_position'] ?? 'center center');
if ($position !== 'custom') {
return $position;
}
$x = $this->axisPercent($row, 'tx_vitec_bg_pos_left', 'tx_vitec_bg_pos_right');
$y = $this->axisPercent($row, 'tx_vitec_bg_pos_top', 'tx_vitec_bg_pos_bottom');
return $x . '% ' . $y . '%';
}
/**
* One axis as a percentage. An empty field means "centred on this axis" -
* which is why the columns are nullable: 0 is a legitimate offset (flush
* against that edge) and has to stay distinguishable from "not set".
*
* @param array<string,mixed> $row
*/
private function axisPercent(array $row, string $nearField, string $farField): int
{
$near = $row[$nearField] ?? null;
if ($near !== null && $near !== '') {
return (int)$near;
}
$far = $row[$farField] ?? null;
if ($far !== null && $far !== '') {
return 100 - (int)$far;
}
return 50;
}
}