Javascript解决方案来计算图像比例以适合给定的平方英尺

时间:2018-02-19 18:12:56

标签: javascript jquery image-processing

好的,我正在开展一个观众可以上传图片的项目,而我的客户希望提供三种基于平方英尺的定义尺寸。

因此,如果观众加载5000 x 3000像素图像,我们想要知道它的大小是24英尺,一英寸或两英寸。它可能是18811 x 3036.

我已经抓住了我的大脑和我留下的小头发想要提出配方。没有找到任何搜索,但是,我已经在网站上看到了这一点,所以我知道它是可行的。

我尝试使用公式来获得共同点但是,它并不总是很像16:9或4:3。可能是2375:1890或更糟。

有想法的人吗? 真的很感激。

由于

1 个答案:

答案 0 :(得分:0)

var imgWidth = 150;   
var imgHeight = 100;
var boundBoxSize = 100;
var ret = {};
if(imgWidth>imgHeight){
          var ratio = imgHeight/imgWidth;
          ret['nheight'] = boundBoxSize*ratio;
          ret['nwidth'] = boundBoxSize;
  }else if(imgWidth<imgHeight){
          var ratio = imgWidth/imgHeight;
          ret['nheight'] = boundBoxSize;
          ret['nwidth'] = boundBoxSize*ratio;
  }else{
        ret['nheight'] = boundBoxSize;
        ret['nwidth'] = boundBoxSize;
  }
  document.write(ret.nwidth+'--'+ret.nheight)