解决360兼容模式图片翻转问题

vue代码demo ,自行查看,注意得点是 img 的 css 样式需要重置orientation得值,image-orientation: none; 因为支持自动翻转得浏览器会出现翻转两次得情况

<template>
  <div class="content">
    <!-- <img src="../assets/952621f3-731d-48ce-ab2e-b95fe6a468ef.jpg" /> -->
    <input type="file" accept="image/*" @change="change" />
    <p>转换前</p>
    <img class="preivew" style="height: 100px;" :src="img1" />
    <p>转换后</p>
    <img
      class="preivew2"
      :style="{ transform: 'rotate(' + route + ')' }"
      :src="img1"
    />
  </div>
</template>

<script>
import Exif from "exif-js";
const rotateImg = (file, fileUrl) => {
  return new Promise((resolve, reject) => {
    let img = new Image();
    img.src = fileUrl;
    img.onload = function() {
      Exif.getData(file, function() {
        const orientation = Exif.getTag(this, "Orientation");
        if (!orientation || orientation == 1) {
          return resolve(0);
        }
        let route = 0;
        //根据orientation设置canvas的大小和旋转参数
        switch (orientation) {
          case 3: // 旋转180°
            route = '180deg';
            break;
          case 6: // 旋转90°
            route = '90deg';
            break;
          case 8: // 旋转-90°
            route = '-90deg';
            break;
          default:
        }
        resolve(route);
      });
    };
  });
};

export default {
  data() {
    return {
      img1: "",
      img2: "",
      route: "0deg",
    };
  },
  methods: {
    change(e) {
      const file = e.target.files[0];
      this.img1 = URL.createObjectURL(file);
      let that = this;
      rotateImg(file, this.img1).then((res) => {
        that.route = res;
      });
    },
  },
};
</script>
<style scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: auto;
}
img {
  width: 400px;
  height: auto;
  image-orientation: none;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值