2015-02-23 64 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