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 // 缩放之后高度
          })
        }
      }
    }
  • 相关阅读:
    python流行的原因
    shell rename directory
    shell if [ -d filename]
    eclipse文本编码格式修改为UTF-8
    egrep 第几列开始
    Java double 精度
    BigDecimal 两种方式
    使用SecureCRT连接ubuntu
    eclipse快速查找一个变量、方法或者类被引用的地方
    我的互联网金融行业经验总结
  • 原文地址:https://www.cnblogs.com/wangsai-666/p/13927356.html
Copyright © 2011-2022 走看看