how to Calculate whether an image is landscape or portrait

前端 未结 3 916
再見小時候
再見小時候 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条回答
  •  梦毁少年i
    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;
    
            }
    

提交回复
热议问题