Retrieve image orientation in PHP

自作多情 提交于 2019-12-18 11:51:23

问题


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 down to a smaller size, I want to know how the image is orientated in order to scale it properly.

Thanks for your answer!


回答1:


I've always done this:

list($width, $height) = getimagesize('image.jpg');
if ($width > $height) {
    // Landscape
} else {
    // Portrait or Square
}



回答2:


list($width, $height) = getimagesize("path/to/your/image.jpg");

if( $width > $height)
    $orientation = "landscape";
else
    $orientation = "portrait";



回答3:


I suppose you could check if the Image width is longer than the length for Landscape and for Portrait if the Length is longer than width.

You can do that with a simple IF / ELSE statement.

You could also use the function: Imagick::getImageOrientation

http://php.net/manual/en/imagick.getimageorientation.php




回答4:


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));
}



回答5:


I use a generalized scaling down algorithm like . ..

   function calculateSize($width, $height){

            if($width <= maxSize && $height <= maxSize){
                $ratio = 1;
            } else if ($width > maxSize){
                $ratio = maxSize/$width;
                } else{
                    $ratio = maxSize/$height;
                    }

        $thumbwidth =  ($width * $ratio);
        $thumbheight = ($height * $ratio);
        }

Here max size is the one I initialized to something like 120px for both height and width . . . so that the thumbnail does not exceed that size . . ..

This Works for me which is irrespective of landscape or portrait orientation and can be applied generally



来源:https://stackoverflow.com/questions/13568440/retrieve-image-orientation-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!