Skip to content

Commit cf0e77b

Browse files
cpboydfreiksenet
authored andcommitted
feat(image-sharp): add trim option (#14137)
* feat(sharp): add trim option * fix: trim option only as number
1 parent 524817a commit cf0e77b

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

packages/gatsby-plugin-sharp/src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ async function generateBase64({ file, args, reporter }) {
154154
return null
155155
}
156156

157+
if (options.trim) {
158+
pipeline = pipeline.trim(options.trim)
159+
}
160+
157161
const forceBase64Format =
158162
args.toFormatBase64 || pluginOptions.forceBase64Format
159163
if (forceBase64Format) {
@@ -384,6 +388,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
384388
duotone: options.duotone,
385389
grayscale: options.grayscale,
386390
rotate: options.rotate,
391+
trim: options.trim,
387392
toFormat: options.toFormat,
388393
toFormatBase64: options.toFormatBase64,
389394
width: base64Width,

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

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const argsWhitelist = [
4141
`jpegProgressive`,
4242
`grayscale`,
4343
`rotate`,
44+
`trim`,
4445
`duotone`,
4546
`fit`,
4647
`background`,
@@ -57,6 +58,7 @@ const argsWhitelist = [
5758
* @property {boolean} jpegProgressive
5859
* @property {boolean} grayscale
5960
* @property {number} rotate
61+
* @property {number} trim
6062
* @property {object} duotone
6163
*/
6264

@@ -89,6 +91,10 @@ exports.processFile = (file, transforms, options = {}) => {
8991

9092
let clonedPipeline = transforms.length > 1 ? pipeline.clone() : pipeline
9193

94+
if (args.trim) {
95+
clonedPipeline = clonedPipeline.trim(args.trim)
96+
}
97+
9298
if (!args.rotate) {
9399
clonedPipeline = clonedPipeline.rotate()
94100
}

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

+28
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,22 @@ const fixedNodeType = ({
161161
type: ImageCropFocusType,
162162
defaultValue: sharp.strategy.attention,
163163
},
164+
fit: {
165+
type: ImageFitType,
166+
defaultValue: sharp.fit.cover,
167+
},
168+
background: {
169+
type: GraphQLString,
170+
defaultValue: `rgba(0,0,0,1)`,
171+
},
164172
rotate: {
165173
type: GraphQLInt,
166174
defaultValue: 0,
167175
},
176+
trim: {
177+
type: GraphQLFloat,
178+
defaultValue: false,
179+
},
168180
},
169181
resolve: (image, fieldArgs, context) => {
170182
const file = getNodeAndSavePathDependency(image.parent, context.path)
@@ -310,6 +322,10 @@ const fluidNodeType = ({
310322
type: GraphQLInt,
311323
defaultValue: 0,
312324
},
325+
trim: {
326+
type: GraphQLFloat,
327+
defaultValue: false,
328+
},
313329
sizes: {
314330
type: GraphQLString,
315331
defaultValue: ``,
@@ -488,10 +504,22 @@ module.exports = ({
488504
type: ImageCropFocusType,
489505
defaultValue: sharp.strategy.attention,
490506
},
507+
fit: {
508+
type: ImageFitType,
509+
defaultValue: sharp.fit.cover,
510+
},
511+
background: {
512+
type: GraphQLString,
513+
defaultValue: `rgba(0,0,0,1)`,
514+
},
491515
rotate: {
492516
type: GraphQLInt,
493517
defaultValue: 0,
494518
},
519+
trim: {
520+
type: GraphQLFloat,
521+
defaultValue: 0,
522+
},
495523
},
496524
resolve: (image, fieldArgs, context) => {
497525
const file = getNodeAndSavePathDependency(image.parent, context.path)

0 commit comments

Comments
 (0)