当使用节点锐利的包来调整图像大小并上传到s3时,它会被旋转
我正在使用这个节点包:
https://www.npmjs.com/package/sharp
我用它来调整图像大小,然后将其上传到亚马逊S3。
大多数图像是找到的,但其中一些(我假设基于长宽比)旋转。
有没有办法来防止这种情况或其原因?
这是我正在使用的代码的副本。 imageData是从文件上传的s3存储桶文件中获取的数据。 正如你所看到的,我没有调用旋转函数。 反正有'锁'旋转?
module.exports.resize = function(imageData, width, fileName){
sharp(imageData).resize(parseInt(width), null).toBuffer(function (err, data) {
if (err) throw err;
s3.putObject({
Bucket: aws.bucket,
Key: 'images/' + width + '/' + fileName,
Body: data
}, function (err, data) {
if (err) {
console.log('Failed to resize image due to an error: ' + err);
return {
message: 'Failed to resize image due to an error: ' + err
};
} else {
console.log('s3 image uploaded to ' + 'images/' + width + '/' + fileName);
return {
message: 's3 image uploaded to ' + 'images/' + width + '/' + fileName
};
}
});
});
});
工作代码从url(s3 url)获取图像,调整大小,然后将大小调整后的图像上传到s3
var sharp = require('sharp')
var request = require('request').defaults({encoding: null})
var s3Upload = require('./s3Upload')
module.exports = function (fileLocation) {
request(fileLocation, function (error, response, body) {
var fileInstance400x400 = sharp(body)
var inst400x400 = fileInstance400x400.resize(400, 400)
s3Upload('filename400x400', inst400X400)
})
}
// fileLocation iabsolute url of image
s3Upload模块
var aws = require('aws-sdk')
var config = require('../config')
/**
* @param fileName: filename to be saved
* @param file: bufferd data
*/
function defaultContentType(req, file, cb) {
setImmediate(function () {
var ct = file.contentType || file.mimetype || 'application/octet-stream'
cb(null, ct);
});
}
module.exports = function (fileName, file) {
aws.config.update({
accessKeyId: config.S3_CONF.access_key_id,
secretAccessKey: config.S3_CONF.access_key,
region: config.S3_CONF.region,
contentType: defaultContentType,
limits: {fileSize: 1000000, files: config.MAX_FILE_COUNT || 6}
})
var s3bucket = new aws.S3({params: {Bucket: config.S3_CONF.media_bucket}});
var params = {Key: fileName, Body: file};
// TODO setting proper header for s3
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('Error uploading data: ', err);
} else {
console.log('Successfully uploaded data to myBucket/myKey');
}
});
}
希望能帮助到你
链接地址: http://www.djcxy.com/p/90561.html上一篇: When using node sharp package to resize image and upload to s3 it is rotated