Skip to content

Commit

Permalink
Breaking change: make the watermark API more obvious
Browse files Browse the repository at this point in the history
Before you could specify both “gravity” and “tile”, which didn’t make sense.
The name “tile” was also awkwardly close to the other argument “file”.

The new API with “position: Repeat” is more user friendly.
  • Loading branch information
rprieto committed May 15, 2018
1 parent b62c68b commit 01f713c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ opts = { quality: 80 }

##### Watermark

You can overlay a transparent watermark over the final image:

```js
opts = {
watermark: {
// PNG file with transparency, relative to the working directory
file: 'path/watermark.png',
// NorthWest | North | NorthEast | West | Center | East | SouthWest | South | SouthEast
gravity: 'SouthEast',
// whether the watermark should be repeated across the whole image
tile: false
file: 'path/watermark.png', // transparent PNG
position: 'NorthEast' // position of the watermark
}
}
```

The possible values for `position` are:

- `Repeat` to repeat the watermark across the whole image
- `Center` to position the watermark in the middle
- `NorthWest`, `North`, `NorthEast`, `West`, `East`, `SouthWest`, `South`, `SouthEast` to position the watermark along the edge

![watermark](docs/watermark.jpg) ![tiled](docs/watermark-tiled.jpg)

Note: watermarks are not compatible with cropped images.
The `watermark` option will simply be ignored if both width and height are specified.

Expand Down
Binary file added docs/watermark-tiled.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/watermark.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ exports.image = function (source, target, options, callback) {
const cropping = options.width && options.height
if (options.watermark && !cropping) {
image.composite(options.watermark.file)
if (options.watermark.tile) {
if (options.watermark.position === 'Repeat') {
image.tile(options.watermark.file)
}
if (options.watermark.gravity) {
image.gravity(options.watermark.gravity)
} else if (typeof options.watermark.position === 'string') {
image.gravity(options.watermark.position)
} else {
image.gravity('SouthEast')
}
Expand Down
4 changes: 2 additions & 2 deletions test/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ tape('can add a watermark in a given location', (test) => {
options: {
watermark: {
file: 'test-data/input/images/watermark.png',
gravity: 'NorthEast'
position: 'NorthEast'
}
}
})
Expand All @@ -107,7 +107,7 @@ tape('can add a tiled watermark', (test) => {
options: {
watermark: {
file: 'test-data/input/images/watermark.png',
tile: true
position: 'Repeat'
}
}
})
Expand Down

0 comments on commit 01f713c

Please sign in to comment.