在 PHP 中检索图像方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13568440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve image orientation in PHP
提问by nimrod
How can I get the image orientation (landscape or portrait) of an image (JPEG or PNG) in PHP?
如何在 PHP 中获取图像(JPEG 或 PNG)的图像方向(横向或纵向)?
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.
我创建了一个 php 站点,用户可以在其中上传图片。在我将它们缩小到更小的尺寸之前,我想知道图像是如何定向的,以便正确缩放。
Thanks for your answer!
感谢您的回答!
回答by Jason
I've always done this:
我一直这样做:
list($width, $height) = getimagesize('image.jpg');
if ($width > $height) {
// Landscape
} else {
// Portrait or Square
}
回答by Paddyd
list($width, $height) = getimagesize("path/to/your/image.jpg");
if( $width > $height)
$orientation = "landscape";
else
$orientation = "portrait";
回答by ajtrichards
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 / ELSEstatement.
你可以用一个简单的IF / ELSE语句来做到这一点。
You could also use the function: Imagick::getImageOrientation
您还可以使用该功能: Imagick::getImageOrientation
回答by palerdot
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 . . ..
这里的最大尺寸是我将 height 和 width 初始化为 120px 之类的尺寸。. . 以便缩略图不超过该大小。. ..
This Works for me which is irrespective of landscape or portrait orientation and can be applied generally
这对我有用,无论横向或纵向如何,都可以普遍应用
回答by HackyStack
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));
}
回答by Tom F.
I am using this shorthand. Sharing just in case someone needs a one-line solution.
我正在使用这个速记。共享以防万一有人需要单线解决方案。
$orientation = ( $width != $height ? ( $width > $height ? 'landscape' : 'portrait' ) : 'square' );
First it checks if image is not 1:1 (square). If it is not it determines the orientation (landscape / portrait).
首先它检查图像是否不是 1:1(正方形)。如果不是,则确定方向(横向/纵向)。
I hope someone finds this helpful.
我希望有人觉得这有帮助。

