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

后端 未结 4 1279
小鲜肉
小鲜肉 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:24

    to get the aspect ratio just simplify the width and height like a fraction for example:

    1024      4
    ----  =  ---
    768       3
    

    the php code:

    function gcd($a, $b)
    {
        if ($a == 0 || $b == 0)
            return abs( max(abs($a), abs($b)) );
    
        $r = $a % $b;
        return ($r != 0) ?
            gcd($b, $r) :
            abs($b);
    }
    
      $gcd=gcd(1024,768);
    
      echo "Aspect ratio = ". (1024/$gcd) . ":" . (768/$gcd);
    

提交回复
热议问题