gpt4 book ai didi

node.js - Node gm 水印和调整大小

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:52 24 4
gpt4 key购买 nike

大家晚上好。

我正在使用带有 imagemagick 的 Node 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 像素,从而保持原始图像尺寸。

我已经与它斗争了很长一段时间,尝试了多种解决方案,但我似乎得到了这种可怕的权衡。有人可以帮忙吗?

最佳答案

我之前的解决方案依赖于.size函数来计算水印位置的尺寸。我已经调整了该函数,现在它通过使用重力定位在右下角;

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

我现在已经构建了一个功能,可以让您调整最长边缘的大小,并选择是否要为其添加水印 - 我想如果您正在寻找这样的东西,我会向您打招呼“您好!” future !

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
}
);

该功能可能可以通过某些方式进行调整,但如果您正在寻找“它只是有效”的解决方案,这就是它。

通过一些调整,如果您愿意,您可以使其沿最短边缘调整大小,但这不是我的项目所需要的,因此我不会在这里介绍它。

关于node.js - Node gm 水印和调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40939662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com