上传时获取图片尺寸(或元数据)

时间:2015-01-23 08:21:15

标签: meteor

我正在尝试在将图像上传到数据库时获取图像的原始尺寸。实际上,获取所有原始元数据(XMP,Adobe)会很棒。但即使获得尺寸也无效:

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

3 个答案:

答案 0 :(得分:1)

我使用Imagemagick从我的图像中获取各种元数据(如EXIF)。

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是一个异步函数,它返回图像的二进制数据。 我使用名为 classcraft:imagemagick 的软件包,因为 graphicsmagick 软件包没有像imagemagick那样提供足够的元数据

答案 1 :(得分:1)

这个有效! - 从discussion with Sanjo at GitHub复制/修改。唯一的问题是我不能完全理解发生了什么。任何人都可以帮助我吗?

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

答案 2 :(得分:0)

这就是你在寻找什么?

普通图像没有宽度;字段,它附带type,name,dateMod,dateUp,and size。(至少我的文件)

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

测试