I am using react-firebase-file-uploader to upload an avatar to firebase storage. However, any time I upload an image of Portrait Orientation (specifically, images taken on Android and IOS devices - they tend to have OrientationRotate 90 CW in their metadata) the image is rotated 90 degrees.
I have read about this previously and I believe that these smartphone images taken are always in Landscape but the orientation is stored EXIF meta. Please correct me if I am mistaken?
Below is an example of a component that is uploading the image using react-firebase-file-uploader - I know it is not an issue with this package and the solution to this question is likely applicable across many applications.
So, what do I need to do as to read the EXIF orientation, change the rotation (if required, or is it that I need to pass the meta with the file upload?) and still continue the upload?
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;
You should take a look at the modern javascript library JavaScript-Load-Image that has already a complete solution to the EXIF orientation included the auto-fix.
You could use the image scaling (.scale()
method) to convert the image to the canvas and fix your image orientation.
Take a look to Fix image orientation with Javascript.
Here is another interesting lib : react-exif-orientation-img
Here the adaptation of the solution @German Plebani menitioned in comments for your needs:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With