how to Calculate whether an image is landscape or portrait

前端 未结 3 912
再見小時候
再見小時候 2021-02-05 22:06

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.

相关标签:
3条回答
  • 2021-02-05 22:45

    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.
    }
    
    0 讨论(0)
  • 2021-02-05 22:47

    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";
    }
    
    }
    
    0 讨论(0)
  • 2021-02-05 22:52

    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;
    
            }
    
    0 讨论(0)
提交回复
热议问题