Skip to content

Instantly share code, notes, and snippets.

@eduardobcastro
Created August 23, 2018 00:29
Show Gist options
  • Save eduardobcastro/b2e13ec4b14bf684114eb73bb75b747f to your computer and use it in GitHub Desktop.
Save eduardobcastro/b2e13ec4b14bf684114eb73bb75b747f to your computer and use it in GitHub Desktop.
Example of how to send data through stdin and get results from stdout as buffers
const { readFile, writeFile } = require('fs')
const { spawn } = require('child_process')
function watermark(buffer, logoPath) {
return new Promise((resolve, reject) => {
let child = spawn('composite', ['-dissolve', '25%', '-gravity', 'SouthEast', '-geometry', '+10+10', logoPath, '-', '-'])
child.stdin.write(buffer)
child.stdin.end()
let chunks = []
child.stdout.on('data', function (data) {
chunks.push(data)
})
child.on('close', function (code) {
resolve(Buffer.concat(chunks))
})
child.on('error', reject)
})
}
readFile('myfile.jpg', function (err, buffer) {
watermark(buffer, 'mylogo.png').then(result => {
writeFile('output.png', result , err => {
if (err) return console.error(err)
})
}).catch(err => {
console.error('Watermark error:', err)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment