Files
spacestation/common/libraries/TcOrc.php
T
小鱼开发 3f30c5bcb2 fixed
2024-06-18 01:25:24 +08:00

243 lines
9.0 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 $log_success_file = 'success.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';
}
$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')
{
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(), $this->log_success_file, $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)
{
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(), $this->log_success_file, $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)
{
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("img:" . $imageUrl, 'success.log', $this->dir);
debug_log("function:" . __FUNCTION__ . $resp->toJsonString(), $this->log_success_file, $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->getMessage(), $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)
{
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)
{
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' => '图片识别失败'];
}
}
}