vue、js实现图片等比压缩,等比缩放完整demo

简介: vue、js实现图片等比压缩,等比缩放完整demo
<!DOCTYPE>
<html>
    <head>
      <meta name="baidu-site-verification" content="LDyoKvyTEK" />
        <link rel="shortcut icon " type="images/x-icon" href="img/gt.ico">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>图片等比处理</title>
    <style type="text/css">
      img {
        width: 300px;
        height: auto;
      }
    </style>
    </head>
    <body class="base-transition">
        <div class="page-wrap homepage" id="vue" v-cloak>
      <p>选择的图片任意一边大于1024,以最长边等比压缩图片,否则不处理</p>
      <input id="photo" type="file" accept="image/png,image/jpeg,image/jpg" @change="handkeFileChange($event)" />
      <h4>
        <span>选择图片长:<span>{{sw}}px</span>&nbsp;&nbsp;&nbsp;&nbsp;宽:<span>{{sh}}px</span></span>
        <br />
        <span>处理后图片长:<span>{{rw}}px</span>&nbsp;&nbsp;&nbsp;&nbsp;宽:<span>{{rh}}px</span></span>
      </h4>
      <img :src="usableImage" />
        </div>
        <script type="text/javascript" src="js/vue.min.js" ></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script>
      var vue = new Vue({
        el: '#vue',
        data: {
          usableImage: '',
          sw: 0,
          sh: 0,
          rw: 0,
          rh: 0
        },
        computed: {},
        watch: {},
        mounted: function() {},
        methods: {
          handkeFileChange: function(event) {
            let that = this;
            that.gate = false;
            let file = event.target.files[0];
            let reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function(event) {
              that.imageSrc = event.target.result;
              //加载图片获取图片真实宽度和高度
              let image = new Image();
              image.onload = function() {
                let width = image.width;
                that.sw = width;
                let height = image.height;
                that.sh = height;
                if(width > 1024 && width > height) {
                  that.dealImage(that.imageSrc, {
                    width: 1024,
                    quality: 1
                  }, function(base) {
                    that.usableImage = base;
                  })
                } else if(height > 1024 && height > width) {
                  that.dealImage(that.imageSrc, {
                    height: 1024,
                    quality: 1
                  }, function(base) {
                    that.usableImage = base;
                  })
                } else {
                  that.rw = width;
                  that.rh = height;
                  that.usableImage = that.imageSrc;
                }
              }
              image.src = that.imageSrc;
            }
          },
          /**
           * 图片压缩,默认同比例压缩
           * @param {Object} path 
           *   pc端传入的路径可以为相对路径,但是在移动端上必须传入的路径是照相图片储存的绝对路径
           * @param {Object} obj
           *   obj 对象 有 width, height, quality(0-1)
           * @param {Object} callback
           *   回调函数有一个参数,base64的字符串数据
           */
          dealImage: function(path, obj, callback) {
            let ts = this;
            var img = new Image();
            img.src = path;
            img.onload = function() {
              var that = this;
              // 默认按比例压缩
              var w = that.width,
                h = that.height,
                scale = w / h;
              w = obj.width || (obj.height * scale);
              h = obj.height || (obj.width / scale);
              var quality = 0.7; // 默认图片质量为0.7
              //生成canvas
              var canvas = document.createElement('canvas');
              var ctx = canvas.getContext('2d');
              // 创建属性节点
              var anw = document.createAttribute("width");
              anw.nodeValue = w;
              ts.rw = w;
              var anh = document.createAttribute("height");
              anh.nodeValue = h;
              ts.rh = h;
              canvas.setAttributeNode(anw);
              canvas.setAttributeNode(anh);
              ctx.drawImage(that, 0, 0, w, h);
              // 图像质量
              if(obj.quality && obj.quality <= 1 && obj.quality > 0) {
                quality = obj.quality;
              }
              // quality值越小,所绘制出的图像越模糊
              var base64 = canvas.toDataURL('image/jpeg', quality);
              // 回调函数返回base64的值
              callback(base64);
            }
          }
        }
      });
    </script>
    </body>
</html>
目录
相关文章
|
3天前
|
JavaScript 数据库
vue中图片文件夹的保存与访问
vue中图片文件夹的保存与访问
|
5天前
|
JavaScript
vue实现base64格式转换为图片
vue实现base64格式转换为图片
13 2
|
19天前
|
存储 JavaScript
报错permission.js:41 [Vue warn]: Property “showClose“ must be accessed with “$data.showClose“ because
报错permission.js:41 [Vue warn]: Property “showClose“ must be accessed with “$data.showClose“ because
|
21天前
|
JavaScript
Vue与原生JS中方法调用
Vue与原生JS中方法调用
8 0
|
21天前
|
JavaScript 前端开发
Vue鼠标悬浮切换图片
Vue鼠标悬浮切换图片
13 0
|
25天前
|
JavaScript 前端开发 UED
Vue工具和生态系统: Vue.js和服务器端渲染(SSR)有关系吗?请解释。
Vue.js是一个渐进式JavaScript框架,常用于开发单页面应用,但其首屏加载较慢影响用户体验和SEO。为解决此问题,Vue.js支持服务器端渲染(SSR),在服务器预生成HTML,加快首屏速度。Vue.js的SSR可手动实现或借助如Nuxt.js的第三方库简化流程。Nuxt.js是基于Vue.js的服务器端渲染框架,整合核心库并提供额外功能,帮助构建高效的应用,改善用户体验。
21 0
|
25天前
|
JavaScript 前端开发 开发者
Vue工具和生态系统: Vue.js和TypeScript可以一起使用吗?
【4月更文挑战第18天】Vue.js与TypeScript兼容,官方文档支持在Vue项目中集成TypeScript。TypeScript作为JavaScript超集,提供静态类型检查和面向对象编程,增强代码准确性和健壮性。使用TypeScript能提前发现潜在错误,提升代码可读性,支持接口和泛型,使数据结构和函数更灵活。然而,不是所有Vue插件都兼容TypeScript,可能需额外配置。推荐尝试在Vue项目中使用TypeScript以提升项目质量。
16 0
|
25天前
|
资源调度 JavaScript 前端开发
Vue工具和生态系统: 如何使用Vue.js实现服务端渲染(SSR)?不少于500字
Vue.js框架用于构建用户界面,而服务端渲染(SSR)能提升首屏加载速度和SEO。以下是使用Vue.js实现SSR的简要步骤:1) 安装vue、vue-server-renderer和express依赖;2) 创建Vue应用如`vue create my-ssr-app`;3) 使用express创建服务器;4) 在Express应用中设定路由处理所有请求;5) 创建渲染器将Vue应用转为HTML;6) 运行服务器如`node my-ssr-app/server.js`。实际应用可能涉及动态内容、状态管理和错误处理等复杂情况。
26 1
|
1月前
|
JavaScript 搜索推荐 测试技术
深入了解 Vue CLI:现代化 Vue.js 项目开发工具
深入了解 Vue CLI:现代化 Vue.js 项目开发工具
|
3天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口