zoukankan      html  css  js  c++  java
  • 图片等比例缩放方法

    export modifyImageSize = (path, maxWidth, maxHeight, callback) => {
      /*
       * @param path 【图片路径】
       * @param maxWidth 【允许缩放的最大宽度】
       * @param maxHeight 【 允许缩放的最大高度】
       * @parsm callback 【回调缩放高度宽度】
       */
      /*
        @使用方法
        
        modifyImageSize(path,500,500,(res)=>{
          let { tempHeight, tempWidth } = res
          console.log('缩放之后高度:'+tempHeight)
          console.log('缩放之后宽度:'+tempWidth)
        })
      */
      let image = new Image();
      image.src = path;
      var tempWidth;
      var tempHeight;
      image.onload = function() {
        if (image.width > 0 && image.height > 0) {
          if (image.width / image.height >= maxWidth / maxHeight) {
            if (image.width > maxWidth) {
              tempWidth = maxWidth;
              tempHeight = (image.height * maxWidth) / image.width;
            } else {
              tempWidth = image.width;
              tempHeight = image.height;
            }
          } else {
            if (image.height > maxHeight) {
              tempHeight = maxHeight;
              tempWidth = (image.width * maxHeight) / image.height;
            } else {
              tempWidth = image.width;
              tempHeight = image.height;
            }
          }
          callback({
            tempWidth, // 缩放之后宽度
            tempHeight // 缩放之后高度
          })
        }
      }
    }
  • 相关阅读:
    2018年年终总结
    oracle存储过程 关于update的动态SQL-工作心得
    编译+远程调试spark
    记录一次 hadoop yarn resourceManager无故切换的故障
    hadoop2.8.4 版本yarn RM fairScheduler调度性能优化的若干次尝试
    记录一次hadoop2.8.4版本RM接入zk ha问题
    Ranger-hdfs插件压测
    ranger-hdfs 插件组权限测试
    Nginx基本参数调优
    Nginx安装配置
  • 原文地址:https://www.cnblogs.com/wangsai-666/p/13927356.html
Copyright © 2011-2022 走看看