美文网首页
百度ocr身份证识别

百度ocr身份证识别

作者: v1i555 | 来源:发表于2017-12-15 11:49 被阅读0次
//扫描
    public function actionSaomiao(){
      
        $base64 =$_POST['base64'];
            ///////////////////////////////
            ///如果不为空
            ///////////////////////////////
            $base64_image = str_replace(' ', '+', $base64);
            //post的数据里面,加号会被替换为空格,需要重新替换回来,如果不是post的数据,则注释掉这一行
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
                //匹配成功
                $name=md5(microtime(true));
                $Ymd=date('Ymd',time());
                ////文件目录
                $dir='upload/'.$Ymd;
                ////拼凑目录
                $image_name =\Yii::getAlias('@webroot').'/'.$dir;
                if (!is_dir($image_name)){
                    //如果不存在就创建该目录
                    mkdir($image_name,0777,true);
                }
                if($result[2] == 'jpeg'){
                    $imginfo='/'.$name.'.jpg';
                }else{
                    $imginfo='/'.$name.".".$result[2];
                }
                $image_file = $image_name.$imginfo;
                ////拼凑路径
                $dir=$dir.$imginfo;
                //服务器文件存储路径

                if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
                    $img=getimagesize($dir);
                    if ($img[0]> 4096 || $img[1] > 4096){
                        if ($img[0] > 4096 ){
                            $newwidth=4095;
                        }else{
                            $newwidth=$img[0];
                        }
                        if ($img[1] >4096){
                            $newheight=4095;
                        }else{
                            $newheight=$img[1];
                        }
                        $thumb=imagecreatetruecolor($newwidth,$newheight);
                        $source = imagecreatefromjpeg($dir);
                        ImageCopyResampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$img[0],$img[1]);
                        switch($img[2]){
                            //支持jpg
                            case  2:
                                imagejpeg($thumb,$dir);
                                break;
                            //支持png
                            case 3:
                                imagejpeg($thumb,$dir);
                                break;
                            default:
                                echo "<script>alert('待开发其他类型');</script>";
                                break;
                        }
                    }
                    $aipOcr =new AipOcr($this->app_id,$this->API_KEY,$this->SECRET_KEY);
                    $isFront = true;
                    $options = array('detect_direction' => "true");
                    $result = $aipOcr->idcard(file_get_contents($dir),$isFront,$options);
                    if (isset($result['error_code'])){
                        if ($result['error_code'] == "SDK109"){
                            $rs=[
                                'success'=>false,
                                'msg'=>'只支持jpg/png/bmp格式'
                            ];
                        }
                        if ($result['error_code'] ==216633){
                         $rs=[
                             'success'=>false,
                             'msg'=>'识别身份证错误,出现此问题的原因一般为:您上传了非身份证图片或您上传的身份证图片不完整'
                         ];
                        }
                        if ($result['error_code'] =="SDK100"){
                         $rs=[
                             'success'=>false,
                            'msg'=>'图片大小超限'
                         ];
                        }
                        if ($result['error_code'] =="SDK101" ){
                            $rs=[
                                'success'=>false,
                                'msg'=>'图片大小超限'
                            ];
                        }
                        header("Access-Control-Allow-Origin:*");
                        return  json_encode($rs,JSON_UNESCAPED_UNICODE);
                    }
                    $name="";
                    $card=0;
                    foreach ($result['words_result'] as $key=>$value){
                        if ($key == "姓名"){
                            $name=$value['words'];
                        }
                        if ($key == "公民身份号码"){
                            $card=$value['words'];
                        }
//                    echo $key.':'.$value['words']."<br/>";
                    }
                    $row['name']=$name;
                    $row['card_id']=$card;
                    $rs=[
                        'success'=>true,
                        'data'=>$row
                    ];
                    header("Access-Control-Allow-Origin:*");
                    return  json_encode($rs,JSON_UNESCAPED_UNICODE);
                }else{
                    $request=[
                        'success'=>false,
                        'filename'=>'上传失败'
                    ];
                    header("Access-Control-Allow-Origin:*");
                    return json_encode($request,JSON_UNESCAPED_UNICODE);
                }
            }else{
                return false;
            }

    }

相关文章

网友评论

      本文标题:百度ocr身份证识别

      本文链接:https://www.haomeiwen.com/subject/gqgiixtx.html