how to Calculate whether an image is landscape or portrait

不打扰是莪最后的温柔 提交于 2021-02-17 19:30:08

问题


I am creating an image gallery with jquery. Is there any possibilities to Calculate whether an image is landscape or portrait using jquery?

Thanks for your support.


回答1:


You can simply compare width and height of the image.

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:


This worked for me, using the natural height/width to get the original properties.

       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;

        }



回答3:


Below javascript function will return the best suited Orientation

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";
}

}


来源:https://stackoverflow.com/questions/16891467/how-to-calculate-whether-an-image-is-landscape-or-portrait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!