我正在使用 react-firebase-file-uploader 将头像上传到Firebase存储。但是,每当我上传人像方向图像(特别是在 Android和IOS 设备上拍摄的图像-它们的元数据中往往具有 OrientationRotate 90 CW )时,该图像就是旋转90度
我以前已经阅读过有关此内容,并且我相信拍摄的这些智能手机图像始终位于 Landscape (横向)中,但方向存储在 EXIF meta 中。如果我弄错了,请纠正我?
以下是使用react- firebase-file-uploader 上传图像的组件的示例-我知道此程序包没有问题,并且该问题的解决方案可能适用跨许多应用程序。
那么,我需要做些什么来读取 EXIF方向,更改旋转角度(如果需要,还是我需要在上传文件时传递元数据?),仍然继续上传吗?
class ProfilePage extends Component {
state = {
avatar: "",
isUploading: false,
progress: 0,
avatarURL: ""
};
handleProgress = progress => this.setState({ progress });
handleUploadError = error => {
this.setState({ isUploading: false });
console.error(error);
};
handleUploadSuccess = filename => {
this.setState({ avatar: filename, progress: 100, isUploading: false });
firebase
.storage()
.ref("images")
.child(filename)
.getDownloadURL()
.then(url => this.setState({ avatarURL: url }));
};
render() {
return (
<div>
<form>
{this.state.isUploading && <p>Progress: {this.state.progress}</p>}
{this.state.avatarURL && <img src={this.state.avatarURL} />}
<FileUploader
accept="image/*"
name="avatar"
randomizeFilename
storageRef={firebase.storage().ref("images")}
onUploadStart={this.handleUploadStart}
onUploadError={this.handleUploadError}
onUploadSuccess={this.handleUploadSuccess}
onProgress={this.handleProgress}
/>
</form>
</div>
);
}
}
export default ProfilePage;
答案 0 :(得分:3)
您应该看看现代的JavaScript库 JavaScript-Load-Image ,该库已经对EXIF方向提供了完整的解决方案,其中包括 auto-fix 。 >
您可以使用图像缩放(.scale()
方法)将图像转换为画布并固定图像方向。
看看Fix image orientation with Javascript。
这是另一个有趣的库:react-exif-orientation-img
答案 1 :(得分:0)
在这里根据您的需要在注释中提及了@German Plebani解决方案的改编:
import React, {Component} from "react";
import FileUploader from "react-firebase-file-uploader";
import exif from 'exif-js';
function readFile(file) {
return new Promise(resolve => {
var reader = new FileReader();
reader.onload = e => resolve(e.target.result);
reader.readAsDataURL(file);
});
};
function createImage(data) {
return new Promise(resolve => {
const img = document.createElement('img');
img.onload = () => resolve(img);
img.src = data;
})
}
function rotate(type, img) {
return new Promise(resolve => {
const canvas = document.createElement('canvas');
exif.getData(img, function () {
var orientation = exif.getAllTags(this).Orientation;
if ([5, 6, 7, 8].indexOf(orientation) > -1) {
canvas.width = img.height;
canvas.height = img.width;
} else {
canvas.width = img.width;
canvas.height = img.height;
}
var ctx = canvas.getContext("2d");
switch (orientation) {
case 2:
ctx.transform(-1, 0, 0, 1, img.width, 0);
break;
case 3:
ctx.transform(-1, 0, 0, -1, img.width, img.height);
break;
case 4:
ctx.transform(1, 0, 0, -1, 0, img.height);
break;
case 5:
ctx.transform(0, 1, 1, 0, 0, 0);
break;
case 6:
ctx.transform(0, 1, -1, 0, img.height, 0);
break;
case 7:
ctx.transform(0, -1, -1, 0, img.height, img.width);
break;
case 8:
ctx.transform(0, -1, 1, 0, 0, img.width);
break;
default:
ctx.transform(1, 0, 0, 1, 0, 0);
}
ctx.drawImage(img, 0, 0, img.width, img.height);
ctx.toBlob(resolve, type);
});
})
}
class OrientationAwareFirebaseImageUploader extends Component {
handleChange = (event) => {
event.target.files.forEach(
file => readFile(file)
.then(createImage)
.then(rotate.bind(undefined, file.type))
.then(blob => {
blob.name = file.name;
this.uploader.startUpload(blob);
})
);
}
render() {
return (
<FileUploader
{...this.props}
ref={ref => this.uploader = ref}
onChange={this.handleChange}
accept="image/*"
/>
);
}
}
export default OrientationAwareFirebaseImageUploader;