Skip to content

Instantly share code, notes, and snippets.

@shuaibird
Created July 9, 2017 01:55
Show Gist options
  • Save shuaibird/39346605dac8806a2fdabbef49e9ec2a to your computer and use it in GitHub Desktop.
Save shuaibird/39346605dac8806a2fdabbef49e9ec2a to your computer and use it in GitHub Desktop.
embed watermark to images (pass different option when needed)
const watermark = require('image-watermark')
const path = require('path')
const fs = require('fs')
class WatermarkOptions {
constructor({ text, color, align, override, resize } = {}) {
this.text = text || 'Shadowrocks'
this.color = color || 'rgb(222, 222, 222)'
this.align = align || 'ltr'
this['override-image'] = override
this.resize = resize
}
}
// Get an array of dirs
process.argv.slice(2)
.map(argv => path.resolve(__dirname, argv))
// Filter the non-existences and non-dirs out
.filter((dir) => {
let stats
try {
stats = fs.statSync(dir)
} catch (err) {
console.log(err.message)
}
return stats && stats.isDirectory()
})
.forEach((dir) => {
fs.readdir(dir, (err, files) => {
files
// Filter the non-images out
.filter(file => /\.(png|jpg|jpeg|gif)$/.test(file))
.forEach(file => {
watermark.embedWatermark(
path.resolve(dir, file),
new WatermarkOptions({ override: true, resize: '50%' })
)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment