2015-02-23 109 views
0

我的网站上有一些裁剪功能。计算图像裁剪比例

enter image description here

那么,问题是,真正的照片尺寸越大即种植面积,当我试图让这个裁剪行动我得到的比例从可见的实际结果 - 在CSS调整 - 区域而不是从这幅图像的阴影大小。如何计算裁剪比例从调整大小到实际大小这张图片。

我这样做,但它不能正常工作,我应该使用一些数学函数或什么,任何人都可以帮助吗?

var width = img.width; 
var height = img.height; 

// c.x, c.y, c.w, c.h is cropping coords which i recive from crop plugin callback function 
// 360 is modal window size, resized area for displaying img 

function showCoords(c) { 
    cords_node = { 
     "x": c.x , 
     "y": c.y, 
     "width": width < c.w ? c.w : (width/360) * c.w, 
     "height": height < c.h ? c.w : (width/360) * c.w 
     /*x2: c.x2, 
     y2: c.y2*/ 
    }; 
    return cords_node; 
} 

回答

2

找出变化的比率:

// find the ratio of change in H and W... 
var ratioW = $('.image')[0].naturalWidth/$('.image').width(); 
var ratioH = $('.image')[0].naturalHeight/$('.image').height(); 
var ratio = Math.min(ratioW, ratioH); 
// now.. multiply all your coords by 'ratio' 
// ... 
+0

谢谢!这就像一个魅力!.. – 2017-07-25 16:46:04