2016-06-28 5 views
1

私のサイズ変更機能の使用と、要約のユーザー表示のための読み込み可能なイメージ比を取得します。 basicly私は次のように動作する機能が欲しい:PHPで読めるイメージの比率

function getHumanRatio($width, $height){ 
    // Do something over here. 
} 

echo getHumanRatio(1920, 1080) // 16:9 
echo getHumanRatio(480, 360) // 4:3 
echo getHumanRatio(360, 480) // 3:4 

としては、私は私のリサイズ機能ではなく、表示/要約機能でそれを使用したいが、数値比の使用は、ここで参考になっていないと述べました。あなたはGMPに依存したくない、あなたはこのコードを使用することができます場合は

+1

は、このリンクをチェックしてください。 com/questions/26697/getting-the-smallest-possible-integer-ratio-between-two-numbersの数字 –

答えて

3
<?php 

function computeReadableRatio($x, $y){ 
    $d = gmp_gcd($x, $y); 
    $xnew = gmp_div($x, $d); 
    $ynew = gmp_div($y, $d); 

    echo gmp_strval($d) . ' ' . gmp_strval($xnew) . ' ' . gmp_strval($ynew); 

} 

computeReadableRatio(40, 60); 
?> 
+0

これは今のところ仕事をしていますが、サーバがgmp thouを実行するかどうかを確認する必要があります。 $ dの意味を教えていただけますか? – IMarks

+0

'$ d'は' gmp_gcd() 'の結果であり、 '最大公約数'を表す必要があります。それ以外の可能性はありません。 –

2

を:($ratio = $oldWidth/$oldHeight;次のように計算する):のhttp://codereview.stackexchange

function greatestCommonDivisor($int1,$int2) 
{ 
    if ($int2 == 0) return $int1; 
    else return greatestCommonDivisor($int2,$int1 % $int2); 
} 

function getHumanRatio($int1,$int2) 
{ 
    $divisor = greatestCommonDivisor($int1,$int2); 
    return intdiv($int1,$divisor).':'.intdiv($int2,$divisor).'<br>'; 
} 

echo getHumanRatio(1920,1080); // 16:9 
echo getHumanRatio(480,360); // 4:3 
echo getHumanRatio(360,480); // 3:4 
関連する問題