2013-06-03 38 views
8

我創造與jQuery的圖片庫。有沒有可能使用jQuery來計算圖像是橫向還是縱向?如何計算的圖像是橫向還是縱向

感謝您的支持。

+0

檢查高/寬比?你有什麼嘗試? – JNF

+2

'如果寬度>長度然後其他景觀portrait' –

+0

@Christian馬克感謝基督教馬克 – geovani075

回答

17

您可以簡單地比較寬度和圖像的高度。

var someImg = $("#someId"); 
if (someImg.width() > someImg.height()){ 
    //it's a landscape 
} else if (someImg.width() < someImg.height()){ 
    //it's a portrait 
} else { 
    //image width and height are equal, therefore it is square. 
} 
2

下面的JavaScript函數將返回最適合的方向

function get_orientation(src){ 

img = new Image(); 
img.src = src; 
var width = img.width; 
var height = img.height; 
height = height + height // Double the height to get best 
//height = height + (height/2) // Increase height by 50% 

if(width > height) { 
return "landscape"; 
} else { 
return "portrait"; 
} 

} 
+0

這假設已將圖像加載到用戶界面。在實際計算比率之前,我們是否應該等待圖像加載事件觸發? –

+1

爲什麼「身高=身高+身高//雙倍身高以獲得最佳」? –

0

這爲我工作,利用自然高度/寬度來獲得原始屬性。

 function imageOrientation(src) { 

      var orientation, 
      img = new Image(); 
      img.src = src; 

      if (img.naturalWidth > img.naturalHeight) { 
       orientation = 'landscape'; 
      } else if (img.naturalWidth < img.naturalHeight) { 
       orientation = 'portrait'; 
      } else { 
       orientation = 'even'; 
      } 

      return orientation; 

     } 
相關問題