0
$\begingroup$

What formula is for counting aspect ratio of the image as in this site:

Aspect ratio calculator (ARC) | NinjaUnits

For example:

 1000x1200 its aspect ratio 5:6 
 1000x1333 its aspect ratio 3:4
 2592x3888 its aspect ratio 2:3

I want to determine this by handling an uploaded image so it should be a formula for counting this, but did not figure out this yet.

$\endgroup$
2
  • 1
    $\begingroup$ Just to be clear, you want the integer ratio and not the actual value? For example, the aspect ratio is just width / height, so for 1000x1200, it's 1000/1200 = 0.8333... But if you want to print it out as a ratio with a format like 16:9, then it's slightly more complicated (though not much). But it's not clear from your question which one you're asking about. $\endgroup$ Commented Mar 12, 2017 at 19:03
  • $\begingroup$ Yes, it is 1000/1200 = 0.8333, but how you actually get 5:6 by knowing just this - 1000/1200 = 0.8333 ? $\endgroup$
    – funguy
    Commented Mar 12, 2017 at 21:06

2 Answers 2

2
$\begingroup$

You need to take your width and height and make them into a fraction, then reduce it. To do that, you'll need to find the greatest common divisor of the numerator and denominator, and divide them both by that.

Now take the new fraction you got by dividing both the numerator and denominator by the greatest common divisor and do it again. Repeat this process until the greatest common divisor is 1. At that point you have your irreducible fraction and you can print it.

Another way to do it is to use Euclid's Algorithm.

Example:

Let's look at your top example, 1,000 x 1,200

1,000 / 1,200 = ?

We can see by examination that they both end in "00" so they're both divisible by 100. So start there:

1,000 ÷ 100 = 10
1,200 ÷ 100 = 12

Next, we see that 10 and 12 are both even, so they're both divisible by 2:

10 ÷ 2 = 5
12 ÷ 2 = 6

Finally, we see that 5 and 6 share no common factors besides 1. (This is because 5 is prime, so its only factors are 5 and 1. 6 has the factor 2 and 3, as well as 1 and 6.) So we have reached the end. The reduced fraction is 5 / 6.

One More Thing

You might also want to be able to detect cases of irrational ratios, such as the golden ratio, which is quite popular in visual arts. Such a ratio will not be possible to divide into small integers, so checking whether width / height is within some small distance of the golden ratio may be sufficient.

$\endgroup$
3
  • $\begingroup$ Can you give an example with numbers? Would appreciate. $\endgroup$
    – funguy
    Commented Mar 12, 2017 at 23:12
  • $\begingroup$ What about other value 1000x1333 it does not add up. It seems like a few people are saying that aspect ratio of this is 3:4, but if I try to count it is always 1000x1333. How they get 3:4 for this? $\endgroup$
    – funguy
    Commented Mar 14, 2017 at 11:31
  • $\begingroup$ That's a case of integer rounding. Technically, 3:4 would be a ratio of 0.75, whereas 1000:1333 is 0.750187546886722... So it's very close to 3:4, but not exact. So another way you could approach this is to have a table of common aspect ratios (like 1:1, 2:1, 3:4, 4:5, 16:9, etc.) and then find the one that's closest. Or you could round the result of the width / height to some decimal amount and then compare. $\endgroup$ Commented Mar 14, 2017 at 16:37
1
$\begingroup$

One way you could do it is by writing the dimensions as a fraction, and simplifying that fraction (dividing each of the numbers by their greatest common denominator). From that you can then create a string and format it as [simplified numerator]:[simplified denominator].

This SO question talks about this too, and it shows some code to implementing it in JavaScript and also suggests some libraries... although I don't know what language you're using, I think this will still help.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.