I was curious myself as to how you might go about doing this, so I whipped up a function and it appears to work... not sure how accurate it is, though.
function get_aspect_ratio($x, $y) {
$r = array( // array of common ratios
array(4, 3),
array(3, 2),
array(16, 9),
array(1.85, 1),
array(2.39, 1)
);
for($i = 0, $prev = null; isset($r[$i]); $i++)
if($prev == null)
$prev = abs(($x / $y) - ($r[$i][0] / $r[$i][1]));
else {
$next = abs(($x / $y) - ($r[$i][0] / $r[$i][1]));
if($next > $prev)
break;
else
$prev = $next;
}
if($i == count($r))
return false;
else
return $r[$i-1];
}
It could be used as such:
$ratio = get_aspect_ratio($image_x, $image_y);
echo "The ratio of the image is $ratio[0]:$ratio[1]";