FS.Store不可靠的gm图像转换

时间:2015-02-03 11:39:16

标签: javascript meteor imagemagick

下面的代码大部分时间都可以工作,但有时会产生零字节图像。我怀疑它是图像缩放.sample(...)的流的一部分。谢谢你的帮助!

function scaleToTargetByInt (width) {
    var maxWidth = 216 * 2; // 2x thumbnail size optimized for Retina display
    var originalWidth = width;
    var scaleInt = Math.floor(maxWidth/originalWidth);
    var scaleSize = scaleInt * originalWidth;
    return scaleSize;
}

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

    transformWrite: function (fileObj, readStream, writeStream) {

        // read image dimensions and write to metadata
        gm(readStream)
        .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}});
            }
        }))
        // read metadata.width and scale acordingly
        .sample(scaleToTargetByInt(fileObj.metadata.width))
        .stream()
        .pipe(writeStream);
    }
});

1 个答案:

答案 0 :(得分:0)

删除var关键字。 并且在更新时,我们应该从某个文档传递._id,如果不是mongo现在将上传哪个文档?

尝试

fileObj.update({_id:this._id},{$set: {'metadata.width': size.width, 'metadata.height': size.height}});

fileObj.update({_id:fileObj._id},{$set: {'metadata.width': size.width, 'metadata.height': size.height}});

代码应如下所示。

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

    transformWrite: function (fileObj, readStream, writeStream) {

        // read image dimensions and write to metadata
        gm(readStream)
        .size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
            if (err) {
                // handle the error
            } else {
                fileObj.update({_id:fileObj._id},{$set: {'metadata.width': size.width, 'metadata.height': size.height}});
            }
        }))
        // read metadata.width and scale acordingly
        .sample(scaleToTargetByInt(fileObj.metadata.width))
        .stream()
        .pipe(writeStream);
    }
});