2016-06-28 2 views
1

크기 조정 기능을 사용하고 요약에 사용자가 표시 할 수있는 이미지의 비율을 얻고 싶습니다.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 

으로 내가 내 크기 조정 기능에서 사용 할뿐만 아니라 디스플레이/요약 함수에 말했지만 숫자 비율의 사용은 여기 helpfull되지 않습니다 는 basicly 나는 다음과 같이 작동하는 기능을합니다. (다음과 같이 계산 : $ratio = $oldWidth/$oldHeight;를) 경우

+1

확인이 링크 : HTTP : //codereview.stackexchange합니다. 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

당신이 GMP에 의존하지 않으려는,이 코드를 사용할 수 :

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