TencentHandler.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Handlers;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\Log;
  6. use TencentCloud\Common\Credential;
  7. use TencentCloud\Common\Profile\ClientProfile;
  8. use TencentCloud\Common\Profile\HttpProfile;
  9. use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
  10. use TencentCloud\Ocr\V20181119\OcrClient;
  11. class TencentHandler {
  12. protected static $secretId;
  13. protected static $secretKey;
  14. //初始化
  15. public function __construct(){
  16. self::$secretId=env('TENCENT_SECRETID');
  17. self::$secretKey=env('TENCENT_SECRETKEY');
  18. }
  19. //获取证件信息
  20. public function getCertInfo($img)
  21. {
  22. $cred = new Credential(env('TENCENT_SECRETID'), env('TENCENT_SECRETKEY'));
  23. $httpProfile = new HttpProfile();
  24. $httpProfile->setEndpoint("ocr.tencentcloudapi.com");
  25. $clientProfile = new ClientProfile();
  26. $clientProfile->setHttpProfile($httpProfile);
  27. $client = new OcrClient($cred, "ap-beijing", $clientProfile);
  28. $req = new IDCardOCRRequest();
  29. $configs=[
  30. "CopyWarn"=>true,
  31. "BorderCheckWarn"=>true,
  32. "ReshootWarn"=>true,
  33. "DetectPsWarn"=>true,
  34. "TempIdWarn"=>true,
  35. "InvalidDateWarn"=>true,
  36. "Quality"=>true,
  37. "ReflectWarn"=>true
  38. ];//"CropIdCard"=>true,"CropPortrait"=>true,
  39. $params = [
  40. 'ImageUrl'=>$img,
  41. 'Config' => json_encode($configs)
  42. ];
  43. $req->fromJsonString(json_encode($params));
  44. $resp = $client->IDCardOCR($req);
  45. return $resp->toJsonString();
  46. }
  47. public function AdvancedInfo(){
  48. return [
  49. '-9100'=>'身份证有效日期不合法',
  50. '-9101'=>'身份证边框不完整',
  51. '-9105'=>'身份证框内遮挡',
  52. '-9107'=>'身份证反光'
  53. ];
  54. }
  55. //活体人脸核身
  56. public function checkFaceId($idCard,$name,$url)
  57. {
  58. $cred = new Credential(env('TENCENT_SECRETID'), env('TENCENT_SECRETKEY'));
  59. $httpProfile = new HttpProfile();
  60. $httpProfile->setEndpoint("faceid.tencentcloudapi.com");
  61. $clientProfile = new ClientProfile();
  62. $clientProfile->setHttpProfile($httpProfile);
  63. $client = new OcrClient($cred, "ap-beijing", $clientProfile);
  64. $req = new IDCardOCRRequest();
  65. $params = [
  66. 'IdCard'=>$idCard,
  67. 'Name'=>$name,
  68. 'ImageBase64'=>$this->getBase64Encode($url),
  69. // 'LivenessType'=>'SILENT',//LIP为数字模式,ACTION为动作模式,SILENT为静默模式
  70. ];
  71. $req->fromJsonString(json_encode($params));
  72. $resp = $client->IDCardOCR($req);
  73. return $resp->toJsonString();
  74. }
  75. //获取base64
  76. protected function getBase64Encode($url)
  77. {
  78. return base64_encode(file_get_contents($url));
  79. }
  80. }