vue.js3:图片镜像(翻转)并保存(vue@3.2.37)

一,js代码:

<template>
  <div style="background: #ffffff;" id="root" >
    <div><button @click="restore">还原</button><button @click="flipx">水平镜像</button>
      <button @click="flipy">垂直镜像</button>
      <button @click="flipxy">水平垂直镜像</button>
    </div>
    <div><input type="file" accept="image/*" @change="open" /><button @click="save">保存</button></div>

    <div style="position: relative;width:500px;height:500px;overflow: hidden;background: lightgray;" >
      <img id="img" :src="imgSrc" style="max-width:500px;max-height: 500px;" />
    </div>
  </div>
</template>

<script>
import {ref} from "vue";
export default {
  name: "FlipImg",
  setup() {
    //翻转的类型:0:还原,1,水平,2,垂直,3,水平垂直
    const flipType = ref(0);
    //图片的src
    const imgSrc = ref("");
    //图片的原宽高
    const imgWidth = ref(0);
    const imgHeight = ref(0);
    //读取图片的信息
    const open = (e) => {
      let file = e.target.files[0];
      let reader = new FileReader();
      reader.readAsDataURL(file);

      reader.onload = () =>{
        //显示图片
        imgSrc.value = reader.result;
        //得到宽高
        let img = new Image();
        img.src= reader.result;
        img.onload = () => {
          //保存宽高
          imgWidth.value = img.width;
          imgHeight.value = img.height;
        }
      }
    }
    //还原
    const restore = () => {
      flipType.value = 0;
      let box = document.getElementById('img');
      box.style.transform = 'rotateY(0deg)';
      box.style.transform = 'rotateX(0deg)';
      box.style.transitionDuration = '500ms';
      box.style.transitionTimingFunction = 'linear';
    }
    //水平镜像
    const flipx = () => {
      flipType.value = 1;
      let box = document.getElementById('img');
      box.style.transform = 'rotateY(180deg)';
      box.style.transitionDuration = '500ms';
      box.style.transitionTimingFunction = 'linear';
    }
    //垂直镜像
    const flipy = () => {
      flipType.value = 2;
      let box = document.getElementById('img');
      box.style.transform = 'rotateX(180deg)';
      box.style.transitionDuration = '500ms';
      box.style.transitionTimingFunction = 'linear';
    }
    //水平垂直镜像
    const flipxy = () => {
      flipType.value = 3;
      let box = document.getElementById('img');
      box.style.transform = 'rotateX(180deg) rotateY(180deg)';
      box.style.transitionDuration = '500ms';
      box.style.transitionTimingFunction = 'linear';
    }
    
    //保存图片
    const save = () => {
      if (flipType.value == 0) {
          alert('请执行操作后再下载');
          return;
      }

      let img = document.getElementById('img')
      //创建canvas
      const canvas = document.createElement('canvas')

      canvas.width = imgWidth.value // 根据图像大小来动态生成canvas
      canvas.height = imgHeight.value;
      if (canvas.getContext) {
        let context = canvas.getContext('2d');
        if (flipType.value == 1){
          context.translate(imgWidth.value / 2, imgHeight.value / 2)
          context.scale(-1,1) //水平翻转
          context.drawImage(img, -imgWidth.value / 2, -imgHeight.value / 2)
        } else if (flipType.value == 2) {
          context.translate(imgWidth.value / 2, imgHeight.value / 2)
          context.scale(1,-1) //垂直翻转
          context.drawImage(img, -imgWidth.value / 2, -imgHeight.value / 2)
        } else if (flipType.value == 3) {
          context.translate(imgWidth.value / 2, imgHeight.value / 2)
          context.scale(-1,-1) //水平垂直翻转
          context.drawImage(img, -imgWidth.value / 2, -imgHeight.value / 2)
        }
        downJpgByCanvas(canvas);
      }
    }

    //下载图片
    const downJpgByCanvas = (canvas) => {
      var oA = document.createElement("a");
      let time = timeFormat();
      oA.download = "img_"+time+'.jpg';// 设置下载的文件名,默认是'下载'
      oA.href = canvas.toDataURL("image/jpeg");
      document.body.appendChild(oA);
      oA.click();
      oA.remove(); // 下载之后把创建的元素删除
    }

    //补0
    const add0 = (m) => {
      return m<10?'0'+m:m
    }
    //格式化时间
    const timeFormat = ()=>{
      var time = new Date();
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      let res = y+add0(m)+add0(d)+add0(h)+add0(mm)+add0(s);
      return res;
    }

    return {
      restore,
      flipx,
      flipy,
      flipxy,
      save,
      open,
      imgSrc,
    }
  }
}
</script>

<style scoped>

</style>

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/02/vue-js3-tu-pian-jing-xiang-fan-zhuan-bing-bao-cun-vue-3-2-37/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,测试效果

还原时:

 

 水平:

 

 垂直:

三,查看vue框架的版本:

liuhongdi@lhdpc:/data/vue/pdf/image2pdf$ npm list vue
image2pdf@0.1.0 /data/vue/pdf/image2pdf
├─┬ @vue/cli-plugin-babel@5.0.8
│ └─┬ @vue/babel-preset-app@5.0.8
│   ├─┬ @vue/babel-preset-jsx@1.3.0
│   │ └── vue@3.2.37 deduped invalid: "2.x" from node_modules/@vue/babel-preset-jsx
│   └── vue@3.2.37 deduped
└─┬ vue@3.2.37
  └─┬ @vue/server-renderer@3.2.37
    └── vue@3.2.37 deduped

 

posted @ 2022-09-07 17:40  刘宏缔的架构森林  阅读(1137)  评论(0)    收藏  举报