Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示正确的纵横比_Php_Aspect Ratio - Fatal编程技术网

Php 显示正确的纵横比

Php 显示正确的纵横比,php,aspect-ratio,Php,Aspect Ratio,我试图获得一张照片的纵横比,但是下面的代码显示了一张宽度为3776、高度为2520(472:315)的照片的错误纵横比,但它显示了一张宽度为3968、高度为2232(16:9)的照片的正确纵横比 我怎样才能解决我的问题 提前感谢。实际上,3780x2520是3:2的纵横比;因为你用3776表示宽度,472:315是正确的比例。如果你做了除法,它就等于1.498,它非常接近1.5,可以考虑舍入到3:2。 如果您只需要“标准”比率(如“3:2”或“16:9”),则可以将检测到的比率传递给另一个函数,

我试图获得一张照片的纵横比,但是下面的代码显示了一张宽度为3776、高度为2520(472:315)的照片的错误纵横比,但它显示了一张宽度为3968、高度为2232(16:9)的照片的正确纵横比

我怎样才能解决我的问题


提前感谢。

实际上,3780x2520是3:2的纵横比;因为你用3776表示宽度,472:315是正确的比例。如果你做了除法,它就等于1.498,它非常接近1.5,可以考虑舍入到3:2。 如果您只需要“标准”比率(如“3:2”或“16:9”),则可以将检测到的比率传递给另一个函数,该函数取而代之,对其进行四舍五入以查找最接近/最佳匹配

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

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

found: 16:9
match: 16:9

found: 472:315
match: 3:2

found: 3:2
match: 3:2
*如果您需要参考,我从中获取了“标准”纵横比列表。

我发现它在Javascript中工作得很好。所以我把它移植到PHP

  /**
   * Returns the nearest aspect ratio of a width and height within a limited range of possible aspect ratios.
   * In other words, while 649x360 technically has an aspect ratio of 649:360,
   * it’s often useful to know that the nearest normal aspect ratio is actually 9:5 (648x360).
   * @param $width
   * @param $height
   * @param int $side The nearest ratio to side with. A number higher than zero
   * tells the function to always return the nearest ratio that is equal or higher
   * than the actual ratio, whereas a smaller number returns the nearest ratio higher
   * that is equal or smaller than the actual ratio. Defaults to 0.
   * @param int $maxW The maximum width in the nearest normal aspect ratio. Defaults to 16.
   * @param int $maxH The maximum height in the nearest normal aspect ratio. Defaults to 16.
   * @return string|null
   */

   function closestAspectRatio($width, $height, $side = 0, $maxW = 16, $maxH = 16) {

    $ratiosT = [];
    $ratios = [];
    $ratio = ($width * 100) / ($height * 100);
    $maxW++;
    $maxH++;

    for ($w = 1; $w < $maxW; $w++) {
      for ($h = 1; $h < $maxH; $h++) {
        $ratioX = strval(($w * 100) / ($h * 100));
        if (!array_key_exists($ratioX, $ratiosT)) {
          $ratiosT[$ratioX] = TRUE;
          $ratios[$w . ':' . $h] = $ratioX;
        }
      }
    }

    $match = NULL;

    foreach ($ratios as $key => $value) {

      if (!$match || (!$side && abs($ratio - $value) < abs($ratio - $ratios[$match])
        ) || (
          $side < 0 && $value <= $ratio && abs($ratio - $value) < abs($ratio - $ratios[$match])
        ) || (
          $side > 0 && $value >= $ratio && abs($ratio - $value) < abs($ratio - $ratios[$match])
        )) {
        $match = $key;
      }
    }

    return $match;
  }
以下是JS演示供参考:

console.log('3776x2520='+最接近正常的aspectratio(37762520,0));
log('2520 x 3776='+最接近法线的aspectratio(25203776,0));
函数最接近法线Aspectratio(宽度、高度、侧面){
变量
比率=(宽度*100)/(高度*100),
参数中的maxW=3?参数[2]:16,
参数中的maxH=4?参数[3]:16,
ratiosW=新数组(maxW).join(',').split(','),
ratiosH=新数组(maxH).join(',').split(','),
ratiosT={},
比率={},
匹配,
钥匙
ratiosW.forEach(函数(空,ratioW){
++拉蒂奥;
ratiosH.forEach(函数(空,ratioH){
++拉蒂奥;
ratioX=(ratioW*100)/(ratioH*100);
如果(!ratiosT[ratioX]){
ratiosT[ratioX]=真;
比率[ratioW+':'+ratioH]=ratioX;
}
});
});
对于(输入比率){
如果(!match | | |(!side&&Math.abs(ratio-ratio[key])=ratio&&Math.abs(ratio-ratios[key])}
为什么这不起作用?您已经根据您的代码找到了纵横比(这是可行的)。问题出在哪里?这张宽3776,高2520的照片是以3:2的高宽比拍摄的。不是472:315(长宽比是否存在?!)。因此我的问题是,实际上,3780x2520是3:2的长宽比;因为你用3776表示宽度,472:315是正确的比例。如果你做除法,结果是1.498,这已经足够接近了。如果您想要“完美”的比率(如标准的“3:2”或“16:9”),可以将检测到的比率传递给另一个函数,该函数取而代之地对其进行四舍五入,以找到最接近/最佳的匹配项。Hm。好吧。这个函数应该是什么样子的?我是用PHP计算大小的初学者。@ErikEdgren很棒,很高兴我能帮上忙。如果您以后注意到任何问题,请告诉我,我可以修改-谁知道还有谁也需要此问题=PVery true:)如果我需要有关此函数的更多帮助,我会回来。1000:1333应为3:4,因此这不会处理此类情况。@MrUpsidown当然,但您必须意识到StackOverflow“答案”更多的是指导如何完成某件事,而不是“这是一个银盘上的完美解决方案”=P。此外,我的答案(8年前)是关于“标准”纵横比——没有一个高度大于宽度。我很高兴你也提供了一个单独的答案来帮助其他人解决这个问题,但希望它能帮助所有人=]我当然知道,我只是向所有对你的解决方案感兴趣的人指出这个问题。
$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
  /**
   * Returns the nearest aspect ratio of a width and height within a limited range of possible aspect ratios.
   * In other words, while 649x360 technically has an aspect ratio of 649:360,
   * it’s often useful to know that the nearest normal aspect ratio is actually 9:5 (648x360).
   * @param $width
   * @param $height
   * @param int $side The nearest ratio to side with. A number higher than zero
   * tells the function to always return the nearest ratio that is equal or higher
   * than the actual ratio, whereas a smaller number returns the nearest ratio higher
   * that is equal or smaller than the actual ratio. Defaults to 0.
   * @param int $maxW The maximum width in the nearest normal aspect ratio. Defaults to 16.
   * @param int $maxH The maximum height in the nearest normal aspect ratio. Defaults to 16.
   * @return string|null
   */

   function closestAspectRatio($width, $height, $side = 0, $maxW = 16, $maxH = 16) {

    $ratiosT = [];
    $ratios = [];
    $ratio = ($width * 100) / ($height * 100);
    $maxW++;
    $maxH++;

    for ($w = 1; $w < $maxW; $w++) {
      for ($h = 1; $h < $maxH; $h++) {
        $ratioX = strval(($w * 100) / ($h * 100));
        if (!array_key_exists($ratioX, $ratiosT)) {
          $ratiosT[$ratioX] = TRUE;
          $ratios[$w . ':' . $h] = $ratioX;
        }
      }
    }

    $match = NULL;

    foreach ($ratios as $key => $value) {

      if (!$match || (!$side && abs($ratio - $value) < abs($ratio - $ratios[$match])
        ) || (
          $side < 0 && $value <= $ratio && abs($ratio - $value) < abs($ratio - $ratios[$match])
        ) || (
          $side > 0 && $value >= $ratio && abs($ratio - $value) < abs($ratio - $ratios[$match])
        )) {
        $match = $key;
      }
    }

    return $match;
  }
print closestAspectRatio(3776, 2520, 0, 16, 16); // prints 3:2
print closestAspectRatio(2520, 3776, 0, 16, 16); // prints 2:3