Skip to content

Commit ed19fa0

Browse files
LBascorbic
LB
andauthored
feat: Add support for aspectRatio (#28941)
* Add support for aspectRatio * Update packages/gatsby-plugin-sharp/src/image-data.ts Co-authored-by: Matt Kane <[email protected]> * handle width too, refactor accordingly * Update packages/gatsby-transformer-sharp/src/customize-schema.js Co-authored-by: Matt Kane <[email protected]> * work for fixed too Co-authored-by: Matt Kane <[email protected]>
1 parent e0f86b0 commit ed19fa0

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

packages/gatsby-plugin-image/src/babel-helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const SHARP_ATTRIBUTES = new Set([
1010
`formats`,
1111
`maxWidth`,
1212
`maxHeight`,
13+
`aspectRatio`,
1314
`quality`,
1415
`avifOptions`,
1516
`jpgOptions`,

packages/gatsby-plugin-image/src/components/static-image.server.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface IStaticImageProps extends Omit<GatsbyImageProps, "image"> {
1414
height?: number
1515
maxWidth?: number
1616
maxHeight?: number
17+
aspectRatio?: number
1718
sizes?: string
1819
quality?: number
1920
transformOptions?: {
@@ -45,6 +46,7 @@ export function _getStaticImage(
4546
maxWidth,
4647
height,
4748
maxHeight,
49+
aspectRatio,
4850
tracedSVGOptions,
4951
placeholder,
5052
formats,
@@ -117,6 +119,7 @@ export const propTypes = {
117119
height: checkDimensionProps,
118120
maxHeight: checkDimensionProps,
119121
maxWidth: checkDimensionProps,
122+
aspectRatio: checkDimensionProps,
120123
sizes: PropTypes.string,
121124
layout: (props: IStaticImageProps & IPrivateProps): Error | undefined => {
122125
if (props.layout === undefined) {

packages/gatsby-plugin-image/src/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ICommonImageProps {
2929
export interface IFluidImageProps extends ICommonImageProps {
3030
maxWidth?: number
3131
maxHeight?: number
32+
aspectRatio?: number
3233
fit?: number
3334
background?: number
3435
}

packages/gatsby-plugin-sharp/src/image-data.ts

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ISharpGatsbyImageArgs {
1919
height?: number
2020
maxWidth?: number
2121
maxHeight?: number
22+
aspectRatio?: number
2223
sizes?: string
2324
quality?: number
2425
transformOptions: {
@@ -137,6 +138,18 @@ export async function generateImageData({
137138
}
138139
}
139140

141+
if (args.aspectRatio) {
142+
if (args.maxWidth && args.maxHeight) {
143+
reporter.warn(
144+
`Specifying aspectRatio along with both width and height will cause aspectRatio to be ignored.`
145+
)
146+
} else if (args.maxWidth) {
147+
args.maxHeight = args.maxWidth / args.aspectRatio
148+
} else if (args.maxHeight) {
149+
args.maxWidth = args.maxHeight * args.aspectRatio
150+
}
151+
}
152+
140153
const formats = new Set(args.formats)
141154
let useAuto = formats.has(``) || formats.has(`auto`) || formats.size === 0
142155

packages/gatsby-transformer-sharp/src/customize-schema.js

+7
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ const imageNodeType = ({
434434
description: stripIndent`
435435
If set, the height of the generated image. If omitted, it is calculated from the supplied width, matching the aspect ratio of the source image.`,
436436
},
437+
aspectRatio: {
438+
type: GraphQLFloat,
439+
description: stripIndent`
440+
If set along with width or height, this will set the value of the other dimension to match the provided aspect ratio, cropping the image if needed.
441+
If neither width or height is provided, height will be set based on the intrinsic width of the source image.
442+
`,
443+
},
437444
placeholder: {
438445
type: ImagePlaceholderType,
439446
defaultValue: `blurred`,

0 commit comments

Comments
 (0)