Home > PHP, web services, Wordpress > Calculate image aspect ratio in PHP

Calculate image aspect ratio in PHP

Calculating image aspect ratio in PHP is very simple. First you need to find greatest common divisor(GCD). You can do that by using following function:

function GCD($a, $b) {
while ($b != 0)
$remainder = $a % $b;
$a = $b;
$b = $remainder;
}
return abs ($a);
}
If you compiled your PHP version with GMP functions, you can also use following:

$gcd = gmp_gcd($a, $b);

 

After finding GCD, simply divide both sides with it:

 

$a = 2048; // screen width
$b = 1152; // screen height
$gcd = GCD($a, $b);
$a = $a/$gcd;
$b = $b/$gcd;
$ratio = $a . “:” . $b;

 

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: