I have an image and would like to apply a watermark (another image) over it using Node.js. The requirement is watermark shouldn't be a solid picture (what I can do with gm library's draw()) but half transparent. Can you please advise any tool to use?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use ImageMagick from the command line by forking a child-process
// Require our module dependencies
var exec = require('child_process').exec;
// Create command array to invoke ImageMagick composite where
// -dissolve is the amount of transparency for the watermark
// -gravity tells how to align images of varying size
// -quality is the image quality of the JPEG (not required if producing PNG)
var command = [
'composite',
'-dissolve', '50%',
'-gravity', 'center',
'-quality', 100,
'/path/to/watermark.png',
'/path/to/image.jpg',
'/path/to/save/result.jpg';
];
// Join command array by a space character and then execute command
exec(command.join(' '), function(err, stdout, stderr) {
// Do stuff with result here
});
If you need an API abstraction, there is a great module found here https://github.com/rsms/node-imagemagick
回答2:
You could make watermark image half-transparent already and use it with gm:
gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
console.log(e||'done'); // What would you like to do here?
});
回答3:
I use code
gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
console.log(e||'done'); // What would you like to do here?
});
Error: Command failed: convert: Non-conforming drawing primitive definition `/path/to/half-transparent-watermark-image.png''@ draw.c / DrawImage / 3124