Skip to content

Commit

Permalink
feat: add option to configure default image quality
Browse files Browse the repository at this point in the history
Fixes #4873
  • Loading branch information
DSchau committed Aug 22, 2018
1 parent 0c042f6 commit 1b2a4b6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
23 changes: 22 additions & 1 deletion packages/gatsby-transformer-sharp/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# gatsby-transformer-sharp

Creates `ImageSharp` nodes from image types that are supported by the
[Sharp](https:/lovell/sharp) image processing library and provides
[Sharp][sharp] image processing library and provides
fields in their GraphQL types for processing your images in a variety of ways
including resizing, cropping, and creating responsive images.

Expand All @@ -21,6 +21,25 @@ module.exports = {
}
```

### Configure default quality

By default, all image operations presume a default quality threshold passed to [sharp][sharp] (currently configured to `50`). To set a default quality for all image operations, do so with a configurable threshold passed to the plugin like so:

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
`gatsby-plugin-sharp`,
{
resolve: `gatsby-transformer-sharp`,
options: {
defaultQuality: 60 // 60% of the time it works every time
}
}
]
}
```

## Parsing algorithm

It recognizes files with the following extensions as images.
Expand All @@ -33,3 +52,5 @@ It recognizes files with the following extensions as images.
- tiff

Each image file is parsed into a node of type `ImageSharp`.

[sharp]: (https:/lovell/sharp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const extendNodeType = require(`../extend-node-type`)
const { __DEFAULT_QUALITY__ } = extendNodeType

const getOptions = (options = {}) => {return {
type: {
name: `ImageSharp`,
},
pathPrefix: `/`,
getNodeAndSavePathDependency: jest.fn(),
reporter: jest.fn(),
...options,
}}

describe(`basic functionality`, () => {
it(`returns empty object if not ImageSharp type`, () => {
const options = getOptions({ type: `MarkdownRemark` })

expect(extendNodeType(options)).toEqual({})
})

it(`returns expected image operations`, () => {
const expected = [`fixed`, `resolutions`, `fluid`, `sizes`, `original`, `resize`]
.reduce((merged, key) => {
merged[key] = expect.any(Object)
return merged
}, {})

expect(extendNodeType(getOptions())).toEqual(expected)
})
})

describe(`quality`, () => {
const options = getOptions()

it(`uses fallback quality if not passed`, () => {
const node = extendNodeType(options);

[node.fixed, node.fluid]
.forEach(type => {
expect(type.args.quality.defaultValue).toBe(__DEFAULT_QUALITY__)
})
})

it(`allows for configuration of quality`, () => {
const defaultQuality = 100
const node = extendNodeType(options, { defaultQuality });

[node.fixed, node.fluid]
.forEach(type => {
expect(type.args.quality.defaultValue).toBe(defaultQuality)
})
})
})
13 changes: 10 additions & 3 deletions packages/gatsby-transformer-sharp/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const {
PotraceType,
} = require(`./types`)

const DEFAULT_QUALITY = 50

function toArray(buf) {
var arr = new Array(buf.length)

Expand All @@ -50,6 +52,7 @@ const fixedNodeType = ({
getNodeAndSavePathDependency,
reporter,
name,
defaultQuality,
}) => {
return {
type: new GraphQLObjectType({
Expand Down Expand Up @@ -128,7 +131,7 @@ const fixedNodeType = ({
},
quality: {
type: GraphQLInt,
defaultValue: 50,
defaultValue: defaultQuality,
},
toFormat: {
type: ImageFormatType,
Expand Down Expand Up @@ -169,6 +172,7 @@ const fluidNodeType = ({
getNodeAndSavePathDependency,
reporter,
name,
defaultQuality,
}) => {
return {
type: new GraphQLObjectType({
Expand Down Expand Up @@ -245,7 +249,7 @@ const fluidNodeType = ({
},
quality: {
type: GraphQLInt,
defaultValue: 50,
defaultValue: defaultQuality,
},
toFormat: {
type: ImageFormatType,
Expand Down Expand Up @@ -285,7 +289,7 @@ module.exports = ({
pathPrefix,
getNodeAndSavePathDependency,
reporter,
}) => {
}, { defaultQuality = DEFAULT_QUALITY } = {}) => {
if (type.name !== `ImageSharp`) {
return {}
}
Expand All @@ -295,6 +299,7 @@ module.exports = ({
pathPrefix,
getNodeAndSavePathDependency,
reporter,
defaultQuality,
}

// TODO: Remove resolutionsNode and sizesNode for Gatsby v3
Expand Down Expand Up @@ -451,3 +456,5 @@ module.exports = ({
},
}
}

module.exports.__DEFAULT_QUALITY__ = 50

0 comments on commit 1b2a4b6

Please sign in to comment.