Skip to content

Commit

Permalink
fix: crop horizontally when aspect ratio is reduced
Browse files Browse the repository at this point in the history
  • Loading branch information
geigerzaehler committed Nov 27, 2024
1 parent 6f2516e commit ad550f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-beers-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'imagetools-core': patch
---

Reducing the aspect ratio without providing width or height works correctly now
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions packages/core/src/transforms/__tests__/resize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ describe('aspect', () => {
expect(await image.toBuffer()).toMatchImageSnapshot()
})

test('w/ crop horizontally', async () => {
//@ts-expect-error we know this is safe
const { image } = await applyTransforms([resize({ aspect: '1:2' }, dirCtx)], img)

const { width = 0, height = 0 } = await sharp(await image.toBuffer()).metadata()
expect(width / height).toEqual(1 / 2)

expect(await image.toBuffer()).toMatchImageSnapshot()
})

test('w/ fit', async () => {
//@ts-expect-error we know this is safe
const { image } = await applyTransforms([resize({ aspect: '4:3', fit: 'contain' }, dirCtx)], img)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transforms/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const resize: TransformFactory<ResizeOptions> = (config, context) => {
finalWidth = originalWidth
} else {
finalHeight = originalHeight
finalWidth = originalHeight / aspect
finalWidth = originalHeight * aspect
}
} else if (width && height) {
// width & height BOTH given, need to look at fit
Expand Down

0 comments on commit ad550f0

Please sign in to comment.