计算图像裁剪比例

Calculate image cropping proportions

我的网站上有一些裁剪功能。

所以,问题在于真实照片的尺寸比裁剪区域大,当我试图获得此裁剪操作的实际结果时,我从可见区域获得比例 - 在 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;
}

找出变化率:

// 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'
// ...