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'=>'失败'];
        }

    }
发布了7 篇原创文章 · 获赞 0 · 访问量 3473

猜你喜欢

转载自blog.csdn.net/weixin_42572968/article/details/105230131