Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add limited support for objectPosition property on images #464

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
36 changes: 36 additions & 0 deletions src/builder/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export function getPreserveAspectRatio(
objectFit: string,
objectPosition?: string
) {
if (objectFit !== 'contain' && objectFit !== 'cover') {
return 'none'
}

let align = 'xMidYMid'

if (objectPosition) {
const parts = objectPosition.split(' ')

if (parts.length === 2) {
const [x, y] = parts

if (x === 'left') {
align = 'xMin'
} else if (x === 'right') {
align = 'xMax'
} else {
align = 'XMid'
}

if (y === 'top') {
align += 'YMin'
} else if (y === 'bottom') {
align += 'YMax'
} else {
align += 'YMid'
}
}
}

return align + (objectFit === 'cover' ? ' slice' : '')
}
11 changes: 5 additions & 6 deletions src/builder/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { buildXMLString } from '../utils.js'
import border, { getBorderClipPath } from './border.js'
import { genClipPath } from './clip-path.js'
import buildMaskImage from './mask-image.js'
import { getPreserveAspectRatio } from './image.js'

export default async function rect(
{
Expand Down Expand Up @@ -200,12 +201,10 @@ export default async function rect(
((style.borderBottomWidth as number) || 0) +
((style.paddingBottom as number) || 0)

const preserveAspectRatio =
style.objectFit === 'contain'
? 'xMidYMid'
: style.objectFit === 'cover'
? 'xMidYMid slice'
: 'none'
const preserveAspectRatio = getPreserveAspectRatio(
String(style.objectFit),
String(style.objectPosition)
)

shape += buildXMLString('image', {
x: left + offsetLeft,
Expand Down
49 changes: 49 additions & 0 deletions test/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,55 @@ describe('Image', () => {

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

it('should convert objectFit', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
}}
>
<img
width='100%'
height='100%'
src='https://via.placeholder.com/150'
style={{
objectFit: 'cover',
}}
/>
</div>,
{ width: 100, height: 100, fonts }
)

expect(svg).toContain('preserveAspectRatio="xMidYMid slice"')
})

it('should convert objectPosition', async () => {
const svg = await satori(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
}}
>
<img
width='100%'
height='100%'
src='https://via.placeholder.com/150'
style={{
objectFit: 'cover',
objectPosition: 'right top',
}}
/>
</div>,
{ width: 100, height: 100, fonts }
)

expect(svg).toContain('preserveAspectRatio="xMaxYMin slice"')
})
})

describe('background-image: url()', () => {
Expand Down