显示正确的长宽比 [英] Show correct aspect ratio

查看:130
本文介绍了显示正确的长宽比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取照片的长宽比,但是以下代码显示了宽度为3776且高度为2520(472:315)的照片的宽高比错误,但是在3968英寸的照片中却显示了正确的长宽比宽,高2232(16:9).

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($widtho, $heighto);
echo ($widtho / $gcd).':'.($heighto / $gcd);

我该如何解决我的问题?

谢谢.

解决方案

实际上,3780x2520的纵横比为3:2;因为您使用3776作为宽度,所以472:315是正确的比率.如果进行除法运算,则得出的结果为1.498,考虑到舍入为3:2时,它已经足够接近1.5了.

如果您只需要标准"比率(例如"3:2"或"16:9"),则可以将检测到的比率传递给另一个函数,该函数会将它们取整以找到最接近/最匹配的值.

这是一个可以为您舍入的综合函数(仅针对示例中的尺寸进行了测试,因此我尚不能保证100%成功):

function findBestMatch($ratio) {
    $commonRatios = array(
        array(1, '1:1'), array((4 / 3), '4:3'), array((3 / 2), '3:2'),
        array((5 / 3), '5:3'), array((16 / 9), '16:9'), array(3, '3')
    );

    list($numerator, $denominator) = explode(':', $ratio);
    $value = $numerator / $denominator;

    $end = (count($commonRatios) - 1);
    for ($i = 0; $i < $end; $i++) {
        if ($value == $commonRatios[$i][0]) {
            // we have an equal-ratio; no need to check anything else!
            return $commonRatios[$i][1];
        } else if ($value < $commonRatios[$i][0]) {
            // this can only happen if the ratio is `< 1`
            return $commonRatios[$i][1];
        } else if (($value > $commonRatios[$i][0]) && ($value < $commonRatios[$i + 1][0])) {
            // the ratio is in-between the current common-ratio and the next in the list
            // find whichever one it's closer-to and return that one.
            return (($value - $commonRatios[$i][0]) < ($commonRatios[$i + 1][0] - $value)) ? $commonRatios[$i][1] : $commonRatios[$i + 1][1];
        }
    }

    // we didn't find a match; that means we have a ratio higher than our biggest common one
    // return the original value
    return $ratio;
}

要使用此功能,请将比率字符串(不是数字值)传递给它,它将尝试在常见比率列表中找到最佳匹配项".

示例用法:

$widtho = 3968;
$heighto = 2232;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

$widtho = 3776;
$heighto = 2520;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

$widtho = 3780;
$heighto = 2520;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

以上测试将输出以下内容:

found: 16:9
match: 16:9

found: 472:315
match: 3:2

found: 3:2
match: 3:2

*我从维基百科,如果您需要参考.

I am trying to get the aspect ratio of a photo but the following code shows wrong aspect ratio on a photo with 3776 in width and 2520 in height (472:315) but it shows correct aspect ratio on a photo with 3968 in width and 2232 in height though (16:9).

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($widtho, $heighto);
echo ($widtho / $gcd).':'.($heighto / $gcd);

How can I solve my problem?

Thanks in advance.

解决方案

Actually, 3780x2520 is an aspect ratio of 3:2; because you're using 3776 for the width, 472:315 is the correct ratio. If you do the division, it comes out to 1.498, which is pretty close-enough to 1.5 to consider rounding to 3:2.

If you want only "standard" ratios (like "3:2" or "16:9"), you could pass the detected ratio to another function that rounds them to find the nearest/best-match instead.

This is a thrown-together function that can do the rounding for you (only tested against the dimensions in your example, so I can't guarantee 100% success yet):

function findBestMatch($ratio) {
    $commonRatios = array(
        array(1, '1:1'), array((4 / 3), '4:3'), array((3 / 2), '3:2'),
        array((5 / 3), '5:3'), array((16 / 9), '16:9'), array(3, '3')
    );

    list($numerator, $denominator) = explode(':', $ratio);
    $value = $numerator / $denominator;

    $end = (count($commonRatios) - 1);
    for ($i = 0; $i < $end; $i++) {
        if ($value == $commonRatios[$i][0]) {
            // we have an equal-ratio; no need to check anything else!
            return $commonRatios[$i][1];
        } else if ($value < $commonRatios[$i][0]) {
            // this can only happen if the ratio is `< 1`
            return $commonRatios[$i][1];
        } else if (($value > $commonRatios[$i][0]) && ($value < $commonRatios[$i + 1][0])) {
            // the ratio is in-between the current common-ratio and the next in the list
            // find whichever one it's closer-to and return that one.
            return (($value - $commonRatios[$i][0]) < ($commonRatios[$i + 1][0] - $value)) ? $commonRatios[$i][1] : $commonRatios[$i + 1][1];
        }
    }

    // we didn't find a match; that means we have a ratio higher than our biggest common one
    // return the original value
    return $ratio;
}

To use this function, you pass in the ratio-string (not the numeric value) to it and it will attempt to "find a best match" in the common list of ratios.

Example usage:

$widtho = 3968;
$heighto = 2232;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

$widtho = 3776;
$heighto = 2520;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

$widtho = 3780;
$heighto = 2520;
$gcd = gcd($widtho, $heighto);
$ratio = ($widtho / $gcd).':'.($heighto / $gcd);
echo 'found: ' . $ratio . "\n";
echo 'match: ' . findBestMatch($ratio) . "\n";

The above test will output the following:

found: 16:9
match: 16:9

found: 472:315
match: 3:2

found: 3:2
match: 3:2

* I took the list of "standard" aspect ratios from wikipedia, if you want a reference.

这篇关于显示正确的长宽比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆