Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for aspectRatio #28941

Merged
merged 6 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 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
18 changes: 18 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,23 @@ export async function generateImageData({
}
}

// note this will need to changed but keeping consistent for now
if (args.maxWidth && args.maxHeight && args.aspectRatio) {
reporter.warn(
`Specifying aspectRatio along with height may cause aspectRatio to be ignored.`
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
)
}

// todo should we allow this for fixed too?
if (
args.aspectRatio &&
args.maxWidth &&
!args.maxHeight &&
layout !== `fixed`
) {
args.maxHeight = args.maxWidth / args.aspectRatio
}

laurieontech marked this conversation as resolved.
Show resolved Hide resolved
const formats = new Set(args.formats)
let useAuto = formats.has(``) || formats.has(`auto`) || formats.size === 0

Expand Down
4 changes: 4 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,10 @@ 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, the image will be cropped using the width of the generated image in relation to the provided aspect ratio.`,
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
},
placeholder: {
type: ImagePlaceholderType,
defaultValue: `blurred`,
Expand Down