先創建一個html,并以json格式傳輸到php文件
!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html xmlns= http://www.w3.org/1999/xhtml head meta http-equiv= Content-Type content= text/html; charset=utf-8 / title 身份證識別 /title style /style script window.onload = function(){ var input = document.getElementById( demo_input var result= document.getElementById( result var img_area = document.getElementById( img_area if ( typeof(FileReader) === undefined ){ result.innerHTML = 抱歉,你的瀏覽器不支持 FileReader,請使用現代瀏覽器操作! input.setAttribute( disabled , disabled }else{ input.addEventListener( change ,readFile,false); function readFile(){ var file = this.files[0]; //這里我們判斷下類型如果不是圖片就返回 去掉就可以上傳任意文件 if(!/image///w+/.test(file.type)){ alert( 請確保文件為圖像類型 return false; var reader = new FileReader(); reader.readAsDataURL(file); console.log(); reader.onload = function(e){ result.innerHTML = this.result; img_area.innerHTML = p >再創建個upload.php
?phpheader( 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 = http://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 圖片處理成功 /br img src= .$img_paths. }else{ echo 圖片處理失敗 /br }else{ echo 圖片上傳失敗 /br }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();
curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
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;
return $response;}這樣就能識別身份證上的信息了
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
php 通過html-table形式完成excel下載的功能實現
PHP的Reflection反射機制的介紹
以上就是php身份證識別ORC的方法實現的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答