我发现了一个很好的npm包,允许你读取和写入图像的https://github.com/Sobesednik/node-exiftool。
我面临的挑战是它需要您提供图像的路径。因此,如果要使用此软件包修改EXIF,则必须将映像写入磁盘。有没有简单的方法来检查/读取EXIF,如有必要,可以将EXIF数据写入图像流?
var imageURL = 'https://nodejs.org/static/images/logos/nodejs-new-pantone-black.png'
var upstreamServer = 'http://someupstreamserver/uploads'
request
.get(imageURL)
.pipe(
// TODO read EXIF
// TODO write missing EXIF
request
.post(upstreamServer, function(err, httpResponse, body){
res.send(201)
})
)
编辑:这个问题也在node-exiftool上提出
答案 0 :(得分:1)
它可以从NodeJS缓冲区读取PNG元数据,并使用新的元数据创建一个新的缓冲区。
这里是一个示例:
const buffer = fs.readFileSync('1000ppcm.png')
console.log(readMetadata(buffer));
withMetadata(buffer,{
clear: true, //remove old metadata
pHYs: { //300 dpi
x: 30000,
y: 30000,
units: RESOLUTION_UNITS.INCHES
},
tEXt: {
Title: "Short (one line) title or caption for image",
Author: "Name of image's creator",
Description: "Description of image (possibly long)",
Copyright: "Copyright notice",
Software: "Software used to create the image",
Disclaimer: "Legal disclaimer",
Warning: "Warning of nature of content",
Source: "Device used to create the image",
Comment: "Miscellaneous comment"
}
});
可以对其进行修改以用于流,例如,您可以实现WritableBufferStream类。
const { Writable } = require('stream');
/**
* Simple writable buffer stream
* @docs: https://nodejs.org/api/stream.html#stream_writable_streams
*/
class WritableBufferStream extends Writable {
constructor(options) {
super(options);
this._chunks = [];
}
_write(chunk, enc, callback) {
this._chunks.push(chunk);
return callback(null);
}
_destroy(err, callback) {
this._chunks = null;
return callback(null);
}
toBuffer() {
return Buffer.concat(this._chunks);
}
}