Skip to content

Commit

Permalink
Add support for custom GraphicsMagick output arguments (e.g. sharpen)
Browse files Browse the repository at this point in the history
This will help support thumbsup/thumbsup#89
  • Loading branch information
rprieto committed May 13, 2018
1 parent bdeac72 commit 832178f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ opts = {
tile: false
}
}

// custom post-processing using GraphicsMagick output arguments
options = {
args: [
'-unsharp 2 0.5 0.7 0',
'-modulate 120'
]
}
```

Note: watermarks are not compatible with cropped images.
Expand Down
18 changes: 18 additions & 0 deletions lib/gmargs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Applies an array of GraphicsMagick string arguments to a <gm> instance
e.g. apply(image, ['--modulate 120'])
*/
exports.apply = function (image, args) {
if (args && args.length) {
args.forEach(arg => {
const index = arg.indexOf(' ')
if (index === -1) {
image.out(arg)
} else {
const command = arg.substr(0, index)
const values = arg.substr(index + 1)
image.out(command, values)
}
})
}
}
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const async = require('async')
const gm = require('gm')
const mkdirp = require('mkdirp')
const path = require('path')
const gmargs = require('./gmargs')
const ffmpeg = require('./ffmpeg')

/*
Expand Down Expand Up @@ -41,6 +42,8 @@ exports.image = function (source, target, options, callback) {
}
// default quality, for typical web-friendly sizes
image.quality(options.quality || 90)
// apply custom post-processing arguments (sharpen, brightness...)
gmargs.apply(image, options.args)
// write the output image
image.write(target, callback)
}
Expand Down
Binary file added test-data/expected/images/desk.processed.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions test/gmargs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const gmargs = require('../lib/gmargs')
const sinon = require('sinon')
const tape = require('tape')

function gm () {
return {
out: sinon.stub()
}
}

tape('no arguments', t => {
t.plan(1)
const image = gm()
gmargs.apply(image, undefined)
t.equal(image.out.callCount, 0)
t.end()
})

tape('empty array of arguments', t => {
t.plan(1)
const image = gm()
gmargs.apply(image, [])
t.equal(image.out.callCount, 0)
t.end()
})

tape('single argument with no values', t => {
t.plan(2)
const image = gm()
gmargs.apply(image, ['-equalize'])
t.equal(image.out.callCount, 1)
t.deepEqual(image.out.args[0], ['-equalize'])
t.end()
})

tape('single argument with one value', t => {
t.plan(2)
const image = gm()
gmargs.apply(image, ['-modulate 120'])
t.equal(image.out.callCount, 1)
t.deepEqual(image.out.args[0], ['-modulate', '120'])
t.end()
})

tape('single argument with space-separated values', t => {
t.plan(2)
const image = gm()
gmargs.apply(image, ['-unsharp 2 0.5 0.5 0'])
t.equal(image.out.callCount, 1)
t.deepEqual(image.out.args[0], ['-unsharp', '2 0.5 0.5 0'])
t.end()
})

tape('multiple arguments', t => {
t.plan(2)
const image = gm()
gmargs.apply(image, [
'-equalize',
'-modulate 120',
'-unsharp 2 0.5 0.5 0'
])
t.equal(image.out.callCount, 3)
t.deepEqual(image.out.args, [
['-equalize'],
['-modulate', '120'],
['-unsharp', '2 0.5 0.5 0']
])
t.end()
})
10 changes: 10 additions & 0 deletions test/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ tape('ignores the watermark when cropping', (test) => {
})
})

tape('can add custom post-processing arguments', (test) => {
diff.image(test, {
input: 'images/desk.jpg',
expect: 'images/desk.processed.jpg',
options: {
args: ['-equalize', '-modulate 120']
}
})
})

const ORIENTATIONS = [1, 2, 3, 4, 5, 6, 7, 8]

ORIENTATIONS.forEach((orientation) => {
Expand Down

0 comments on commit 832178f

Please sign in to comment.