【问题标题】:Calc Different video sizes with same aspect ratio计算具有相同纵横比的不同视频大小
【发布时间】:2014-06-20 11:43:40
【问题描述】:

如何生成一个包含四个项目的列表,这些项目都应该具有相同的纵横比,都取决于 480x360 的原始分辨率? 原始纵横比 480x360 也属于列表。

Youtube 上有一个例子打开任何视频点击分享然后嵌入

现在您会看到我想要的列表,与此处描述的相同。

1. 420x315

    2. 480x360

3. 640x480
4. 960x720

*编辑我希望你现在明白我的问题

【问题讨论】:

  • 您是否提供了所有有效分辨率的列表?
  • 是的,宽度除以高度
  • 我的要求是,如果您有某种类型的所有分辨率的数组......有效。有效是指 1280x720、320x240 等。无效会有些奇怪,例如 1067x89。那么:您是否有一个包含所有有效分辨率的数组,您必须从中获取所需比例的数组?
  • 不,请重新阅读我的问题
  • 那么 8x6 和 64x36 分别是您的示例的有效分辨率吗?您需要某种具有分辨率的数组。然后你需要遍历它,检查比率是否相等,然后打印或用它做任何你想做的事情。在这里您会找到一些可能的(在可用性方面有效)的解决方案:upload.wikimedia.org/wikipedia/commons/thumb/f/f0/…

标签: php screen-resolution aspect-ratio


【解决方案1】:
<?php

// create class to easily add and store resolutions
class Resolution
{
public $x;
public $y;
}

// create our original resolution and calculate its aspect ratio
$origRes = new Resolution();
$origRes->x = 640;
$origRes->y = 480;
$originalRatio = ($origRes->x / $origRes->y);

// create valid resolution
$firstRes = new Resolution();
$firstRes->x = 1280;
$firstRes->y = 720;

// create another resolution
$secondRes = new Resolution();
$secondRes->x = 320;
$secondRes->y = 240;

// ...and so on; two are enough for the example
// you must learn about arrays and came up with proper solution how to populate them

// create array of resolutions from existing resolutions
$arrayOfRes = array($firstRes, $secondRes);

// another way of adding could be: $arrayOfRes[] = $thirdRes

// because floating point values aren't perfect on computers,
// we need some range value to check if the values are "close enough"
// in other words: it's our precision
$epsilon = 0.00001;

// iterate over all elements in array
foreach ($arrayOfRes as $res)
{
// calculate ratio
$ratio = $res->x / $res->y;

// abs - absolute value, thus always positive
// we check if difference is precise enough and print some text
if(abs($ratio - $originalRatio) < $epsilon)
echo'print it here or do something';
}
?>

这是我作为非 PHP 程序员的十分钟回答代码。我认为这段代码是有效的,但可能会有一些错误。

[编辑]

检查了 ideone.com,修复了小错误,现在它可以正常工作了。 https://ideone.com/lpW9DM

【讨论】:

    【解决方案2】:

    900 / 1600 = 0.5625 得到一半 800 * 0.5625 = 450

    要获得纵横比,宽度 * 0.5625 = 高度

    代码

    $width = 640;
    $height = 480;
    $ratio = $height / $width;
    
    function ratio($width){
       return $width * $ratio;
    }
    
    echo ratio(1280);
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 2016-04-27
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多