我需要一些基本的帮助。我正在研究一个php函数,它接收这些可能的输入字符串(这些是示例,可以是任何解析):
1600x900
1440x900
1366x768
1360x768
1280x1024
1280x800
1024x1024
1024x768
640x960
320x480
320x480
etc
我想处理这些字符串中的任何一个并返回适当的纵横比字符串,格式如下:
5:4
4:3
16:9
etc
对解决这个问题的简单方法有什么想法吗?
编辑:这是我一直在使用的参考图表:
http://en.wikipedia.org/wiki/File:Vector_Video_Standards2.svg
编辑:以下是javascript的答案:
aspectRatio: function(a, b) {
var total = a + b;
for(var i = 1; i <= 40; i++) {
var arx = i * 1.0 * a / total;
var brx = i * 1.0 * b / total;
if(i == 40 || (
Math.abs(arx - Math.round(arx)) <= 0.02 &&
Math.abs(brx - Math.round(brx)) <= 0.02)) {
// Accept aspect ratios within a given tolerance
return Math.round(arx)+':'+Math.round(brx);
}
}
},
最佳答案
以下功能可能更有效:
function aspectratio($a,$b){
# sanity check
if($a<=0 || $b<=0){
return array(0,0);
}
$total=$a+$b;
for($i=1;$i<=40;$i++){
$arx=$i*1.0*$a/$total;
$brx=$i*1.0*$b/$total;
if($i==40||(
abs($arx-round($arx))<=0.02 &&
abs($brx-round($brx))<=0.02)){
# Accept aspect ratios within a given tolerance
return array(round($arx),round($brx));
}
}
}
我把这个代码放在公共域中。