我需要一些可能非常基本的帮助。我正在开发一个接收这些可能的输入字符串的 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));
}
}
}
我将此代码放在公共(public)域中。
关于php - 使用 PHP 将分辨率转换为纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132041/