我正在使用此节点包:
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
};
}
});
});
});
答案 0 :(得分:4)
使用代码从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
s3上传模块
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');
}
});
}
希望有所帮助