-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ export async function generateImageData({ | |
tracedSVGOptions = {}, | ||
transformOptions = {}, | ||
quality, | ||
backgroundColor, | ||
} = args | ||
|
||
args.formats = args.formats || [`auto`, `webp`] | ||
|
@@ -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 | ||
args.height = undefined | ||
} | ||
|
||
if (!args.width && !args.height && metadata.width) { | ||
if (!args.width && !args.height && metadata?.width) { | ||
args.width = metadata.width | ||
} | ||
|
||
|
@@ -187,15 +188,19 @@ export async function generateImageData({ | |
reporter, | ||
}) | ||
|
||
const sharedOptions = { | ||
quality, | ||
...transformOptions, | ||
fit, | ||
cropFocus, | ||
background: backgroundColor, | ||
} | ||
|
||
const transforms = imageSizes.sizes.map(outputWidth => { | ||
const width = Math.round(outputWidth) | ||
const transform = createTransformObject({ | ||
quality, | ||
...transformOptions, | ||
fit, | ||
cropFocus, | ||
...sharedOptions, | ||
...options, | ||
tracedSVGOptions, | ||
width, | ||
height: Math.round(width / imageSizes.aspectRatio), | ||
toFormat: primaryFormat, | ||
|
@@ -237,6 +242,7 @@ export async function generateImageData({ | |
const imageProps: IGatsbyImageData = { | ||
layout, | ||
placeholder: undefined, | ||
backgroundColor, | ||
images: { | ||
fallback: { | ||
src: primaryImage.src, | ||
|
@@ -251,10 +257,7 @@ export async function generateImageData({ | |
const transforms = imageSizes.sizes.map(outputWidth => { | ||
const width = Math.round(outputWidth) | ||
const transform = createTransformObject({ | ||
quality, | ||
...transformOptions, | ||
fit, | ||
cropFocus, | ||
...sharedOptions, | ||
...args.avifOptions, | ||
width, | ||
height: Math.round(width / imageSizes.aspectRatio), | ||
|
@@ -283,14 +286,12 @@ export async function generateImageData({ | |
const transforms = imageSizes.sizes.map(outputWidth => { | ||
const width = Math.round(outputWidth) | ||
const transform = createTransformObject({ | ||
quality, | ||
...transformOptions, | ||
fit, | ||
cropFocus, | ||
...sharedOptions, | ||
...args.webpOptions, | ||
width, | ||
height: Math.round(width / imageSizes.aspectRatio), | ||
toFormat: `webp`, | ||
background: backgroundColor, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we passing this if we already have sharedOptions? That seems redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Left that in after I refactored the others into sharedOptions |
||
}) | ||
if (pathPrefix) { | ||
transform.pathPrefix = pathPrefix | ||
|
@@ -317,13 +318,12 @@ export async function generateImageData({ | |
const { src: fallback } = await base64({ | ||
file, | ||
args: { | ||
...sharedOptions, | ||
...options, | ||
...transformOptions, | ||
fit, | ||
cropFocus, | ||
toFormatBase64: args.blurredOptions?.toFormat, | ||
width: placeholderWidth, | ||
height: Math.round(placeholderWidth / imageSizes.aspectRatio), | ||
background: backgroundColor, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. sharedOptions is already passed. |
||
}, | ||
reporter, | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,9 +504,8 @@ const imageNodeType = ({ | |
type: TransformOptionsType, | ||
description: `Options to pass to sharp to control cropping and other image manipulations.`, | ||
}, | ||
background: { | ||
backgroundColor: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I don't think we mention it |
||
type: GraphQLString, | ||
defaultValue: `rgba(0,0,0,0)`, | ||
description: `Background color applied to the wrapper. Also passed to sharp to use as a background when "letterboxing" an image to another aspect ratio.`, | ||
}, | ||
}, | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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