输入类型的图像旋转="file"
Image rotation on input type="file"
我正在使用 react-firebase-file-uploader 将头像上传到 firebase 存储。但是,任何时候我上传纵向方向的图像(具体来说,在 Android 和 IOS 设备上拍摄的图像 - 它们往往具有 OrientationRotate 90 CW 在他们的元数据中)图像旋转了 90 度。
我之前读过这方面的信息,我相信这些拍摄的智能手机图像总是横向,但存储的方向是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;
这里根据您的需要改编@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;
您应该看看现代 javascript 库 JavaScript-Load-Image,它已经完整解决了 EXIF 方向,包括 自动修复.
您可以使用图像缩放(.scale()
方法)将图像转换为 canvas 并修复图像方向。
看看Fix image orientation with Javascript。
这是另一个有趣的库:react-exif-orientation-img
我正在使用 react-firebase-file-uploader 将头像上传到 firebase 存储。但是,任何时候我上传纵向方向的图像(具体来说,在 Android 和 IOS 设备上拍摄的图像 - 它们往往具有 OrientationRotate 90 CW 在他们的元数据中)图像旋转了 90 度。
我之前读过这方面的信息,我相信这些拍摄的智能手机图像总是横向,但存储的方向是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;
这里根据您的需要改编@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;
您应该看看现代 javascript 库 JavaScript-Load-Image,它已经完整解决了 EXIF 方向,包括 自动修复.
您可以使用图像缩放(.scale()
方法)将图像转换为 canvas 并修复图像方向。
看看Fix image orientation with Javascript。
这是另一个有趣的库:react-exif-orientation-img