Skip to content

Commit

Permalink
feat: Add support for aspectRatio (#28941)
Browse files Browse the repository at this point in the history
* Add support for aspectRatio

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

Co-authored-by: Matt Kane <matt@gatsbyjs.com>

* handle width too, refactor accordingly

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

Co-authored-by: Matt Kane <matt@gatsbyjs.com>

* work for fixed too

Co-authored-by: Matt Kane <matt@gatsbyjs.com>
  • Loading branch information
LB and ascorbic committed Jan 11, 2021
1 parent e0f86b0 commit ed19fa0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-image/src/babel-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const SHARP_ATTRIBUTES = new Set([
`formats`,
`maxWidth`,
`maxHeight`,
`aspectRatio`,
`quality`,
`avifOptions`,
`jpgOptions`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IStaticImageProps extends Omit<GatsbyImageProps, "image"> {
height?: number
maxWidth?: number
maxHeight?: number
aspectRatio?: number
sizes?: string
quality?: number
transformOptions?: {
Expand Down Expand Up @@ -45,6 +46,7 @@ export function _getStaticImage(
maxWidth,
height,
maxHeight,
aspectRatio,
tracedSVGOptions,
placeholder,
formats,
Expand Down Expand Up @@ -117,6 +119,7 @@ export const propTypes = {
height: checkDimensionProps,
maxHeight: checkDimensionProps,
maxWidth: checkDimensionProps,
aspectRatio: checkDimensionProps,
sizes: PropTypes.string,
layout: (props: IStaticImageProps & IPrivateProps): Error | undefined => {
if (props.layout === undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-image/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ICommonImageProps {
export interface IFluidImageProps extends ICommonImageProps {
maxWidth?: number
maxHeight?: number
aspectRatio?: number
fit?: number
background?: number
}
Expand Down
13 changes: 13 additions & 0 deletions packages/gatsby-plugin-sharp/src/image-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ISharpGatsbyImageArgs {
height?: number
maxWidth?: number
maxHeight?: number
aspectRatio?: number
sizes?: string
quality?: number
transformOptions: {
Expand Down Expand Up @@ -137,6 +138,18 @@ export async function generateImageData({
}
}

if (args.aspectRatio) {
if (args.maxWidth && args.maxHeight) {
reporter.warn(
`Specifying aspectRatio along with both width and height will cause aspectRatio to be ignored.`
)
} else if (args.maxWidth) {
args.maxHeight = args.maxWidth / args.aspectRatio
} else if (args.maxHeight) {
args.maxWidth = args.maxHeight * args.aspectRatio
}
}

const formats = new Set(args.formats)
let useAuto = formats.has(``) || formats.has(`auto`) || formats.size === 0

Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-transformer-sharp/src/customize-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ const imageNodeType = ({
description: stripIndent`
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.`,
},
aspectRatio: {
type: GraphQLFloat,
description: stripIndent`
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.
If neither width or height is provided, height will be set based on the intrinsic width of the source image.
`,
},
placeholder: {
type: ImagePlaceholderType,
defaultValue: `blurred`,
Expand Down

0 comments on commit ed19fa0

Please sign in to comment.