技术饭
exif_read_data()解决上传图片旋转、图片反向问题,getimagesize获取图片高度宽度相反的问题
PHP getimagesize 函数获取的图像宽高反了?
exif_read_data()解决上传图片旋转、图片反向问题,getimagesize获取图片高度宽度相反的问题,手机图片上传之后出现了反向问题,操作系统里的文件属性功能可能已经把图片给修正过了,那这时候就要把图片给旋转回来就需要用到php的exif_read_data方法。
返回结果:
Array
(
[FileName] => phpx7iokl
[FileDateTime] => 1558932683
[FileSize] => 3896832
[FileType] => 2
[MimeType] => image/jpeg
[SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP
[COMPUTED] => Array
(
[html] => width="4000" height="3000"
[Height] => 3000
[Width] => 4000
[IsColor] => 1
[ByteOrderMotorola] => 1
[ApertureFNumber] => f/1.8
[Thumbnail.FileType] => 2
[Thumbnail.MimeType] => image/jpeg
)
[Model] => MI 6X
[Software] => wayne-user 9 PKQ1.180904.001 V10.3.2.0.PDCCNXM release-keys
[Orientation] => 6
[DateTime] => 2019:05:26 15:30:57
[YCbCrPositioning] => 1
[Exif_IFD_Pointer] => 255
[ResolutionUnit] => 2
[GPS_IFD_Pointer] => 798
[XResolution] => 72/1
[YResolution] => 72/1
[Make] => Xiaomi
[THUMBNAIL] => Array
(
[JPEGInterchangeFormat] => 969
[Orientation] => 6
[JPEGInterchangeFormatLength] => 12256
[Compression] => 6
[ResolutionUnit] => 2
[XResolution] => 72/1
[YResolution] => 72/1
)
[ISOSpeedRatings] => 400
[ExposureProgram] => 0
[FNumber] => 175/100
[ExposureTime] => 1/50
[UndefinedTag:0x9999] => {"sensor_type":"rear","mirror":false}
[SensingMethod] => 2
[SubSecTimeDigitized] => 732806
[SubSecTimeOriginal] => 732806
[SubSecTime] => 732806
[FocalLength] => 4070/1000
[Flash] => 16
[UndefinedTag:0x8889] => HHT
[MeteringMode] => 2
[SceneCaptureType] => 0
[InteroperabilityOffset] => 768
[FocalLengthIn35mmFilm] => 4
[DateTimeDigitized] => 2019:05:26 15:30:57
[ExifImageLength] => 3000
[WhiteBalance] => 0
[DateTimeOriginal] => 2019:05:26 15:30:57
[BrightnessValue] => -97/100
[ExifImageWidth] => 4000
[ExposureMode] => 0
[ApertureValue] => 161/100
[ComponentsConfiguration] =>
[ColorSpace] => 1
[SceneType] =>
[ShutterSpeedValue] => 5643/1000
[ExifVersion] => 0220
[FlashPixVersion] => 0100
[GPSTimeStamp] => Array
(
[0] => 7/1
[1] => 30/1
[2] => 56/1
)
[GPSDateStamp] => 2019:05:26
[InterOperabilityIndex] => R98
[InterOperabilityVersion] => 0100
)
<?php
//获取图片的旋转角度
$rotate = 0;
$orientation = 1;
if (function_exists('exif_read_data')) {
try { //这里使用try catch主要是解决iphone手机不支持这个方法
$exif = exif_read_data($tep_name);
} catch (Exception $exp) {
$exif = false;
}
if ($exif && isset($exif['Orientation'])){
$orientation = $exif['Orientation'];
}
} else if (preg_match('@\x12\x01\x03\x00\x01\x00\x00\x00(.)\x00\x00\x00@', file_get_contents($tep_name), $matches)) {
$orientation = ord($matches[1]);
}
//获取角度
switch($orientation) {
case 1:
$rotate = 0;
break;
case 8:
$rotate = -90;
break;
case 3:
$rotate = 0;
break;
case 6:
$rotate = 90;
break;
}
?>
问题解决方案参考:https://www.e-learn.cn/content/wangluowenzhang/717898
PHP官方给出的参考:https://www.php.net/manual/zh/function.exif-read-data.php
参考文档:https://www.cnblogs.com/csonezp/p/5564809.html
文明上网理性发言!