简体   繁体   中英

Write EXIF data to image stream Node.js

I found a nice npm package that allows you to read and write Exif data to images, https://github.com/Sobesednik/node-exiftool .

The challenge that I have is that it requires you to provide the path to an image. So, the image has to be written to disk if you want to modify the EXIF using this package. Is there an easy way to check/read the EXIF, and if necessary, write EXIF data to an image stream?

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

EDIT: This question was also asked on node-exiftool

i had a similar task. I had to write physical dimensions and additional metadata to PNG files. I have found some solutions and combined it into one small library. png-metadata

it could read PNG metadata from NodeJS Buffers, and create a new Buffers with new metadata.

Here is an example:

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

It could be modified to be used with streams, for example, you could implement WritableBufferStream class.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM