var buf = require('fs').readFileSync('test.jpg');
gm().in('-page', '+0+0').in(buf,'test.jpg').write('output.jpg', function (err) {
if (err) console.log(err);
})
在这种情况下,我想把缓冲区数据作为输入传递给gm.in()
方法。
下面是我参考的链接,但其中使用了图像路径作为输入。 我想使用缓冲区数据作为输入。我该怎么做?
var buf = require('fs').readFileSync('test.jpg');
gm().in('-page', '+0+0').in(buf,'test.jpg').write('output.jpg', function (err) {
if (err) console.log(err);
})
在这种情况下,我想把缓冲区数据作为输入传递给gm.in()
方法。
下面是我参考的链接,但其中使用了图像路径作为输入。 我想使用缓冲区数据作为输入。我该怎么做?
.in()
方法传递的参数将被转换为命令行参数。GraphicsMagick程序仅接受文件名作为该参数,并不会尝试处理任何直接形式的数据。实际上,我正在创建一张海报,其中包含两张不同的图片,一张是模板图片“背景”,另一张是带有一些文本的顶部图片。我尝试使用gm
,但是图像质量变差了。有人指导我使用缓冲数据作为输入来提高图像质量。我尝试过了,但不知道如何将缓冲数据作为输入传递。因此,我最终决定使用节点子进程和命令字符串。以下是我与您分享的示例代码。
var fs = require('fs');
var gm = require("gm");
var exec = require('child_process').exec;
var IMAGEFILEPATH = "/images";
var gmcreateImage = function() {
var imageConfig = {"topimage":{"density":"300x300","startx":925,"starty":650,"width":575,"height":825},
"offers": [
{"startx": 75, "starty": 850, "msg": "SAVE 5$", "textcolor": "#4f61ac", "font": "Arial Bold", "fontsize":34,"stroke":{"color":"#4f61ac","width":4}},
{"startx": 75, "starty": 970, "msg": "per gallon", "textcolor": "#4f61ac", "font": "Arial Bold", "fontsize":34,"stroke":{"color":"#4f61ac","width":4}},
{"startx": 75, "starty": 1150, "msg": "With the purchase of", "textcolor": "black", "font": "Arial", "fontsize":18,"stroke":{"color":"black","width":1}},
{"startx": 75, "starty": 1260, "msg": "any Pepsi Z0 S2", "textcolor": "black", "font": "Arial", "fontsize":16,"stroke":{"color":"black","width":1}},
{"startx": 75, "starty": 1370, "msg": "on all flavours", "textcolor": "black", "font": "Arial", "fontsize":16,"stroke":{"color":"black","width":1}},
{"startx": 75, "starty": 1480, "msg": "Ask for details.", "textcolor": "black", "font": "Arial", "fontsize":18,"stroke":{"color":"black","width":1}}
]};
var addLast=imageConfig.topimage.last;
var commandStr = "gm convert '-page' '+0+0' '-units' 'PixelsPerInch' '-density' '" + imageConfig.topimage.density + "' '" + IMAGEFILEPATH+ "/template.jpg' ";
var imageActualPosition={};
imageActualPosition["x"] = imageConfig.topimage.startx;
imageActualPosition["y"] = imageConfig.topimage.starty;
if (!addLast) {
commandStr += " '-page' '+" + imageActualPosition["x"] + "+" + imageActualPosition["y"] + "' '" + IMAGEFILEPATH + "/top.jpg' ";
}
var offers = imageConfig.offers;
for (var i in offers) {
var color = offers[i].textcolor;
var startX = offers[i].startx;
var startY = offers[i].starty;
var font = offers[i].font;
var fontSize = offers[i].fontsize;
var msg = offers[i].msg;
var offerStr = "";
if (offers[i].stroke) {
offerStr += " '-stroke' '" + offers[i].stroke.color + "' '-strokewidth' '" + offers[i].stroke.width + "'";
}
offerStr += " '-fill' '" + color + "' '-pointsize' '" + fontSize + "' '-draw' 'text " + startX + " " + startY + " \"" + msg + "\"'";
commandStr += offerStr;
}
if (addLast) {
commandStr += " '-page' '+" + imageActualPosition["x"] + "+" + imageActualPosition["y"] + "' '" + IMAGEFILEPATH + "/top.jpg' ";
}
var finalImage="done.jpg";
commandStr += " '-mosaic' '-quality' '100' '" + IMAGEFILEPATH + finalImage + "'";
exec(commandStr, function(err, stdout, stderr) {
if (err) {
console.log("Error while executing gm commands" + err);
return;
} else {
console.log("Done See your image");
}
})
};
gmcreateImage();
添加uuid模块:
npm install uuid
const magickbuffers = {}
const uuid = require('uuid')
const http = require('http')
const magickserver = new http.Server()
const magickport = 9555
magickserver.on('request', (req, res) => {
res.writeHead(200, { 'Content-Type': 'image/png' })
if (magickbuffers[req.url]) {
res.end(magickbuffers[req.url])
}
else {
res.end("\n")
}
})
magickserver.listen(magickport, () => {})
Object.prototype.openBuffer = function() {
this.id = uuid()
magickbuffers[`/${this.id}`] = this
return `http://localhost:${magickport}/${this.id}`
}
Object.prototype.closeBuffer = function() {
delete magickbuffers[`/${this.id}`]
return true
}
旧的例子是想要的,但不能正常工作:
gm()
.in('-page', '+0+0')
.in(bufferone) // gm cant use a buffer here, this won't work
.in('-page', '+50+20')
.in(buffertwo) // or here
.mosaic()
.stream('png', function (err, stdout, stderr) {
stdout.pipe(writestream)
})
let one = bufferone.openBuffer()
let two = buffertwo.openBuffer()
gm()
.in('-page', '+0+0')
.in(one) // gm recognizes this because 'one' is a url pointing to the buffer
.in('-page', '+50+20')
.in(two)
.mosaic()
.stream('png', function (err, stdout, stderr) {
stdout.pipe(writestream)
bufferone.closeBuffer() // do this once gm has completely finished using these buffers
buffertwo.closeBuffer() // don't forget to do this for each buffer manipulated with .openBuffer()
})
我还没有想出如何同时使用图像
和水印
作为缓冲区,但我已经想出了如何将图像保留为缓冲区:
gm(imageBuffer)
.composite('./logo_path.png')
.geometry(geometry)
.gravity('SouthEast')
.dissolve(this.options.opacity)
.toBuffer(function (err, buffer) {
next(err, buffer, 'image/jpeg');
});
};
查看这个优秀库中的代码以获取更多信息。
gm
上也有一个名为page
的函数--所以不必使用in("-page")
,直接将缓冲区传递给page
函数即可。我认为这样应该可以。 - treecoder