在Node.js中对图像应用半透明水印

mak*_*nse 1 graphics image imagemagick node.js

我有一个图像,并希望使用Node.js在其上应用水印(另一个图像).要求是水印不应该是一个坚实的图片(我可以用gm库的draw()做什么),但半透明.您能否建议使用任何工具?

srq*_*inn 5

通过分叉子进程从命令行使用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
});
Run Code Online (Sandbox Code Playgroud)

如果您需要API抽象,可以在这里找到一个很棒的模块https://github.com/rsms/node-imagemagick


小智 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?
});
Run Code Online (Sandbox Code Playgroud)