-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
adds ImageTransform.background #15153
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: f68d125 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
15fd3aa to
db9458f
Compare
|
I missed a few other spots that needed updating. Doing that now. @Princesseuh Could you explain why this doesn't check other things for equality, e.g. astro/packages/astro/src/assets/internal.ts Lines 172 to 175 in 34f84c2
Edit: Ah, I think I get it! But now I don't get why |
|
Alright, everything fixed now. |
|
|
||
| // if background is set, flatten the image with the specified background | ||
| if (transform.background) { | ||
| result.flatten({ background: transform.background }); |
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 property is also relevant for when you crop an image isn't it? I assume it should be passed there as well
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.
@Princesseuh Since we flatten() when it's set, any subsequent resize() will be on the flattened image that has the background already.
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.
@Princesseuh …but, it might actually make sense to flatten() only after any resize(), since this means less pixel operations. Would you want me to move it there?
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.
@Princesseuh Since we
flatten()when it's set, any subsequentresize()will be on the flattened image that has the background already.
But can't a resize introduce new alpha depending on the cropping strategy? Would it include the background?
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.
@Princesseuh you're right, I don't know if Sharp keeps a state of "oh this image is flattened now, so if the canvas expands I should fill it with this background color". I think it's safer to move flatten() at the end. I'll do that now.
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.
@Princesseuh Done!
Princesseuh
left a comment
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 good with me!
sarah11918
left a comment
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.
Thanks for providing this new feature! Left a note on the kind of changeset message we'd typically like to have!
Also, for further docs, we need to add an API reference for the new option in this section: https://docs.astro.build/en/reference/modules/astro-assets/#imagetransform (I think it's fine to add it to the end of the list, after position)
And, for example, if this is something actually set on the <Image /> component itself, then it would need to be added here https://docs.astro.build/en/reference/modules/astro-assets/#image- (I think it's fine to stick it on at the end, after priority.)
You can follow the model for the existing properties in each place this is needed. Under ImageTransform you can see the descriptions are very minimal. Properties of the <Image /> component include an initial API reference block and then the content can be something like the changeset content here, but un-hyped. Just a matter-of-fact explanation of what it is/does, and an example showing it in use.
As I explain below, I'm not sure where the user sets this value, so the key point is to make sure that it's obvious to the reader how to get started and use this new feature. That's where I'll need your help!
.changeset/shaky-bananas-clap.md
Outdated
| 'astro': minor | ||
| --- | ||
|
|
||
| Add `background` in `ImageTransform`, to allow flattening images with the given background color. This is especially useful when the requested output format doesn't support an alpha channel, e.g. `jpeg`. |
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.
Just a note here that for a minor release of a new feature, we'd usually hype it up a bit more, focusing on what a user can now do (with an example usage shown) rather than implementation details. You can see a model of this in our contributing docs.
In this case, I suspect that maybe talking about the ImageTransform type here, and possibly even flattening, is more of an implementation detail? More commonly, we'd stick to wording that closer describes what someone is trying to achieve, like in the PR description itself ("providing a background color in image transforms.")
You are the subject matter expert here, and I don't really have a good idea of how this is used: added as a property on the <Image /> component which will then be used when the image is transformed? Is this passed when using something like one of the image transform functions?
So this is just very rough, and I'm only throwing words together; may not be accurate, but we'd expect a changeset for a new feature added to guide someone though what it looks like to use the new feature something like:
Adds a new `background` property to the `<Image />` component
This optional property allows you to provide a background color for image transformations. By default, images converted to another format by Sharp use a default black background. Providing a value for `background` on an `<Image />` component, or passing it to the `getImage()` helper, will flatten images using that color instead.
This is especially useful when the requested output format doesn't support an alpha channel (e.g. `jpeg`) and can't support transparent backgrounds.
```
show an example of this in use
```
See more about this new property in [the image reference docs](https://docs.astro.build/en/reference/modules/astro-assets/#background)
That's the general idea anyway:
- Quick sentence to shout "New! Shiny!" and grab people's attention without a lot of visual clutter
- describe the problem it solves or ability it unlocks and why that's cool
- show an example that people could copy into their projects right away to try it out so that it's easy for them to get started
- link to the docs for the new thing
You know the feature better than I do, so you'll need to describe what makes sense to show it off, but that's the pattern!
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.
Thanks! I'll make the necessary changes over the weekend.
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.
Regarding the reference docs, those should probably mention that
backgroundsupports any syntax supported bycolor-string.
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.
@sarah11918 I see you're hardcoding the locale in https://docs.astro.build/en/reference/modules/astro-assets/ instead of relying on the automatic resolution of the current locale using https://docs.astro.build/reference/modules/astro-assets/. Is this what's expected? I'm OK with it but I think it can lead to links in a bogus locale incorrectly leaking into translations.
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.
@sarah11918 How is the new changeset?
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.
Changeset is good @jcayzac ! See my one tiny suggestion.
https://docs.astro.build/en/reference/modules/astro-assets/ instead of relying on the automatic resolution of the current locale using https://docs.astro.build/reference/modules/astro-assets/. Is this what's expected?
All docs links are written with a locale, even the English ones. That's the URL people will arrive at anyway, and always see in their browser, so no reason to create a redirect!
fix outdated comment add missing url param handling, and remove support for object move sharp's flatten() after any resize()
12902ab to
80da645
Compare
80da645 to
9d108fd
Compare
sarah11918
left a comment
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 PR is looking great to me! (One small suggestion). Just a reminder that we don't merge this PR or consider the feature done until docs are added, though, so please do make a Docs PR to add background! You can follow the existing examples, and if you need it find info on how we document the API references (yes, including the line break 😅 ) in the contributing guide here: https://contribute.docs.astro.build/reference/api-references/
.changeset/shaky-bananas-clap.md
Outdated
| 'astro': minor | ||
| --- | ||
|
|
||
| Add `background` in `ImageTransform`, to allow flattening images with the given background color. This is especially useful when the requested output format doesn't support an alpha channel, e.g. `jpeg`. |
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.
Changeset is good @jcayzac ! See my one tiny suggestion.
https://docs.astro.build/en/reference/modules/astro-assets/ instead of relying on the automatic resolution of the current locale using https://docs.astro.build/reference/modules/astro-assets/. Is this what's expected?
All docs links are written with a locale, even the English ones. That's the URL people will arrive at anyway, and always see in their browser, so no reason to create a redirect!
Co-authored-by: Sarah Rainsberger <[email protected]>
@sarah11918 Committed, thanks!
@Princesseuh started a PR here. I would like to amend it, but I don't have any push access to the branch she created. Is it OK to make a PR that targets her branch, so we'll merge the combined result to |
Changes
This adds support for providing a background color in image transforms. Right now Astro has no support for that, so the background of anything converted to e.g.
jpegis always Sharp's default black.Testing
I'd need some help figuring how to implement a test for this. Is there a way to render a JPEG to a
<canvas>in a headless browser inside Astro tests?Docs
Some documentation updates are definitely needed for this, to document the new field.
/cc @withastro/maintainers-docs for feedback!