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(gatsby-plugin-image): Add support for backgroundColor in sharp #29141

Merged
merged 2 commits into from
Jan 22, 2021

Conversation

ascorbic
Copy link
Contributor

Unifies API to support backgroundColor on both the component and the resolver. This is a breaking API change, as poreviously the resolver took background. However that was not actually implemented, so was ignored.

New behaviour is to pass the prop to sharp, so when resizing with a crop fit of e.g. contain, sharp uses that as the background color for letterboxing. The value is also set as the background color of the container, meaning that if the image has transparency, if there is no placeholder, or the placeholder is tracedSVG then the background is visible.

[ch22667]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 22, 2021
@laurieontech laurieontech added topic: media Related to gatsby-plugin-image, or general image/media processing topics and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 22, 2021
Copy link
Contributor

@laurieontech laurieontech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks solid. Left a few comments but should be good when those are addressed.

@@ -118,11 +119,11 @@ export async function generateImageData({
reporter.warn(
`Specifying fullWidth images will ignore the width and height arguments, you may want a constrained image instead. Otherwise, use the breakpoints argument.`
)
args.width = metadata.width
args.width = metadata?.width
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a scenario where metadata will be undefined entirely? It looks like in the worst case it's an empty object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sharp throws an error getting metadata it would be undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it so it's an empty object though

...args.webpOptions,
width,
height: Math.round(width / imageSizes.aspectRatio),
toFormat: `webp`,
background: backgroundColor,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we passing this if we already have sharedOptions? That seems redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Left that in after I refactored the others into sharedOptions

toFormatBase64: args.blurredOptions?.toFormat,
width: placeholderWidth,
height: Math.round(placeholderWidth / imageSizes.aspectRatio),
background: backgroundColor,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. sharedOptions is already passed.

@@ -504,9 +504,8 @@ const imageNodeType = ({
type: TransformOptionsType,
description: `Options to pass to sharp to control cropping and other image manipulations.`,
},
background: {
backgroundColor: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we have any other doc where this needs to be changed? I don't think so, but wanted to mention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think we mention it

Copy link
Contributor

@laurieontech laurieontech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@ascorbic ascorbic added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Jan 22, 2021
@gatsbybot gatsbybot merged commit eb2bede into master Jan 22, 2021
@gatsbybot gatsbybot deleted the feat/background-color branch January 22, 2021 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants