Shoaib Ijaz
Shoaib Ijaz

Reputation: 5597

How calculate aspect ratio from dimensions of image

I am using Jcrop for cropping image so i want to calculate ratio of height and width of image but problem is that there is no limit of maximum height and width.

when user upload image then i want to get height,width ratio so on cropping it should be crop with respect to aspect ratio for example

Width=835, Height=625 aspect ratio would be 167: 125

i have calculated this ratio from following link Aspect ratio calculator

I don't want to cacalute new height,width. I just want to calculate ratio 167: 125

How can i do this?

Upvotes: 3

Views: 10313

Answers (1)

Debajit Mukhopadhyay
Debajit Mukhopadhyay

Reputation: 4182

I think you are looking for HCF (Highest Common Factor) but the ratio (Width:835,Height:625) will be 167:125. Here is the function by which you can calculate HCF between 2 numbers.

 private int FindHCF(int m, int n)
 {
     int temp, remainder;
     if (m < n)
     {
         temp = m;
         m = n;
         n = temp;
     }
     while (true)
     {
         remainder = m % n;
         if (remainder == 0)
             return n;
         else
             m = n;
         n = remainder;
     }
 }

So here is the rest of the code

int hcf = FindHcf(835, 625);
int factorW = 835 / hcf;
int factorH = 625 / hcf;

Upvotes: 7

Related Questions