输入类型="文件"时的图像旋转

Dar*_*ren 10 javascript exif orientation reactjs

我正在使用react-firebase-file-uploader将头像上传到firebase存储.但是,每当我上传Portrait Orientation图像(特别是在Android和IOS设备上拍摄的图像- 它们的元数据中往往会有OrientationRotate 90 CW)时,图像会旋转90度.

我以前读过这个,我相信这些拍摄的智能手机图像总是在横向,但方向是存储EXIF元.如果我弄错了,请纠正我?

下面是使用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;
Run Code Online (Sandbox Code Playgroud)

Zak*_*rki 5

您应该看一下现代的JavaScript库JavaScript-Load-Image,它已经对EXIF方向提供了完整的解决方案,其中包括auto-fix

您可以使用图像缩放(.scale()方法)将图像转换为画布并固定图像方向。

看一下用Javascript修复图像方向

这是另一个有趣的库:react-exif-orientation-img