Skip to content

Commit

Permalink
fix(gatsby-plugin-image): Correctly calculate aspect ratio when width…
Browse files Browse the repository at this point in the history
… and height are both set (#30466)
  • Loading branch information
ascorbic committed Mar 26, 2021
1 parent 2f87f89 commit 35c90f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-image/src/__tests__/image-utils.ts
Expand Up @@ -311,6 +311,11 @@ describe(`the helper utils`, () => {
expect(url).toEqual(`https://example.com/afile.jpg/20/15/image.jpg`)
})

it(`gets a low-resolution image URL when width and height are set`, () => {
const url = getLowResolutionImageURL({ ...args, width: 200, height: 200 })
expect(url).toEqual(`https://example.com/afile.jpg/20/20/image.jpg`)
})

it(`gets a low-resolution image URL with correct aspect ratio`, () => {
const url = getLowResolutionImageURL({
...fullWidthArgs,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-image/src/image-utils.ts
Expand Up @@ -168,7 +168,7 @@ export function setDefaultDimensions(
layout = camelCase(layout) as Layout

if (width && height) {
return { ...args, formats, layout }
return { ...args, formats, layout, aspectRatio: width / height }
}
if (sourceMetadata.width && sourceMetadata.height && !aspectRatio) {
aspectRatio = sourceMetadata.width / sourceMetadata.height
Expand Down

0 comments on commit 35c90f8

Please sign in to comment.