Created
August 23, 2018 00:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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