我正在尝试使用transformWrite设置FS.store,它可以扩展上传的图像。我有一个最大宽度大小。但缩放应该只是整数因子。
例如,如果我的最大宽度为100px,原始图像宽度为40px,则图像应按整数因子2缩放,这会导致目标宽度为80px(因此会导致3倍的因子)图像太宽了。)
要计算原始宽度并进行数学计算,我设置了应返回最佳宽度的函数scaleToMaxByInteger
。由于某种原因,它不适用于gm转换。为什么呢?
var ScaledStore = new FS.Store.FileSystem("ScaledPix", {
path: pathToScaledFolder,
beforeWrite: function(fileObj, readStream) {
return {
extension: 'png', // sets extension of file
type: 'image/png' // sets content of type field in db
};
},
transformWrite: function(fileObj, readStream, writeStream) {
// calculate max possible size if scaled by integer
// that will fit into maximal width available
function scaleToMaxByInteger(readStream) {
gm(readStream)
.size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
if (err) {
// handle the error
} else {
var maxWidth = 100;
var originalWidth = size.width;
var scaleInt = Math.floor(maxWidth/originalWidth);
var scaleSize = scaleInt * originalWidth;
console.log('originalWidth: ' + originalWidth);
console.log('scaleInt: ' + scaleInt);
console.log('scaleSize: ' + scaleSize);
return scaleSize;
}
}));
};
// Resizing image with no antialiasing
gm(readStream)
.sample(scaleToMaxByInteger(readStream)) // >>>>>> This is NOT WORKING
.stream('PNG')
.pipe(writeStream);
}
});