移动端上传图片iphone图片旋转以及服务端处理方法

判断是否需要旋转

    /**
             *iphone判断图片方向,是否需要旋转图片
             */
            if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')&&in_array($ext, array('jpg','png','gif','jpeg','jepg'))){
                $exif =exif_read_data($tmp);
                switch ($exif['Orientation']){
                    case 6:
                        self::imgturn($tmp,1,$ext);
                        break;
                    case 3:
                        self::imgturn($tmp,1,$ext);
                        self::imgturn($tmp,1,$ext);
                        break;
                    case 8:
                        self::imgturn($tmp,2,$ext);
                        break;
                }
            }

 

 

一下是php旋转方法

function imgturn($src, $direction = 1,$ext) {
        switch ($ext) {
            case 'gif' :
                $img = imagecreatefromgif ( $src );
                break;
            case 'jepg':
            case 'jpg' :
            case 'jpeg' :
                $img = imagecreatefromjpeg ( $src );
                break;
            case 'png' :
                $img = imagecreatefrompng ( $src );
                break;
            default :
                die ( '图片格式错误!' );
                break;
        }
        $width = imagesx ( $img );
        $height = imagesy ( $img );
        $img2 = imagecreatetruecolor ( $height, $width );
        // 顺时针旋转90度
        if ($direction == 1) {
            for($x = 0; $x < $width; $x ++) {
                for($y = 0; $y < $height; $y ++) {
                    imagecopy ( $img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1 );
                }
            }
        } else if ($direction == 2) {
            // 逆时针旋转90度
            for($x = 0; $x < $height; $x ++) {
                for($y = 0; $y < $width; $y ++) {
                    imagecopy ( $img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1 );
                }
            }
        }
        switch ($ext) {
            case 'jepg':
            case 'jpg' :
            case "jpeg" :
                imagejpeg ( $img2, $src, 100 );
                break;
            
            case "gif" :
                imagegif ( $img2, $src, 100 );
                break;
            
            case "png" :
                imagepng ( $img2, $src, 100 );
                break;
            
            default :
                die ( '图片格式错误!' );
                break;
        }
        imagedestroy ( $img );
        imagedestroy ( $img2 );
    }

 

转载于:https://www.cnblogs.com/sss-justdDoIt/p/6259339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值