Skip to content

Commit 494ad07

Browse files
VGoosewardpeet
authored andcommitted
feat(*): add options fit and background to image sharp (#13078)
gatsby-plugin-sharp - add `fit` and `background` options to control cropping when using fluid gatsby-transformer-sharp - add new `ImageFitType` type and add `fit` and `background` to `fluidNodeType` args. ## Related Issues fixes #12972.
1 parent c4d6a65 commit 494ad07

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

packages/gatsby-plugin-sharp/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,21 @@ a base64 image to use as a placeholder) you need to implement the "blur up"
9696
technique popularized by Medium and Facebook (and also available as a Gatsby
9797
plugin for Markdown content as gatsby-remark-images).
9898

99+
When both a `maxWidth` and `maxHeight` are provided, sharp will use `COVER` as a fit strategy by default. This might not be ideal so you can now choose between `COVER`, `CONTAIN` and `FILL` as a fit strategy. To see them in action the [CSS property object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) comes close to its implementation.
100+
101+
#### Note
102+
103+
fit strategies `CONTAIN` and `FILL` will not work when `cropFocus` is assigned to [sharp.strategy][6]. The `cropFocus` option cannot be `ENTROPY` or `ATTENTION`
104+
99105
#### Parameters
100106

101107
- `maxWidth` (int, default: 800)
102108
- `maxHeight` (int)
103109
- `quality` (int, default: 50)
104110
- `sizeByPixelDensity` (bool, default: false)
105111
- `srcSetBreakpoints` (array of int, default: [])
112+
- `fit` (string, default: '[sharp.fit.cover][6]')
113+
- `background` (string, default: 'rgba(0,0,0,1)')
106114

107115
#### Returns
108116

@@ -308,3 +316,4 @@ pre-process your images with a tool such as [ExifTool][17].
308316
[15]: http://sharp.dimens.io/en/stable/api-operation/#flatten
309317
[16]: https://github.com/mozilla/mozjpeg
310318
[17]: https://www.sno.phy.queensu.ca/~phil/exiftool/
319+
[18]: https://www.npmjs.com/package/color

packages/gatsby-plugin-sharp/src/__tests__/process-file.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe(`createArgsDigest`, () => {
1212
grayscale: false,
1313
rotate: 0,
1414
duotone: null,
15+
fit: `COVER`,
16+
background: `rgb(0,0,0,1)`,
1517
}
1618

1719
describe(`changes hash if used args are different`, () => {
@@ -47,12 +49,14 @@ describe(`createArgsDigest`, () => {
4749
testHashDifferent(`rotate change`, {
4850
rotate: defaultArgsBaseline.rotate + 1,
4951
})
50-
testHashDifferent(`dutone change`, {
52+
testHashDifferent(`duotone change`, {
5153
duotone: {
5254
highlight: `#ff0000`,
5355
shadow: `#000000`,
5456
},
5557
})
58+
testHashDifferent(`fit change`, { fit: `CONTAIN` })
59+
testHashDifferent(`background change`, { background: `#fff0` })
5660
})
5761

5862
describe(`doesn't change hash if not used options are different`, () => {

packages/gatsby-plugin-sharp/src/process-file.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const argsWhitelist = [
4242
`grayscale`,
4343
`rotate`,
4444
`duotone`,
45+
`fit`,
46+
`background`,
4547
]
4648

4749
/**
@@ -104,6 +106,8 @@ exports.processFile = (file, transforms, options = {}) => {
104106
clonedPipeline
105107
.resize(roundedWidth, roundedHeight, {
106108
position: args.cropFocus,
109+
fit: args.fit,
110+
background: args.background,
107111
})
108112
.png({
109113
compressionLevel: args.pngCompressionLevel,

packages/gatsby-transformer-sharp/src/extend-node-type.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
ImageCropFocusType,
2929
DuotoneGradientType,
3030
PotraceType,
31+
ImageFitType,
3132
} = require(`./types`)
3233

3334
function toArray(buf) {
@@ -285,6 +286,14 @@ const fluidNodeType = ({
285286
type: ImageCropFocusType,
286287
defaultValue: sharp.strategy.attention,
287288
},
289+
fit: {
290+
type: ImageFitType,
291+
defaultValue: sharp.fit.cover,
292+
},
293+
background: {
294+
type: GraphQLString,
295+
defaultValue: `rgba(0,0,0,1)`,
296+
},
288297
rotate: {
289298
type: GraphQLInt,
290299
defaultValue: 0,

packages/gatsby-transformer-sharp/src/types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const ImageFormatType = new GraphQLEnumType({
1919
},
2020
})
2121

22+
const ImageFitType = new GraphQLEnumType({
23+
name: `ImageFit`,
24+
values: {
25+
COVER: { value: sharp.fit.cover },
26+
CONTAIN: { value: sharp.fit.contain },
27+
FILL: { value: sharp.fit.fill },
28+
},
29+
})
30+
2231
const ImageCropFocusType = new GraphQLEnumType({
2332
name: `ImageCropFocus`,
2433
values: {
@@ -78,6 +87,7 @@ const PotraceType = new GraphQLInputObjectType({
7887

7988
module.exports = {
8089
ImageFormatType,
90+
ImageFitType,
8191
ImageCropFocusType,
8292
DuotoneGradientType,
8393
PotraceType,

0 commit comments

Comments
 (0)