php - 使用 PHP 将分辨率转换为纵横比

标签 php string image image-processing

我需要一些可能非常基本的帮助。我正在开发一个接收这些可能的输入字符串的 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/

相关文章:

javascript - 正确的语法是什么?

c++ - 自动分离已相乘的两个图像

php - Magento 和可配置的产品属性

php - mysqli_fetch_assoc 不返回第一行

linux - 如何计算字符串中的特定字符 - CSH

java - 从java中的其他 Activity 更改字符串的值

php - Google Visualization API 中的逗号分隔数据

php - 从mysql打印json

ruby - 检查字符串是否包含 Ruby 中的特定字符序列

android - 包含姓名和图片 URI 的联系人查询