先创建一个html,并以json格式传输到php文件
身份证识别
再创建个upload.php
<?php header("Content-Type: text/html; charset=UTF-8"); /** * base64图片上传 * @param $base64_img * @return array */ $base64_img = trim($_POST['img']); $up_dir = 'upload/';//存放在当前目录的upload文件夹下 $fi_dir = 'ok_upload/';//存放在当前目录的upload文件夹下 if(!file_exists($up_dir)){ mkdir($up_dir,0777); } if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){ $type = $result[2]; if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){ $new_file = $up_dir.date('YmdHis_').'.'.$type; if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){ $img_path = str_replace('../../..', '', $new_file); $path = 'upload/'; $data = file_get_contents($img_path); $base64 = base64_encode($data); $appkey = 'LzJu1grfwH6UaDX2'; $params = array( 'app_id' => '1106920947', 'image' => $base64, 'card_type' => '0', 'time_stamp' => strval(time()), 'nonce_str' => strval(rand()), 'sign' => '', ); $params['sign'] = getReqSign($params, $appkey); // 执行API调用 $url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_idcardocr';//身份证识别OCR $response = doHttpPost($url, $params); echo $response;die; $arr = json_decode($response,true); $photo = base64_decode($arr['data']['image']); if(!file_exists($fi_dir)){ mkdir($fi_dir,0777); } $type = 'jpg'; if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){ $new_file = $fi_dir.date('YmdHis_').'.'.$type; if(file_put_contents($new_file, str_replace($result[1], '', $photo))){ $img_paths = str_replace('../../..', '', $new_file); echo '图片处理成功
![]('%20.$img_paths.%20')
'; }else{ echo '图片处理失败'; } } }else{ echo '图片上传失败'; } }else{ //文件类型错误 echo '图片上传类型错误'; } }else{ //文件错误 echo '文件错误'; } // getReqSign :根据 接口请求参数 和 应用密钥 计算 请求签名 // 参数说明 // - $params:接口请求参数(特别注意:不同的接口,参数对一般不一样,请以具体接口要求为准) // - $appkey:应用密钥 // 返回数据 // - 签名结果 function getReqSign($params /* 关联数组 */, $appkey /* 字符串*/) { // 1. 字典升序排序 ksort($params); // 2. 拼按URL键值对 $str = ''; foreach ($params as $key => $value) { if ($value !== '') { $str .= $key . '=' . urlencode($value) . '&'; } } // 3. 拼接app_key $str .= 'app_key=' . $appkey; // 4. MD5运算+转换大写,得到请求签名 $sign = strtoupper(md5($str)); return $sign; } // doHttpPost :执行POST请求,并取回响应结果 // 参数说明 // - $url :接口请求地址 // - $params:完整接口请求参数(特别注意:不同的接口,参数对一般不一样,请以具体接口要求为准) // 返回数据 // - 返回false表示失败,否则表示API成功返回的HTTP BODY部分 function doHttpPost($url, $params) { $curl = curl_init(); $response = false; do { // 1. 设置HTTP URL (API地址) curl_setopt($curl, CURLOPT_URL, $url); // 2. 设置HTTP HEADER (表单POST) $head = array( 'Content-Type: application/x-www-form-urlencoded' ); curl_setopt($curl, CURLOPT_HTTPHEADER, $head); // 3. 设置HTTP BODY (URL键值对) $body = http_build_query($params); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $body); // 4. 调用API,获取响应结果 curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); if ($response === false) { $response = false; break; } $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($code != 200) { $response = false; break; } } while (0); curl_close($curl); return $response; } 这样就能识别身份证上的信息了 ———————————————— 版权声明:本文为CSDN博主「香蕉你个巴拉啊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_32551929/article/details/80844326