-
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-utils): support aspect ratio for Image Service #35087
Conversation
We should use aspectRatio when calculating crop & size instead of passing it through sharp. gatsby/packages/gatsby-plugin-image/src/image-utils.ts Lines 152 to 197 in c0d2eba
|
@@ -76,6 +80,66 @@ describe(`gatsbyImageData`, () => { | |||
}, | |||
} | |||
|
|||
it(`should return proper image props for aspect ratio and automatically add "fit" prop`, async () => { |
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.
Yay for adding tests!!
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.
🙌
Got it! I have a couple Q's about that:
|
|
both |
if (aspectRatio) { | ||
// crop must be set for aspectRatio to work | ||
if (!cropFocus) { | ||
args.push(`fit=crop`) |
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.
There is already a fit
parameter in gatsbyImageResolver
that should be used instead of the hard coded fit=crop
.
Also - fit should be set even if there isn't an 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.
This is actually a bit of a special case. We use fit
only to calculate width
and height` so that locally processed images and those processed with Image CDN on Gatsby Cloud are identical in terms of size and crop.
Because of the way that aspectRatio
is handled on Gatsby Cloud, it is required that fit be either "crop" or "fill". Here I make sure that the fit
param is added if it does not exist already so that aspectRatio
will actually be used on the Gatsby Cloud side
@@ -28,10 +28,12 @@ export function generateImageArgs({ | |||
format, | |||
cropFocus, | |||
quality, | |||
aspectRatio, |
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.
it would also be good to pass along the backgroundColor
via the url so that when people use fit=contain
the letterboxing can be done with the appropriate colour
packages/gatsby-plugin-utils/src/polyfill-remote-file/__tests__/gatsby-image-resolver.ts
Outdated
Show resolved
Hide resolved
packages/gatsby-plugin-utils/src/polyfill-remote-file/jobs/dispatchers.ts
Outdated
Show resolved
Hide resolved
43b3167
to
964b373
Compare
Description
We were not passing along the
aspectRatio
arg passed into theGatsbyImage
field / resolver to the underlying query string arg builder and thus we weren't actually doing anything withaspectRatio
. This fixes that and adds a couple tests!