Files
2023-05-06 14:33:46 +08:00

295 lines
11 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Desc: 腾讯云图片识别
* Date: 2021-07-20
* Time: 16:49
*/
defined('BASEPATH') OR exit('No direct script access allowed');
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Ocr\V20181119\OcrClient;
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
use TencentCloud\Ocr\V20181119\Models\BizLicenseOCRRequest;
use TencentCloud\Ocr\V20181119\Models\CarInvoiceOCRRequest;
use TencentCloud\Ocr\V20181119\Models\VehicleLicenseOCRRequest;
use TencentCloud\Ocr\V20181119\Models\VatInvoiceOCRRequest;
class TcOrc{
private $accessKeyId;
private $accessKeySecret;
private $ci;
private $dir;
private $log_file = 'error.log';
private $is_dev = false;
public function __construct($accessKeyId='',$accessKeySecret=''){
if($accessKeyId && $accessKeySecret){
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
}else{
$this->accessKeyId = 'AKIDMXS7WhbEdlcCWZMs75mDNHuIPHsCF2Yn';
$this->accessKeySecret = 'zh8JDpAF3bOvCCvNYS5RXLs87pGQKxJO';
}
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) {//dev
$this->is_dev = true;
}
$this->ci = & get_instance();
$this->dir = 'tcorc';
}
/**
* 身份证识别
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/33524
* @param $imageUrl string 身份证图片地址
* @param $CardSide string 省份证正反面 (FRONT 正面 BACK 反面)
* @param return array 返回值参考官方文档
*/
public function IdentityCard($imageUrl,$CardSide='FRONT'){
if($this->is_dev){
$this->ci->load->library('mycurl');
$debug_url = 'http://104.194.86.23:8889/index.php';
$params = [
'ImageUrl' => $imageUrl,
'CardSide' => $CardSide
];
$result = $this->ci->mycurl->httpGet($debug_url,$params);
$result = json_decode($result,true);
return $result;
}
try {
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
$req = new IDCardOCRRequest();
$params = array(
"ImageUrl" => $imageUrl,
"CardSide" => $CardSide
);
$req->fromJsonString(json_encode($params));
$resp = $client->IDCardOCR($req);
debug_log("function:".__FUNCTION__.$resp->toJsonString(),'success.log',$this->dir);
$result = json_decode($resp->toJsonString(),true);
if($result['Error']){
return ['code'=>0,'msg'=>$result['Error']['Message']];
}else{
return ['code'=>1,'msg'=>'识别成功','data'=>$result];
}
}
catch(TencentCloudSDKException $e) {
debug_log($e,$this->log_file,$this->dir);
return ['code'=>0,'msg'=>'识别失败'];
}
}
/**
* 营业执照
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/36215
* @param $imageUrl string 营业执照图片url
* @param return array 返回值参考官方文档
*/
public function BizLicense($imageUrl){
//dev测试
if($this->is_dev){
$this->ci->load->library('mycurl');
$debug_url = 'http://104.194.86.23:8889/biz.php';
$params = [
'ImageUrl' => $imageUrl,
];
$result = $this->ci->mycurl->httpGet($debug_url,$params);
$result = json_decode($result,true);
return $result;
}
try {
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
$req = new BizLicenseOCRRequest();
$params = array(
"ImageUrl" => $imageUrl,
);
$req->fromJsonString(json_encode($params));
$resp = $client->BizLicenseOCR($req);
debug_log("function:".__FUNCTION__.$resp->toJsonString(),'success.log',$this->dir);
$result = json_decode($resp->toJsonString(),true);
if($result['Error']){
return ['code'=>0,'msg'=>$result['Error']['Message']];
}else{
return ['code'=>1,'msg'=>'识别成功','data'=>$result];
}
}
catch(TencentCloudSDKException $e) {
debug_log($e,$this->log_file,$this->dir);
return ['code'=>0,'msg'=>'识别失败'];
}
}
/**
* 购车发票识别
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/37076
* @param $imageUrl string 营业执照图片url
* @param return array 返回值参考官方文档
*/
public function CarInvoiceInfos($imageUrl){
//dev测试
if($this->is_dev){
$this->ci->load->library('mycurl');
$debug_url = 'http://104.194.86.23:8889/infos.php';
$params = [
'ImageUrl' => $imageUrl,
];
$result = $this->ci->mycurl->httpGet($debug_url,$params);
$result = json_decode($result,true);
return $result;
}
try {
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
$req = new CarInvoiceOCRRequest();
$params = array(
"ImageUrl" => $imageUrl,
);
$req->fromJsonString(json_encode($params));
$resp = $client->CarInvoiceOCR($req);
debug_log("function:".__FUNCTION__.$resp->toJsonString(),'success.log',$this->dir);
$result = json_decode($resp->toJsonString(),true);
if($result['Error']){
return ['code'=>0,'msg'=>$result['Error']['Message']];
}else{
return ['code'=>1,'msg'=>'图片识别成功','data'=>$result];
}
}
catch(TencentCloudSDKException $e) {
debug_log($e,$this->log_file,$this->dir);
return ['code'=>0,'msg'=>'图片识别失败'];
}
}
/**
* 行驶证识别
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/36209
* @param $imageUrl string 行驶证照图片url
* @param return array 返回值参考官方文档
*/
public function VehicleLicense($imageUrl){
//dev测试
if($this->is_dev){
$this->ci->load->library('mycurl');
$debug_url = 'http://104.194.86.23:8889/veh.php';
$params = [
'ImageUrl' => $imageUrl,
];
$result = $this->ci->mycurl->httpGet($debug_url,$params);
$result = json_decode($result,true);
return $result;
}
try {
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
$req = new VehicleLicenseOCRRequest();
$params = array(
"ImageUrl" => $imageUrl,
);
$req->fromJsonString(json_encode($params));
$resp = $client->VehicleLicenseOCR($req);
debug_log("function:".__FUNCTION__.$resp->toJsonString(),'success.log',$this->dir);
$result = json_decode($resp->toJsonString(),true);
if($result['Error']){
return ['code'=>0,'msg'=>$result['Error']['Message']];
}else{
return ['code'=>1,'msg'=>'图片识别成功','data'=>$result];
}
}
catch(TencentCloudSDKException $e) {
debug_log($e,$this->log_file,$this->dir);
return ['code'=>0,'msg'=>'图片识别失败'];
}
}
/**
* 增值税发票识别
* Desc: 腾讯官方地址:https://cloud.tencent.com/document/product/866/36210
* @param $imageUrl string 增值税发票图片url
* @param return array 返回值参考官方文档
*/
public function VatInvoice($imageUrl){
//dev测试
if($this->is_dev){
$this->ci->load->library('mycurl');
$debug_url = 'http://104.194.86.23:8889/vinvoice.php';
$params = [
'ImageUrl' => $imageUrl,
];
$result = $this->ci->mycurl->httpGet($debug_url,$params);
$result = json_decode($result,true);
return $result;
}
try {
$cred = new Credential($this->accessKeyId, $this->accessKeySecret);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-guangzhou", $clientProfile);
$req = new VatInvoiceOCRRequest();
$params = array(
"ImageUrl" => $imageUrl,
);
$req->fromJsonString(json_encode($params));
$resp = $client->VatInvoiceOCR($req);
debug_log("function:".__FUNCTION__.$resp->toJsonString(),'success.log',$this->dir);
$result = json_decode($resp->toJsonString(),true);
if($result['Error']){
return ['code'=>0,'msg'=>$result['Error']['Message']];
}else{
return ['code'=>1,'msg'=>'图片识别成功','data'=>$result];
}
}
catch(TencentCloudSDKException $e) {
debug_log($e,$this->log_file,$this->dir);
return ['code'=>0,'msg'=>'图片识别失败'];
}
}
}