Skip to content

Commit 5bc0b36

Browse files
FineWolfDSchau
authored andcommitted
Add option to keep metadata in files processed by gatsby-plugin-sharp (#10210)
* Add option to keep metadata in files processed by `gatsby-plugin-sharp` * Since pluginOptions are strictly type-checked, no need for the strict check for false. Co-Authored-By: FineWolf <[email protected]> * Changed README.md and index.js based on recommendations from @DSchau * Spelling corrections in README.md * Fixed Pngquant not respecting the stripMetadata option
1 parent 1c0d051 commit 5bc0b36

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

packages/gatsby-plugin-sharp/README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ images. By default it uses a quality setting of [50-75].
2222

2323
```javascript
2424
// In your gatsby-config.js
25-
plugins: [`gatsby-plugin-sharp`]
25+
plugins: [
26+
{
27+
resolve: `gatsby-plugin-sharp`,
28+
options: {
29+
useMozJpeg: false,
30+
stripMetadata: true,
31+
},
32+
},
33+
]
2634
```
2735

2836
## Methods
@@ -242,12 +250,37 @@ You can opt-in to use [MozJPEG][16] for jpeg-encoding. MozJPEG provides even
242250
better image compression than the default encoder used in `gatsby-plugin-sharp`.
243251
However, when using MozJPEG the build time of your Gatsby project will increase
244252
significantly.
245-
To enable MozJPEG set the [environment variable](/docs/environment-variables/#environment-variables):
253+
254+
To enable MozJPEG, you can set the `useMozJpeg` plugin option to `true` in
255+
`gatsby-config.js`.
256+
257+
For backwards compatible reasons, if `useMozJpeg` is not defined in the plugin
258+
options, the [environment variable](/docs/environment-variables/#environment-variables)
259+
`GATSBY_JPEG_ENCODER` acts as a fallback if set to `MOZJPEG`:
246260

247261
```shell
248262
GATSBY_JPEG_ENCODER=MOZJPEG
249263
```
250264

265+
### EXIF and ICC metadata
266+
267+
By default, `gatsby-plugin-sharp` strips all EXIF, ICC and other metadata
268+
present in your source file. This is the recommended default as it leads to
269+
smaller file sizes.
270+
271+
However, in situations where you wish to preserve EXIF metadata or ICC profiles
272+
(example: you are building a photography portfolio and wish to conserve
273+
the color profile or the copyright information of the photos you've exported
274+
from Adobe Lightroom or Phase One's Capture One), you can set the `stripMetadata`
275+
plugin option to `false` in `gatsby-config.js`.
276+
277+
It is important to note that if `stripMetadata` is set to `false`, **all**
278+
metadata information will be preserved from the source image, including but not
279+
limited to the latitude/longitude information of where the picture was taken
280+
(if present). If you wish to strip this information from the source file, you
281+
can either leave `stripMetadata` to its default of `true`, or manually
282+
pre-process your images with a tool such as [ExifTool][17].
283+
251284
[1]: https://alistapart.com/article/finessing-fecolormatrix
252285
[2]: http://blog.72lions.com/blog/2015/7/7/duotone-in-js
253286
[3]: https://ines.io/blog/dynamic-duotone-svg-jade
@@ -264,3 +297,4 @@ GATSBY_JPEG_ENCODER=MOZJPEG
264297
[14]: https://github.com/oliver-moran/jimp
265298
[15]: http://sharp.dimens.io/en/stable/api-operation/#flatten
266299
[16]: https://github.com/mozilla/mozjpeg
300+
[17]: https://www.sno.phy.queensu.ca/~phil/exiftool/

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { setBoundActionCreators } = require(`./index`)
1+
const { setBoundActionCreators, setPluginOptions } = require(`./index`)
22

3-
exports.onPreInit = ({ actions }) => {
3+
exports.onPreInit = ({ actions }, pluginOptions) => {
44
setBoundActionCreators(actions)
5+
setPluginOptions(pluginOptions)
56
}
67

78
// TODO

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ exports.setBoundActionCreators = actions => {
4141
boundActionCreators = actions
4242
}
4343

44+
/// Plugin options are loaded onPreInit in gatsby-node
45+
const pluginDefaults = {
46+
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
47+
stripMetadata: true,
48+
}
49+
let pluginOptions = Object.assign({}, pluginDefaults)
50+
exports.setPluginOptions = opts => {
51+
pluginOptions = Object.assign({}, pluginOptions, opts)
52+
}
53+
4454
// Promisify the sharp prototype (methods) to promisify the alternative (for
4555
// raw) callback-accepting toBuffer(...) method
4656
Promise.promisifyAll(sharp.prototype, { multiArgs: true })
@@ -110,8 +120,6 @@ const healOptions = (args, defaultArgs) => {
110120
return options
111121
}
112122

113-
const useMozjpeg = process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`
114-
115123
let totalJobs = 0
116124
const processFile = (file, jobs, cb, reporter) => {
117125
// console.log("totalJobs", totalJobs)
@@ -124,7 +132,14 @@ const processFile = (file, jobs, cb, reporter) => {
124132

125133
let pipeline
126134
try {
127-
pipeline = sharp(file).rotate()
135+
pipeline = sharp(file)
136+
137+
// Keep Metadata
138+
if (!pluginOptions.stripMetadata) {
139+
pipeline = pipeline.withMetadata()
140+
}
141+
142+
pipeline = pipeline.rotate()
128143
} catch (err) {
129144
reportError(`Failed to process image ${file}`, err, reporter)
130145
jobs.forEach(job => job.outsideReject(err))
@@ -170,7 +185,7 @@ const processFile = (file, jobs, cb, reporter) => {
170185
})
171186

172187
// jpeg
173-
if (!useMozjpeg) {
188+
if (!pluginOptions.useMozJpeg) {
174189
clonedPipeline = clonedPipeline.jpeg({
175190
quality: args.quality,
176191
progressive: args.jpegProgressive,
@@ -231,6 +246,7 @@ const processFile = (file, jobs, cb, reporter) => {
231246
args.quality + 25,
232247
100
233248
)}`, // e.g. 40-65
249+
strip: !!pluginOptions.stripMetadata, // Must be a bool
234250
}),
235251
],
236252
})
@@ -242,7 +258,7 @@ const processFile = (file, jobs, cb, reporter) => {
242258
.catch(onFinish)
243259
// Compress jpeg
244260
} else if (
245-
useMozjpeg &&
261+
pluginOptions.useMozJpeg &&
246262
((job.file.extension === `jpg` && args.toFormat === ``) ||
247263
(job.file.extension === `jpeg` && args.toFormat === ``) ||
248264
args.toFormat === `jpg`)

0 commit comments

Comments
 (0)