gpt4 book ai didi

node.js - 如何将照片方向固定为 Angular

转载 作者:太空狗 更新时间:2023-10-29 18:24:57 27 4
gpt4 key购买 nike

我在获取照片以在我的 Angular/Node 应用程序中以正确的方式定位时遇到问题。

我的应用程序设置为使用 ng2-file-upload 将文件从 Angular 应用程序发送到服务器,multer 和 multer-s3 将照片保存在 AWS S3 中。然后,我将文件名保存在我的 postgres 数据库中,并使用它通过 url 调用照片。

我的问题是,照片​​是由 iPhone 上传的(可能是其他我只试过 iphone 的),然后向左旋转返回。

我研究了服务器端的不同选项,但这些选项对我不起作用。这包括库 fix-orientation 和 jpeg-autorotate。

有没有人有他们在 Angular2+ 客户端实现的解决方案?

这是我在服务器端的图片上传代码

aws.config.loadFromPath(path3);
var s3 = new aws.S3();
var fileName = '';
var uploadM = multer({
storage: multerS3({
s3: s3,
bucket: 'XXX',
acl: 'public-read',
metadata: function (req, file, cb) {
console.log(file);
console.log(req.file);
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
fileName = Date.now().toString() + "-" + (Math.round(Math.random() * 10000000000000000)).toString() + '-' + file.originalname;
cb(null, fileName)
}
})
});

router.post('/upload', uploadM.array('photo', 3), function(req,res) {
if (res.error) {
return res.status(400).json({
message: "Error",
error: res.error
});
}
return res.status(200).send(fileName);

});


module.exports = router;

这是我在 Angular 应用程序上的代码

  public uploader:FileUploader = new FileUploader({url: this.devUrl, itemAlias: 'photo'});

constructor(
private userS: UserService,
private authS: MyAuthService,
private router: Router,
private itemS: ItemService,
private uis: UiService) {}

ngOnInit() {
this.uploader.onAfterAddingFile = (file)=> { file.withCredentials = false; };

this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
this.awsUrls.push('AWSURL' + response);
this.filesUploaded = true;
this.uploaderLoading = false;
};

}

addItem() {
this.uploaderLoading = true;
this.itemS.onNewItem(this.awsUrls)
.subscribe(data => {
this.uis.onFlash('Posted Successfully. Add Details!', 'success');
this.itemS.onSetUpdateItemId(data.id, false);
this.uploaderLoading = false;
this.onPhotoAdded();
}, resp => {
this.uploaderLoading = false;
this.uis.onFlash('Error Posting', 'error');
console.log(resp);
});
}

onUploadClicked() {
this.uploader.uploadAll();
this.uploaderLoading = true;
}

最佳答案

通过以 Angular 读取 Exif 数据来解决图像方向问题。

安装exif-js

npm i exif-js --save

将 EXIF 导入您的组件

import * as EXIF from 'exif-js';

setImgOrientation 方法有 file param 是上传的文件对象,inputBase64Stringbase64 同一文件的字符串

setImgOrientation(file, inputBase64String) {
return new Promise((resolve, reject) => {
const that = this;
EXIF.getData(file, function () {
if (this && this.exifdata && this.exifdata.Orientation) {
that.resetOrientation(inputBase64String, this.exifdata.Orientation, function
(resetBase64Image) {
inputBase64String = resetBase64Image;
resolve(inputBase64String);
});
} else {
resolve(inputBase64String);
}
});
});
}

resetOrientation(srcBase64, srcOrientation, callback) {
const img = new Image();

img.onload = function () {
const width = img.width,
height = img.height,
canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');

// set proper canvas dimensions before transform & export
if (4 < srcOrientation && srcOrientation < 9) {
canvas.width = height;
canvas.height = width;
} else {
canvas.width = width;
canvas.height = height;
}

// transform context before drawing image
switch (srcOrientation) {
case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;
case 3: ctx.transform(-1, 0, 0, -1, width, height); break;
case 4: ctx.transform(1, 0, 0, -1, 0, height); break;
case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
case 6: ctx.transform(0, 1, -1, 0, height, 0); break;
case 7: ctx.transform(0, -1, -1, 0, height, width); break;
case 8: ctx.transform(0, -1, 1, 0, 0, width); break;
default: break;
}

// draw image
ctx.drawImage(img, 0, 0);

// export base64
callback(canvas.toDataURL());
};

img.src = srcBase64;
}

关于node.js - 如何将照片方向固定为 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135349/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com