搜尋
首頁後端開發php教程php身份證識別ORC的方法實現

php身份證識別ORC的方法實現

Jul 05, 2018 pm 04:20 PM
身分證識別

這篇文章主要介紹了關於php身分證識別ORC的方法實現,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

先創建一個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) === &#39;undefined&#39; ){
            result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!"; 
            input.setAttribute(&#39;disabled&#39;,&#39;disabled&#39;); 
        }else{
            input.addEventListener(&#39;change&#39;,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 = &#39;<p class="sitetip">图片img标签展示:</p><img src="/static/imghwm/default1.png"  data-src="&#39;+this.result+&#39;"  class="lazy"   alt=""/>&#39;; 
        }
    } 
</script> 
</head>


<body> 
    <form action="upload.php" method="post">
    <input type="file" value="sdgsdg" id="demo_input" /> 
    <textarea style=&#39;display: none;&#39; name="img" id="result" rows=30 cols=300></textarea> 
    <p id="img_area"></p> 
    <input type="submit" value="提交">
</form>
</body> 
</html>

再建立個upload.php

<?php
header("Content-Type: text/html; charset=UTF-8");
/**
 * base64图片上传
 * @param $base64_img
 * @return array
 */
$base64_img = trim($_POST[&#39;img&#39;]);
$up_dir = &#39;upload/&#39;;//存放在当前目录的upload文件夹下
$fi_dir = &#39;ok_upload/&#39;;//存放在当前目录的upload文件夹下
if(!file_exists($up_dir)){
    mkdir($up_dir,0777);
}
if(preg_match(&#39;/^(data:\s*image\/(\w+);base64,)/&#39;, $base64_img, $result)){
    $type = $result[2];
    if(in_array($type,array(&#39;pjpeg&#39;,&#39;jpeg&#39;,&#39;jpg&#39;,&#39;gif&#39;,&#39;bmp&#39;,&#39;png&#39;))){
        $new_file = $up_dir.date(&#39;YmdHis_&#39;).&#39;.&#39;.$type;
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], &#39;&#39;, $base64_img)))){
 $img_path = str_replace(&#39;../../..&#39;, &#39;&#39;, $new_file);
$path   = &#39;upload/&#39;;
$data   = file_get_contents($img_path);
$base64 = base64_encode($data);
$appkey = &#39;LzJu1grfwH6UaDX2&#39;;
$params = array(
    &#39;app_id&#39;     => &#39;1106920947&#39;,
                &#39;image&#39;      => $base64,
                &#39;card_type&#39;  => &#39;0&#39;,
                &#39;time_stamp&#39; => strval(time()),
                &#39;nonce_str&#39;  => strval(rand()),
                &#39;sign&#39;       => &#39;&#39;,
);
$params[&#39;sign&#39;] = getReqSign($params, $appkey);
// 执行API调用
$url = &#39;https://api.ai.qq.com/fcgi-bin/ocr/ocr_idcardocr&#39;;//身份证识别OCR
$response = doHttpPost($url, $params);
            echo $response;die;
$arr = json_decode($response,true);
$photo = base64_decode($arr[&#39;data&#39;][&#39;image&#39;]);
if(!file_exists($fi_dir)){
    mkdir($fi_dir,0777);
}
$type = &#39;jpg&#39;;
    if(in_array($type,array(&#39;pjpeg&#39;,&#39;jpeg&#39;,&#39;jpg&#39;,&#39;gif&#39;,&#39;bmp&#39;,&#39;png&#39;))){
        $new_file = $fi_dir.date(&#39;YmdHis_&#39;).&#39;.&#39;.$type;
        if(file_put_contents($new_file, str_replace($result[1], &#39;&#39;, $photo))){
            $img_paths = str_replace(&#39;../../..&#39;, &#39;&#39;, $new_file);
            echo &#39;图片处理成功</br><img  src="/static/imghwm/default1.png"  data-src="&#39; .$img_paths. &#39;"  class="lazy"   alt="php身份證識別ORC的方法實現" >&#39;;
        }else{
            echo &#39;图片处理失败</br>&#39;;
        }
    }
        }else{
                    echo &#39;图片上传失败</br>&#39;;
        }
    }else{
        //文件类型错误
    echo &#39;图片上传类型错误&#39;;
    }
}else{
    //文件错误
    echo &#39;文件错误&#39;;
}
// getReqSign :根据 接口请求参数 和 应用密钥 计算 请求签名
// 参数说明
//   - $params:接口请求参数(特别注意:不同的接口,参数对一般不一样,请以具体接口要求为准)
//   - $appkey:应用密钥
// 返回数据
//   - 签名结果
function getReqSign($params /* 关联数组 */, $appkey /* 字符串*/)
{
    // 1. 字典升序排序
    ksort($params);
    // 2. 拼按URL键值对
    $str = &#39;&#39;;
    foreach ($params as $key => $value)
    {
        if ($value !== &#39;&#39;)
        {
            $str .= $key . &#39;=&#39; . urlencode($value) . &#39;&&#39;;
        }
    }
    // 3. 拼接app_key
    $str .= &#39;app_key=&#39; . $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(
            &#39;Content-Type: application/x-www-form-urlencoded&#39;
        );
        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;
}

這樣就能辨識身分證上的資訊了

以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!

相關推薦:

php 透過html-table形式完成excel下載的功能實作

##PHP的Reflection反射機制的介紹

以上是php身份證識別ORC的方法實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

為會話cookie設置httponly標誌的重要性是什麼?為會話cookie設置httponly標誌的重要性是什麼?May 03, 2025 am 12:10 AM

設置httponly標誌對會話cookie至關重要,因為它能有效防止XSS攻擊,保護用戶會話信息。具體來說,1)httponly標誌阻止JavaScript訪問cookie,2)在PHP和Flask中可以通過setcookie和make_response設置該標誌,3)儘管不能防範所有攻擊,但應作為整體安全策略的一部分。

PHP會議在網絡開發中解決了什麼問題?PHP會議在網絡開發中解決了什麼問題?May 03, 2025 am 12:02 AM

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他們儲存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器