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
1 change: 1 addition & 0 deletions src/lib/api-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function resolveDimensions(
height?: number
): DimensionResolution {
if (width !== undefined) width = Math.round(width);
if (height !== undefined) height = Math.round(height);

if (width !== undefined && (width < OG_MIN_WIDTH || width > OG_MAX_WIDTH)) {
return {
Expand Down
22 changes: 20 additions & 2 deletions tests/api/og.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ describe('resolveDimensions', () => {
});
});

it('rounds a fractional height to a whole number', () => {
expect(resolveDimensions(undefined, 315.6)).toEqual({
success: true,
width: 602,
height: 316,
});
expect(resolveDimensions(undefined, 175.2)).toEqual({
success: true,
width: 333,
height: 175,
});
});

it('derives width when only height is supplied', () => {
expect(resolveDimensions(undefined, 315)).toEqual({
success: true,
Expand Down Expand Up @@ -225,6 +238,11 @@ describe('resolveDimensions', () => {
width: 2400,
height: 1260,
});
expect(resolveDimensions(600.4, 315.2)).toEqual({
success: true,
width: 600,
height: 315,
});
});

it('rejects mismatched aspect ratios when both dimensions are supplied', () => {
Expand Down Expand Up @@ -558,15 +576,15 @@ describe('OG API dimension resolution and scaling', () => {
);
});

it('scales down to 601 when height is fractional', async () => {
it('scales down to a whole-number width when height is fractional', async () => {
const { renderToPng } = await import('@/lib/og-engine');
vi.mocked(renderToPng).mockClear();

const res = await GET(makeContext(`${BASE}?template=blog-minimal-dark&title=Hi&height=315.5`));
expect(res.status).toBe(200);
expect(renderToPng).toHaveBeenCalledWith(
expect.anything(),
{ width: 1200, height: 630, scaleDown: 601 }
{ width: 1200, height: 630, scaleDown: 602 }
);
});

Expand Down
Loading