Add multiple gradient stops and image fill modes

This commit is contained in:
2026-11-28 02:19:28 +08:00
parent a0df3e60de
commit 0d99a3475b
10 changed files with 337 additions and 39 deletions
+57 -4
View File
@@ -27,6 +27,13 @@ final class CanvasStyle
'strike-on' => 'text-decoration-line:line-through', 'strike-off' => 'text-decoration-line:none',
];
private const PREFIX = 'studio-style-';
private const IMAGE_SIZES = [
'cover' => 'cover', 'contain' => 'contain', 'stretch' => '100% 100%', 'original' => 'auto', 'tile' => 'auto',
];
private const IMAGE_POSITIONS = [
'c' => 'center', 'tl' => 'left top', 'tc' => 'center top', 'tr' => 'right top',
'ml' => 'left center', 'mr' => 'right center', 'bl' => 'left bottom', 'bc' => 'center bottom', 'br' => 'right bottom',
];
public static function encode(array $presentation): string
{
@@ -175,18 +182,64 @@ final class CanvasStyle
if (preg_match('/^gradient-(0|[1-9]\d{0,2})-([a-f0-9]{6})-([a-f0-9]{6})$/D', $token, $parts) && (int) $parts[1] <= 360) {
return "background:linear-gradient({$parts[1]}deg,#{$parts[2]},#{$parts[3]})!important";
}
if (!preg_match('/^image-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
if (str_starts_with($token, 'gradient-')) {
return self::gradient(substr($token, strlen('gradient-')));
}
$size = 'cover';
$position = 'c';
if (preg_match('/^image-v2-([a-z]+)-([a-z]+)-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
[, $size, $position, $encoded] = $parts;
} elseif (preg_match('/^image-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
$encoded = $parts[1];
} else {
return null;
}
$url = base64_decode(strtr($parts[1], '-_', '+/'), true);
if (!isset(self::IMAGE_SIZES[$size], self::IMAGE_POSITIONS[$position])) {
return null;
}
$url = base64_decode(strtr($encoded, '-_', '+/'), true);
if ($url === false || strlen($url) > 1000 || !mb_check_encoding($url, 'UTF-8') || !Html::url($url, true)
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $parts[1]) {
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $encoded) {
return null;
}
// Quote the URL for CSS and keep the result safe inside a raw-text HTML style element.
$escaped = str_replace(['"', '<', '>'], ['\\22 ', '\\3c ', '\\3e '], $url);
return 'background-image:url("' . $escaped . '")!important;background-position:center!important;background-size:cover!important;background-repeat:no-repeat!important';
$repeat = $size === 'tile' ? 'repeat' : 'no-repeat';
return 'background-image:url("' . $escaped . '")!important;background-position:' . self::IMAGE_POSITIONS[$position]
. '!important;background-size:' . self::IMAGE_SIZES[$size] . '!important;background-repeat:' . $repeat . '!important';
}
private static function gradient(string $token): ?string
{
$parts = explode('-', $token, 15);
$type = array_shift($parts);
if ($type === 'linear') {
$angle = array_shift($parts);
if ($angle === null || !preg_match('/^(0|[1-9]\d{0,2})$/D', $angle) || (int) $angle > 360) {
return null;
}
$direction = $angle . 'deg';
} elseif ($type === 'radial') {
$direction = 'circle at center';
} else {
return null;
}
if (count($parts) < 2 || count($parts) > 12) {
return null;
}
$last = 0;
$stops = [];
foreach ($parts as $stop) {
if (!preg_match('/^(0|[1-9]\d?|100)_([a-f0-9]{6})$/D', $stop, $color) || (int) $color[1] < $last) {
return null;
}
$last = (int) $color[1];
$stops[] = "#{$color[2]} {$last}%";
}
return "background:{$type}-gradient($direction," . implode(',', $stops) . ')!important';
}
public static function dynamicRules(string $markup): string