计算具有相同宽高比的不同视频大小


Calc Different video sizes with same aspect ratio

我怎么能生成一个列表与四个项目都应该有相同的纵横比都取决于原始分辨率480x360 ?原始宽高比480x360也属于列表。

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

现在你看到了我想要的列表,就像这里描述的一样。

1. 420x315
    2. 480x360
3. 640x480
4. 960x720
我希望你现在明白我的问题了
<?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

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);