我有一个图像,想使用Node.js在其上应用水印(另一个图像)。要求是水印不应该是一个坚实的图片(我可以用gm库的draw()做什么),但半透明。您能否建议使用任何工具?
答案 0 :(得分:5)
您可以将水印图像设置为半透明,并将其与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?
});
答案 1 :(得分:4)
通过分配子进程
从命令行使用ImageMagick// 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
});
如果您需要API抽象,可以在https://github.com/rsms/node-imagemagick
找到一个很棒的模块答案 2 :(得分:-2)
我使用代码
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?
});
错误:命令失败:转换:不符合图形原语定义`/path/to/half-transparent-watermark-image.png''@ draw.c / DrawImage / 3124