Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Satori uses the same Flexbox [layout engine](https://yogalayout.com) as React Na
<tr><td><code>backgroundColor</code></td><td>Supported, single value</td><td></td></tr>
<tr><td><code>backgroundImage</code></td><td><code>linear-gradient</code>, <code>repeating-linear-gradient</code>, <code>radial-gradient</code>, <code>repeating-radial-gradient</code>, <code>url</code>, single value</td><td></td></tr>
<tr><td><code>backgroundPosition</code></td><td>Support single value</td><td></td></tr>
<tr><td><code>backgroundSize</code></td><td>Support two-value size i.e. <code>10px 20%</code></td><td></td></tr>
<tr><td><code>backgroundSize</code></td><td>Support <code>cover</code>, <code>contain</code>, <code>auto</code>, and two-value sizes i.e. <code>10px 20%</code></td><td><a href="https://og-playground.vercel.app/?share=ZZXXjqNIFIZfpeWb2RW9Itik3tmRwAQDJgeDNTfkHEwwYdTvvnhGWq003HDOqZ8fDlX11Y9D2Ebx4ePwNcqf35u3t2Fcq_ifHz9e8dtbFudpNn68wRD0_qsy59GY_b8Q-GGZ9u3UREbcxf4u_tK0f_U_4y-_acx8i3dF2Dajnze_jwu1n74EU1_9Efmj_5G_CmDXpH8H_hBjp_fcoVVjhiQ-ban9Ukw7Y-10j3j9ldtnyttvND6LubMHTABVrO4YJypeXGSdBtuwwCRWANMRpy7W49jmUFvuyU0fnmZeduAiZ6ZZaueUXM0V5VDBNs2OtfX6MimW0iTTBD0JeYbFeSOrpLGR5_iMkrHGYSAGEFJl77xRCsjNDC_MmIHV8KDOdsboszJAXchEZifrokEtSyrT5cUkbkifODIuyKC2oblXy8yAqyt-VLcJr8UhYJc2PGoWrW4dppebvMnexThypKZD-IRPx2dFjjnj3eAxqMU1XLTipNKFhrv-1Te3ZWE41aKHtHUogVtC98ZlniyqI9l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">Example</a></td></tr>
<tr><td><code>backgroundClip</code></td><td><code>border-box</code>, <code>text</code></td><td></td></tr>
<tr><td><code>backgroundRepeat</code></td><td><code>repeat</code>, <code>repeat-x</code>, <code>repeat-y</code>, <code>no-repeat</code>, defaults to <code>repeat</code></td><td></td></tr>

Expand Down
136 changes: 116 additions & 20 deletions src/builder/background-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,61 @@ function toAbsoluteValue(v: string | number, base: number) {
return +v
}

function calculateKeywordSize(
keyword: string,
containerWidth: number,
containerHeight: number,
imageWidth: number,
imageHeight: number
): [number, number] {
if (!imageWidth || !imageHeight) {
return [containerWidth, containerHeight]
}

if (keyword === 'cover') {
// Scale to cover the container (use max scale to ensure it covers)
const scaleX = containerWidth / imageWidth
const scaleY = containerHeight / imageHeight
const scale = Math.max(scaleX, scaleY)
return [imageWidth * scale, imageHeight * scale]
}

if (keyword === 'contain') {
// Scale to fit within the container (use min scale to ensure it fits)
const scaleX = containerWidth / imageWidth
const scaleY = containerHeight / imageHeight
const scale = Math.min(scaleX, scaleY)
return [imageWidth * scale, imageHeight * scale]
}

// For 'auto' or other values, handle auto
if (keyword === 'auto' || keyword.includes('auto')) {
const parts = keyword.split(' ')
const widthPart = parts[0] || 'auto'
const heightPart = parts[1] || parts[0] || 'auto'

let finalWidth = imageWidth
let finalHeight = imageHeight

if (widthPart === 'auto' && heightPart !== 'auto') {
// Width is auto, height is specified
const parsedHeight = toAbsoluteValue(heightPart, containerHeight)
finalHeight = parsedHeight
finalWidth = (imageWidth / imageHeight) * parsedHeight
} else if (heightPart === 'auto' && widthPart !== 'auto') {
// Height is auto, width is specified
const parsedWidth = toAbsoluteValue(widthPart, containerWidth)
finalWidth = parsedWidth
finalHeight = (imageHeight / imageWidth) * parsedWidth
}
// If both are auto, use intrinsic dimensions

return [finalWidth, finalHeight]
}

return [containerWidth, containerHeight]
}

function parseLengthPairs(
str: string,
{
Expand Down Expand Up @@ -76,12 +131,35 @@ export default async function backgroundImage(
const repeatX = repeat === 'repeat-x' || repeat === 'repeat'
const repeatY = repeat === 'repeat-y' || repeat === 'repeat'

const dimensions = parseLengthPairs(size, {
x: width,
y: height,
defaultX: width,
defaultY: height,
})
// Check if size is a keyword (cover, contain, auto) that needs to be calculated later
const isKeywordSize =
size &&
(size === 'cover' ||
size === 'contain' ||
size === 'auto' ||
size.includes('auto'))

// For gradients, keyword sizes (cover, contain, auto) resolve to the
// container dimensions since gradients have no intrinsic size.
// For url() images, keyword sizes are calculated later using the image's
// intrinsic dimensions.
const isGradient =
image.startsWith('linear-gradient(') ||
image.startsWith('repeating-linear-gradient(') ||
image.startsWith('radial-gradient(') ||
image.startsWith('repeating-radial-gradient(')

const dimensions =
isKeywordSize && isGradient
? [width, height] // Gradients have no intrinsic size; keyword sizes resolve to container
: isKeywordSize
? [0, 0] // Will be calculated later when we have image dimensions
: parseLengthPairs(size, {
x: width,
y: height,
defaultX: width,
defaultY: height,
})
const offsets = parseLengthPairs(position, {
x: width,
y: height,
Expand Down Expand Up @@ -118,23 +196,41 @@ export default async function backgroundImage(
}

if (image.startsWith('url(')) {
const dimensionsWithoutFallback = parseLengthPairs(size, {
x: width,
y: height,
defaultX: 0,
defaultY: 0,
})
const [src, imageWidth, imageHeight] = await resolveImageData(
image.slice(4, -1)
)
const resolvedWidth =
from === 'mask'
? imageWidth || dimensionsWithoutFallback[0]
: dimensionsWithoutFallback[0] || imageWidth
const resolvedHeight =
from === 'mask'
? imageHeight || dimensionsWithoutFallback[1]
: dimensionsWithoutFallback[1] || imageHeight

let resolvedWidth: number
let resolvedHeight: number

if (isKeywordSize) {
// Calculate dimensions based on keyword (cover, contain, auto)
const [calcWidth, calcHeight] = calculateKeywordSize(
size,
width,
height,
imageWidth,
imageHeight
)
resolvedWidth = calcWidth
resolvedHeight = calcHeight
} else {
// Use the previously parsed dimensions
const dimensionsWithoutFallback = parseLengthPairs(size, {
x: width,
y: height,
defaultX: 0,
defaultY: 0,
})
resolvedWidth =
from === 'mask'
? imageWidth || dimensionsWithoutFallback[0]
: dimensionsWithoutFallback[0] || imageWidth
resolvedHeight =
from === 'mask'
? imageHeight || dimensionsWithoutFallback[1]
: dimensionsWithoutFallback[1] || imageHeight
}

return [
`satori_bi${id}`,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions test/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,74 @@ describe('background-image: url()', () => {
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support background-size: cover', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
backgroundImage: `url(${PNG_SAMPLE})`,
backgroundSize: 'cover',
}}
></div>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support background-size: contain', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
backgroundImage: `url(${PNG_SAMPLE})`,
backgroundSize: 'contain',
}}
></div>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support background-size: auto', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
backgroundImage: `url(${PNG_SAMPLE})`,
backgroundSize: 'auto',
}}
></div>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should support background-size: cover with non-square container', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
backgroundImage: `url(${PNG_SAMPLE})`,
backgroundSize: 'cover',
}}
></div>,
{ width: 200, height: 100, fonts }
)

expect(toImage(svg, 200)).toMatchImageSnapshot()
})

it('should correctly position the background pattern', async () => {
const svg = await satori(
<div
Expand Down