skip to Main Content

I need to get the original aspect ratio of the image
enter image description here

Expect to get the original aspect ratio of the image

2

Answers


  1. Try this :

    const aspectRatio = yourImage.naturalWidth / yourImage.naturalHeight;

    console.log(‘Aspect ratio:’, aspectRatio);

    Login or Signup to reply.
  2. Pass the "getAspectRatio()" function an "img.src" parameter.
    The actual dimensions of the picture will be taken, after which a check will be made whether the picture is in portrait or landscape format.
    As a result, the function returns aspect ratio of the image.

    function getAspectRatio(image) {
    
        const w = image.naturalWidth;
        const h = image.naturalHeight;
    
        let aspectRatio;
    
        if (w > h) {
            aspectRatio = w / h;
        } else {
            aspectRatio = h / w;
        }
    
        return aspectRatio;
    
    };
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search