PHP GetImageSize 反转 Width/Height

PHP GetImageSize reverses Width/Height

我在下面附上了包含这些细节的图片: 宽度:3024px 高度:4032px

getimagesize()函数returns相反:

$size = GetimageSize("test.jpg");
echo "image width: " . $size[0] . ", height: " . $size[1];

image width: 4032, height: 3024

这怎么可能?

您可以使用下图自己尝试。

* 更新 * 从 post 中删除图像并添加 link 到 zip 文件(包含图像),因为图像在从堆栈溢出处理后工作正常。 Tinyupload ZIP

结果截图:

您的图片可能在您查看时自动旋转。方向将在获得正确的高度和宽度方面发挥作用。此代码取自 php documentation 将确保您的图像正确旋转。 link中还有许多其他示例可供选择。

<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}
// $image now contains a resource with the image oriented correctly
?>

不能完全信任 exif 方向标签,因为它实际上取决于设置使用正确值的标签的程序或设备。

这篇文章更好地解释了 exif 方向标签以及为什么它是一团糟。 http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/

函数 getimagesize() 更改横向(水平)照片的宽度和高度。 您可以使用此代码:

<?php
$img = "test.jpg";
$exif = exif_read_data($img);
if(empty($exif['Orientation'])) {
    list($width, $height, $type, $attr) = getimagesize($img);
}else{
    list($height, $width, $type, $attr) = getimagesize($img);
}
?>

但在 PHP7 及更高版本中已自动修复。