thinkphp5 上传图片 iPhone 设备 图片旋转问题处理

前台代码

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>test</title>
    </head>
    <body>
        <form action="/index/index/upfile" method="post" accept-charset="utf-8" enctype="multipart/form-data">
            
            <input type="file" name="up1" id="up1" value="" />
            
            <button type="submit">上传</button>
            
        </form>
    </body>
</html>

后台代码

   

    //获取文件类型 - 扩展名
    private function getType($filename){
        $temp_arr = explode(".", $filename);
        $file_ext = array_pop($temp_arr);
        $file_ext = trim($file_ext);
        $file_ext = strtolower($file_ext);
        return $file_ext;
    }
    
    //获取上传设备信息 - 如果非iphone,ipad苹果设备 返回 0 
    private function getFileVer($user_agent){
        $ua = strtolower($user_agent);
        if(strpos($ua,"iphone") || strpos($ua,"ipad") || strpos($ua,"ipod") || strpos($ua,"ios")){
            $verArr = explode('cpu iphone os',$ua);
            $verArr2 = explode('like mac os',$verArr[1]);
            $ver = trim($verArr2[0]);
            $ver = str_replace("_","",$ver);
            $ver = intval($ver);
            if(strlen($ver)<7) {
                $ver = $ver*pow(10,(7-strlen($ver)));
            }
            return $ver;
        }else{
            return 0;
        }
    }
    
    //图片格式转换
private function transform_image($image_path, $to_ext = 'png', $save_path = null)
{
    if (! in_array($to_ext, ['png', 'gif', 'jpeg', 'wbmp', 'webp', 'xbm'])) {
        throw new \Exception('unsupport transform image to ' . $to_ext);
    }
    switch (exif_imagetype($image_path)) {
        case IMAGETYPE_GIF :
            $img = imagecreatefromgif($image_path);
            break;
        case IMAGETYPE_JPEG :
        case IMAGETYPE_JPEG2000:
            $img = imagecreatefromjpeg($image_path);
            break;
        case IMAGETYPE_PNG:
            $img = imagecreatefrompng($image_path);
            break;
        case IMAGETYPE_BMP:
        case IMAGETYPE_WBMP:
            $img = imagecreatefromwbmp($image_path);
            break;
        case IMAGETYPE_XBM:
            $img = imagecreatefromxbm($image_path);
            break;
        case IMAGETYPE_WEBP: //(从 PHP 7.1.0 开始支持)
            $img = imagecreatefromwebp($image_path);
            break;
        default :
            throw new \Exception('Invalid image type');
    }
    $function = 'image'.$to_ext;
    if ($save_path) {
        return $function($img, $save_path);
    } else {
        $tmp = __DIR__.'/'.uniqid().'.'.$to_ext;
        if ($function($img, $tmp)) {
            $content = file_get_contents($tmp);
            unlink($tmp);
            return $content;
        } else {
            unlink($tmp);
            throw new \Exception('the file '.$tmp.' can not write');
        }
    }
}
    
 /**
 * 解决iPhone图片旋转问题
 * $src 图片绝对路径
 * $ver 手机版本号
 * **/
private function rotateImage($filename,$ver){
    $type = $this->getType($filename);
    if($type == 'png' || $type == 'PNG') {
        $this->transform_image($filename,'jpeg',$filename);
    }
    $exif = exif_read_data($filename);
    if(!empty($exif['Orientation']) && $ver>0) {
        $orientation = $exif['Orientation'];
        if($exif['MimeType'] == 'image/gif'){
            $image = imagecreatefromgif($filename);
        }else if($exif['MimeType'] == 'image/png'){
            $image = imagecreatefrompng($filename);
        }else{
            $image = imagecreatefromjpeg($filename);
        }
        if($ver < 1350000) {
            switch ($orientation) {
                case 8;
                    $image = imagerotate($image, 270, 0);
                    break;
                case 3:
                    $image = imagerotate($image, 0, 0);
                    break;
                case 6:
                    $image = imagerotate($image, 0, 0);
                    break;
            }
        }else if($ver >= 1350000 && $ver <1400000){
            switch ($orientation) {
                case 8;
                    $image = imagerotate($image, 0, 0);
                    break;
                case 3:
                    $image = imagerotate($image, 0, 0);
                    break;
                case 6:
                    $image = imagerotate($image, 0, 0);
                    break;
                case 1:
                    $image = imagerotate($image, 0, 0);
                    break;
            }
        }else if($ver >= 1400000 && $ver < 1500000){
                switch ($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;
                    case 1:
                        $image = imagerotate($image, 0, 0);
                        break;
                }
        }else{
            switch ($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;
            }
        }

        if($exif['MimeType'] == 'image/gif'){
            imagegif($image,$filename);
        }else if($exif['MimeType'] == 'image/png'){
            imagepng($image,$filename);
        }else{
            imagejpeg($image,$filename);
        }
        imagedestroy($image);
    }
}