Get aspect ratio from width and height of image (PHP or JS)

后端 未结 4 1278
小鲜肉
小鲜肉 2021-01-01 04:51

I can\'t believe I can\'t find the formula for this. I am using a PHP script called SLIR to resize images. The script asks to specify an aspect ratio for cropping. I\'d like

4条回答
  •  不知归路
    2021-01-01 05:50

    Here's a much simpler alternative for greatest common divisor integer ratios:

    function ratio( $x, $y ){
        $gcd = gmp_strval(gmp_gcd($x, $y));
        return ($x/$gcd).':'.($y/$gcd);
    }
    

    The request echo ratio(25,5); returns 5:1.

    If your server wasn't compiled with GMP functions ...

    function gcd( $a, $b ){
        return ($a % $b) ? gcd($b,$a % $b) : $b;
    }
    function ratio( $x, $y ){
        $gcd = gcd($x, $y);
        return ($x/$gcd).':'.($y/$gcd);
    }
    

提交回复
热议问题