PHP getimagesize()混合了宽度和高度

Chi*_*iel 8 php orientation getimagesize

我使用PHP脚本上传图像,然后使用PHP的getImageSize()获取尺寸,然后根据图片方向(纵向或横向)对图像进行处理.

然而(PHP版本5.4.12)在某些 .jpg文件中它获得了高度和宽度,并且在某些(用iPhone拍摄)它交换它们,认为肖像图片实际上是风景.
它不仅发生在我的本地Wampserver上,也发生在远程服务器上(具有不同的PHP版本).

有谁知道如何

1)修复这个或
2)找到解决问题的方法?

Pab*_*eco 7

某些相机在文件本身的元数据部分中包含方向标记.这样设备本身可以每次都以正确的方向显示它,无论原始数据中的图片方向如何.

看起来Windows不支持读取此方向标记,而只是读取像素数据并按原样显示它.

解决方案是在每个图像的基础上改变受影响图片的元数据中的方向标签,或者

使用PHP的exif_read_data()函数来读取方向并相应地定位图像,如下所示:

<?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
?>
Run Code Online (Sandbox Code Playgroud)

参考文献: