Retrieve image orientation in PHP

后端 未结 6 974
闹比i
闹比i 2021-02-04 06:54

How can I get the image orientation (landscape or portrait) of an image (JPEG or PNG) in PHP?

I created a php site where users can upload pictures. Before I scale them

6条回答
  •  生来不讨喜
    2021-02-04 07:29

    Simple. Just check the width and height and compare them to get orientation. Then resize accordingly. Straight-forward really. If you are trying to maintain aspect ratio, but fit into some square box you could use something like this:

    public static function fit_box($box = 200, $x = 100, $y = 100)
    {
      $scale = min($box / $x, $box / $y, 1);
      return array(round($x * $scale, 0), round($y * $scale, 0));
    }
    

提交回复
热议问题