Get aspect ratio from width and height of image (PHP or JS)
Asked Answered
C

6

4

I can't believe I can't find the formula for this. I am using a PHP script called SLIR to resize images. The script asks to specify an aspect ratio for cropping. I'd like to get the aspect ratio based on the width and height of the image being specified in a form I'm letting users enter these values in. For example, if a user enters a 1024x768 image, I would get an aspect ratio of 4:3. For the life of me, I can't find an example of the formula in PHP or Javascript I can use to get the aspect ratio values based on knowing the w,h and plug the aspect ratio into a variable.

Carmelocarmen answered 7/11, 2011 at 23:16 Comment(1)
not sure if I understand right, but maybe you are looking for 1024/768 (width/height)?Submerged
K
1

There is no need for you to do any kind of calculation.

Just because it says aspect ratio doesn't mean it has to be one of a limited set of commonly used ratios. It can be any pair of numbers separated by a colon.

Quoting from the SLIR usage guide:

For example, if you want your image to be exactly 150 pixels wide by 100 pixels high, you could do this:

<img src="/slir/w150-h100-c150:100/path/to/image.jpg" alt="Don't forget your alt text" /> 

Or, more concisely:

<img src="/slir/w150-h100-c15:10/path/to/image.jpg" alt="Don't forget your alt text" />

Note that they didn't bother to reduce that even further to c3:2.

So, simply use the values as entered by the user: 1024:768.

If you want to be concise, calculate the greatest common divisor of the width and height and divide both of them by that. That would reduce your 1024:768 down to 4:3.

Key answered 8/11, 2011 at 0:14 Comment(0)
B
11

If you can get one of: height, width then you can calculate the missing width height:

original width * new height / original height = new width;

original height * new width / original width = new height;

Or if you just want a ratio:

original width / original height = ratio

Bowdlerize answered 7/11, 2011 at 23:19 Comment(0)
P
7

to get the aspect ratio just simplify the width and height like a fraction for example:

1024      4
----  =  ---
768       3

the php code:

function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        gcd($b, $r) :
        abs($b);
}

  $gcd=gcd(1024,768);

  echo "Aspect ratio = ". (1024/$gcd) . ":" . (768/$gcd);
Proctor answered 8/11, 2011 at 0:6 Comment(0)
E
4

Here's a much simpler alternative for greatest common divisor integer ratios:

function ratio( $x, $y ){
    $gcd = gmp_strval(gmp_gcd($x, $y));
    return ($x/$gcd).':'.($y/$gcd);
}

The request echo ratio(25,5); returns 5:1.

If your server wasn't compiled with GMP functions ...

function gcd( $a, $b ){
    return ($a % $b) ? gcd($b,$a % $b) : $b;
}
function ratio( $x, $y ){
    $gcd = gcd($x, $y);
    return ($x/$gcd).':'.($y/$gcd);
}
Evaevacuant answered 20/4, 2014 at 7:10 Comment(2)
Why would I be getting 331:220 when the image size is 662x440, was expecting 6:4Perspiratory
This function is all about reducing a ratio to its lowest common denominator. For example, 6:4 = 660x440 ... not 662:440, whose lowest exact ratio = 331:220. Does that make sense?Evaevacuant
K
1

There is no need for you to do any kind of calculation.

Just because it says aspect ratio doesn't mean it has to be one of a limited set of commonly used ratios. It can be any pair of numbers separated by a colon.

Quoting from the SLIR usage guide:

For example, if you want your image to be exactly 150 pixels wide by 100 pixels high, you could do this:

<img src="/slir/w150-h100-c150:100/path/to/image.jpg" alt="Don't forget your alt text" /> 

Or, more concisely:

<img src="/slir/w150-h100-c15:10/path/to/image.jpg" alt="Don't forget your alt text" />

Note that they didn't bother to reduce that even further to c3:2.

So, simply use the values as entered by the user: 1024:768.

If you want to be concise, calculate the greatest common divisor of the width and height and divide both of them by that. That would reduce your 1024:768 down to 4:3.

Key answered 8/11, 2011 at 0:14 Comment(0)
B
0

If you don't have the GMP math extension installed. Here is a dependancy free solution I use:

function aspect_ratio($width, $height) {

  $ratio = [$width, $height];

  for ($x = $ratio[1]; $x > 1; $x--) {
    if (($ratio[0] % $x) == 0 && ($ratio[1] % $x) == 0) {
      $ratio = [$ratio[0] / $x, $ratio[1] / $x];
    }
  }

  return implode(':', $ratio);
}

It can be used like this:

echo aspect_ratio(1920, 1080); // Outputs 16:9
echo aspect_ratio(1024, 768); // Outputs 4:3
echo aspect_ratio(200, 300); // Outputs 2:3

Source: https://forums.digitalpoint.com/threads/how-i-will-get-ratio-of-2-number-in-php.937696/

Blumenthal answered 3/4, 2022 at 22:41 Comment(0)
Q
0

Here's a much simpler alternative for get aspect ratios without gmp extension:

<?php

function getAspectRatio(int $width, int $height)
{
    // search for greatest common divisor
    $greatestCommonDivisor = static function($width, $height) use (&$greatestCommonDivisor) {
        return ($width % $height) ? $greatestCommonDivisor($height, $width % $height) : $height;
    };

    $divisor = $greatestCommonDivisor($width, $height);

    return $width / $divisor . ':' . $height / $divisor;
}

echo getAspectRatio(1280, 1024);
echo PHP_EOL;
echo getAspectRatio(1275, 715);

Source from: https://gist.github.com/wazum/5710d9ef064caac7b909a9e69867f53b

Quinquagesima answered 21/6, 2023 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.