【PHP】上传图片翻转问题

手机图片上传后获取到的宽高反了,网上的说法是操作系统里的文件属性功能可能已经把图片给修正过了,看到的图片是正确的,但是通过getimagesize获取到的宽高不对;这时需要用到exif扩展的exif_read_data方法获取图片头部信息

exif扩展安装:[https://www.cnblogs.com/lanse1993/p/13229238.html]

- 获取宽高信息

$orientationArr  = [1 => 0, 8 => -90, 3 => 180, 6 => 90];
$imageInfo       = exif_read_data($posterImg);
$orientation     = empty($imageInfo['Orientation']) ? -1 : $imageInfo['Orientation'];
if (! empty($orientationArr[$orientation]) && abs($orientationArr[$orientation]) == 90) {
    $posterImgWidth = $imageInfo['ExifImageLength'];
} else {
    $posterImgWidth = getimagesize($posterImg)[0];
}

- 需要用来画图时(这里用的Imagick)

//载入图像
$this->img = new Imagick(realpath($imgname));

// 图片是否旋转
$orientationArr  = [1 => 0, 8 => -90, 3 => 180, 6 => 90];
$imageInfo       = exif_read_data(realpath($imgname));
$orientation     = empty($imageInfo['Orientation']) ? -1 : $imageInfo['Orientation'];
if (! empty($orientationArr[$orientation])) {
    $this->img->rotateImage('#000', $orientationArr[$orientation]);
}
得意时做事,失意时读书
原文地址:https://www.cnblogs.com/lanse1993/p/15071589.html