节点gm水印和resize
晚上好。
我正在与imagemagick节点gm
库进行image processing。 我的意图是处理一个图像,调整到2048的高度/宽度(保持高宽比),并在右下angular用256×256的图像进行水印处理。
我本质上是build立一个社交媒体整合的在线画廊,上传全尺寸的图片,每个相关的社交networking上都有一个“最佳尺寸”的图片。
我有一些麻烦,这是我目前的代码大小到Facebook;
gm(origLoc) .resize(2048, null) .command('composite') .gravity('SouthEast') .out('-geometry', '+20+10') .in(waterMarkFile_Location) .write(thisFile.destination + "rsz/FB/"+newFileName, function(err, stdout, stderr, command){ if (err){ console.log("Err in FB"); console.log(err); } });
它输出一个2048像素宽的图像,保持比例,但问题是水印被缩放,覆盖整个图像。
如果我删除或注释resize
行,它会像您所期望的水印,但它不会将整个图像调整到2048像素,保持原始图像的尺寸。
我一直在为它奋斗了一段时间,尝试了很多的解决scheme,而且我似乎正在得到这个可怕的折衷。 谁能帮忙?
我以前的解决scheme依靠.size
函数来计算水印位置的尺寸。 我已经适应了这个function,所以它现在通过gravity
定位在右下方。
gm(_image) // WATERMARK - PARAM ORDER: [X Pos, Y Pos, width, height] .draw(['gravity SouthEast image Over 0,0 256,256 "/path/to/watermark.png"']) // RESIZE DIMENSIONS - PARAM ORDER: [width, height] .resize(2048, null) .write("/path/to/resized/output.jpg", function(err, stdout, stderr, command){ if (err){ return cb(err); } return cb("done"); });
256是我的水印所需的宽度和高度。 .draw
上面的.draw
说明了这些值所在的顺序 – 如果使用gravity
来设置位置,这些值是偏移量,可以是负值。
在这个例子中,水印将在右下angular。
首先,你需要在图像上.draw
水印。
其次,你需要根据你想要的输出尺寸来设置.resize
。
最后,你写。(或.stream
)到你的输出目的地。
编辑 – 2016年12月2日星期五23:21
我现在已经build立了一个function,可以让你调整最长的边缘,并select是否要水印 – 我想如果你正在寻找这样的东西,我问候你“你好!”,未来的人!
function processImgLongEdge(_image, _outputDest, _maxLongEdge, watermark, cb){ var gm = require("gm").subClass({imageMagick: true}); if (watermark == true){ gm(_image).size(function(err, value){ var isLandscape; if (value.width > value.height){ isLandscape = true; } else { isLandscape = false; } if (isLandscape == true){ gm(_image) .draw(['gravity SouthEast image Over 0,0 256,256 "/full/path/to/watermark.png"']) .resize(_maxLongEdge, null) .write(_outputDest, function(err, stdout, stderr, command){ if (err){ return cb(err); } return cb("done"); }); } else { gm(_image) .draw(['gravity SouthEast image Over 0,0 256,256 "/full/path/to/watermark.png"']) .resize(null, _maxLongEdge) .write(_outputDest, function(err, stdout, stderr, command){ if (err){ return cb(err); } return cb("done"); }); } }); } else { gm(_image).size(function(err, value){ var isLandscape; if (value.width > value.height){ isLandscape = true; } else { isLandscape = false; } if (isLandscape == true){ gm(_image) .resize(_maxLongEdge, null) .write(_outputDest, function(err, stdout, stderr, command){ if (err){ return cb(err); } return cb("done"); }); } else { gm(_image) .resize(null, _maxLongEdge) .write(_outputDest, function(err, stdout, stderr, command){ if (err){ return cb(err); } return cb("done"); }); } }); } };
要使用它,只需要去这个并根据需要进行configuration;
processImgLongEdge( "/path/to/input/image.jpg", // Path to original image "/path/to/output/image.jpg", // Path to output image 600, // Max length of longest edge false, // Should it have a watermark? <true | false> function(imgResult){ console.log(imgResult); // Will log "done" or error from gm to the console } );
该function可能在某些方面可以进行调整,但如果您正在寻找一个“它只是有效”的解决scheme,就是这样。
随着一些调整,你可以使它沿着最短边缘调整,如果你喜欢,但这不是我需要的项目,所以我不会在这里覆盖。