大家晚上好。
我正在使用带有imagemagick的节点gm
库进行图像处理。我的意图是以这样的方式处理图像,使其大小调整到2048高/宽(保持高宽比),并在右下角用256x256图像进行水印处理。
我实际上是在建立一个与社交媒体整合的在线画廊,这样就可以上传全尺寸的图片,并且每个相关的社交网络都有一个“最佳大小”的图像,上面有水印。
我遇到了一些麻烦,这是我现在给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像素,从而保持原始图像的尺寸。
我已经与它斗争了很长一段时间,尝试了无数的解决方案,我似乎得到了这种可怕的权衡。有人能帮忙吗?
发布于 2016-12-02 20:48:12
我以前的解决方案依赖于.size
函数来计算水印位置的维数。从那以后,我调整了这个函数,所以它现在使用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
上面的注释说明了这些值的顺序--如果使用gravity
设置位置,这些值是偏移的,可以是负值。
在本例中,水印将位于右下角。
首先,您需要在图像上.draw
水印。
其次,您需要根据所需的输出维度进行.resize
。
最后,您可以将.write
(或.stream
)发送到输出目的地。
编辑-2016年12月2日星期五23时21分
我现在已经建立了一个函数,让你调整最长的边的大小,并选择你是否想水印它-我想如果你想要这样的东西,我问候你“你好!”,未来的人!
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");
});
}
});
}
};
要使用它,只需与此并举并根据需要配置;
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
}
);
该函数可能会在某些方面进行调整,但如果您正在寻找“它只是起作用”的解决方案,这就是它。
通过一些调整,你可以让它沿着最短的边调整,如果你愿意的话,但这不是我的项目所需要的东西,所以我不会在这里讨论它。
https://stackoverflow.com/questions/40939662
复制相似问题