thinkphp5.0 单图上传解决移动端苹果手机上传图片翻转问题

thinkphp5.0 单图上传解决移动端苹果手机上传图片翻转问题

public function uploadImg(Request $request ){
        $param = $request->param();
        $file = request()->file('imglist');
        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads'. '/' .  date("Ymd");
            // 移动到框架应用根目录/public/uploads/ 目录下
            
            $info = $file->validate(['size'=>config('imgfile.size'),'ext'=>config('imgfile.ext')])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                return ['code'=>404,'msg'=>$error];
            }
            if($info){
                //获取图片的名字
                $imgName = $info->getSaveName();
                //获取图片的路径
                $imgpath = 'static' . DS .'uploads/'.date("Ymd").'/'.$info->getSaveName();
                
               
                $photo=  '/static' . DS . 'uploads'. '/' . date("Ymd") . '/' . $imgName;
                $image = \think\Image::open($imgpath);
                $exif = function_exists('exif_read_data') && @exif_imagetype($imgpath) == 2 ? @exif_read_data($imgpath):NULL;
               
                if(isset($exif) && !empty($exif) && !empty($exif['Orientation'])) {
                    
                    switch ($exif['Orientation']) {
                        case 8:
                            $image->thumb(config('imgfile.width'), config('imgfile.height'))->rotate(90)->save($imgpath);
                            break;
                        case 3:
                            $image->thumb(config('imgfile.width'), config('imgfile.height'))->rotate(180)->save($imgpath);
                            break;
                        case 6:
                            $image->thumb(config('imgfile.width'), config('imgfile.height'))->rotate(90)->save($imgpath);
                            break;
                    }
                    
                }else{
                    
                    $image->thumb(config('imgfile.width'), config('imgfile.height'))->save($imgpath);
                }
                
                
                return ['code'=>1,'msg'=>'成功','photo'=>$photo];
            }else{
                // 上传失败获取错误信息
                
                return ['code'=>404,'msg'=>$file->getError()];
            }
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值