我如何生成一个包含四个项目的列表,这些项目都应具有相同的宽高比,这取决于480x360的原始分辨率? 原始宽高比480x360也属于列表。
Youtube上有一个示例,打开任何视频点击分享然后嵌入
现在,您可以看到我想要的列表与此处描述的相同。
1. 420x315
2. 480x360
3. 640x480
4. 960x720
*编辑我希望你现在明白我的问题
答案 0 :(得分:0)
900/1600 = 0.5625 得到一半 800 * 0.5625 = 450
获得aspectratio,宽度* 0.5625 =高度
代码
$width = 640;
$height = 480;
$ratio = $height / $width;
function ratio($width){
return $width * $ratio;
}
echo ratio(1280);
答案 1 :(得分:0)
<?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