当前位置: 动力学知识库 > 问答 > 编程问答 >

meteor - Get image dimensions (or metadata) at upload

问题描述:

I'm trying to get the original dimensions for an image while uploading it to a database. Actually it would be great to get all of it's original metadata (XMP, Adobe). But even getting the dimensions is not working:

Template.pixUpload.events({

'change .myPixInput': function(event, template) {

FS.Utility.eachFile(event, function(file) {

// get the image's width

var img = event.target.files[0]

var imgwidth = img.width;

console.log('width: ' + width);

var newFile = new FS.File(file);

newFile.metadata = {width: imgwidth};

MyPix.insert(newFile, function (err, fileObj) {

//If !err, we have inserted new doc with ID fileObj._id, and

//kicked off the data upload using HTTP

});

});

}

});

网友答案:

I use Imagemagick to get all kinds of metadata (like EXIF) from my images.

var assetStore = new FS.Store.GridFS("assetFiles", {
    transformWrite: function(fileObj, readStream, writeStream) {
        readStream.pipe(writeStream);

        // write the image data to the fileobj

        getBinaryData(readStream, FS.Utility.safeCallback(function(err, binary) {
            var imageData = Imagemagick.identify({
                data: binary
            });
            fileObj.update({
                $push: {
                    data: imageData
                }
            });
        }));
    }
});

getBinaryData is a async function that returns the binary data of my image. I use a package called classcraft:imagemagick since the graphicsmagick package does not give you as much metadata as imagemagick

网友答案:

This works! – copy/modified from a discussion with Sanjo at GitHub. Only problem is I don't fully understand what's happening. Can anyone help me out?

var OriginalsStore = new FS.Store.FileSystem("OriginalPix", {
  path: pathToOriginalsFolder,

  transformWrite: function (fileObj, readStream, writeStream) {

    // write original image to writeStream, no transformations
    readStream.pipe(writeStream);

    gm(readStream, fileObj.name())
    .size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
      if (err) {
        // handle the error
      } else {
        fileObj.update({$set: {'metadata.width': size.width, 'metadata.height': size.height}});
      }
    }));
  }
});
网友答案:

This is what are you looking for?

A normal image don't come with the 'width; field, it comes with type,name,dateMod,dateUp,and size.(well atleast my files)

 Template.pixUpload.events({
  'change .myPixInput': function(event, template) {
    FS.Utility.eachFile(event, function(file) {

      // get the image's width
      var img = event.target.files[0]
      var imgwidth = img.width;
      console.log('width: ' + width);
      console.log(img.lastModified);
      console.log(img.lastModifiedDate);
      console.log(img.name);
      console.log(img.size);
      console.log(img.type);
      var newFile = new FS.File(file);
         newFile.metadata = {
                width: imgwidth
                name:img.name,
                size:img.size,
                type:img.type,
                lstModDate:img.lastModifiedDate
                lstDate:img.lastModified
               };
      MyPix.insert(newFile, function (err, fileObj) {
        //If !err, we have inserted new doc with ID fileObj._id, and
        //kicked off the data upload using HTTP
      });
    });
  }
});

Test it

分享给朋友:
您可能感兴趣的文章:
随机阅读: