苹果手机上传图片成横着的了?安卓正常
用ajax异步上传图片,用手机来上传,安卓手机就是显示的正常,但是用苹果手机就出现了问题了,那就是我用苹果手机上传的图片它就显示是横着的。
解决办法:
目测是exif搞的,用下面这个函数处理,需要exif扩展【宝塔安装如下,其他请自行配置】
function removeExif($imgFile) {
if (!function_exists('exif_read_data')) {
return;
}
$img = @imagecreatefromjpeg($imgFile);
if($img === false){
return;
}
$exif = exif_read_data($imgFile);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 8:
$image = imagerotate($img, 90, 0);
break;
case 3:
$image = imagerotate($img, 180, 0);
break;
case 6:
$image = imagerotate($img, -90, 0);
break;
}
}
imagedestroy($img);
if (isset($image)) {
imagejpeg($image, $imgFile);
imagedestroy($image);
}
}
实际使用 tp中
实际使用 原生
<?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
?>