{"id":17922715,"url":"https://github.com/overtrue/laravel-qcloud-content-audit","last_synced_at":"2025-07-02T10:35:07.967Z","repository":{"id":38123271,"uuid":"374554354","full_name":"overtrue/laravel-qcloud-content-audit","owner":"overtrue","description":"腾讯云内容安全（文字图片内容审核）服务","archived":false,"fork":false,"pushed_at":"2024-05-11T07:29:23.000Z","size":103,"stargazers_count":44,"open_issues_count":2,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-20T19:18:12.279Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["overtrue"]}},"created_at":"2021-06-07T06:10:37.000Z","updated_at":"2025-01-15T15:09:05.000Z","dependencies_parsed_at":"2023-11-09T11:03:59.512Z","dependency_job_id":"23521b32-a126-451b-ad1d-30ed727ab7fc","html_url":"https://github.com/overtrue/laravel-qcloud-content-audit","commit_stats":{"total_commits":43,"total_committers":4,"mean_commits":10.75,"dds":0.2790697674418605,"last_synced_commit":"0786d8e54e02132bfeaf51e4d15c53282bdba7f8"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":"overtrue/laravel-package","purl":"pkg:github/overtrue/laravel-qcloud-content-audit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-qcloud-content-audit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-qcloud-content-audit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-qcloud-content-audit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-qcloud-content-audit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-qcloud-content-audit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-qcloud-content-audit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261044433,"owners_count":23101834,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T20:40:34.455Z","updated_at":"2025-07-02T10:35:07.940Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":[],"sub_categories":[],"readme":"# 腾讯云内容安全（文字图片内容审核）服务\n\n---\n\n[![CI](https://github.com/overtrue/Laravel-qcloud-content-audit/actions/workflows/ci.yml/badge.svg)](https://github.com/overtrue/Laravel-qcloud-content-audit/actions/workflows/ci.yml)\n[![Latest Stable Version](https://poser.pugx.org/overtrue/Laravel-qcloud-content-audit/v/stable.svg)](https://packagist.org/packages/overtrue/Laravel-qcloud-content-audit) \n[![Latest Unstable Version](https://poser.pugx.org/overtrue/Laravel-qcloud-content-audit/v/unstable.svg)](https://packagist.org/packages/overtrue/Laravel-qcloud-content-audit) \n[![Total Downloads](https://poser.pugx.org/overtrue/Laravel-qcloud-content-audit/downloads)](https://packagist.org/packages/overtrue/Laravel-qcloud-content-audit) \n[![License](https://poser.pugx.org/overtrue/Laravel-qcloud-content-audit/license)](https://packagist.org/packages/overtrue/Laravel-qcloud-content-audit)\n\nT-Sec 天御内容安全服务使用了深度学习技术，识别文本/图片中出现的可能令人反感、不安全或不适宜内容，支持用户配置词库/图片黑名单，识别自定义的识别类型。\n\n- :book: [TMS 官方 API 文档](https://cloud.tencent.com/product/tms)\n- :book: [IMS 官方 API 文档](https://cloud.tencent.com/product/ims)\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n## Installing\n\n```shell\n$ composer require overtrue/Laravel-qcloud-content-audit -vvv\n```\n\n### Config\n\n请在 `config/services.php` 中配置以下内容：\n\n```php\n    //...\n    // 文字识别服务\n    'tms' =\u003e [\n        'secret_id' =\u003e env('TMS_SECRET_ID'),\n        'secret_key' =\u003e env('TMS_SECRET_KEY'),\n        'endpoint' =\u003e env('TMS_ENDPOINT'),\n        \n        // 可选，默认使用腾讯云默认策略\n        'biz_type' =\u003e env('TMS_BIZ_TYPE'), \n        // 可选，开启后跳过 tms 识别/打码功能\n        'dry' =\u003e env('TMS_DRY', false),\n    ],\n    \n    // 图片审核/识别服务\n    'ims' =\u003e [\n        'secret_id' =\u003e env('IMS_SECRET_ID'),\n        'secret_key' =\u003e env('IMS_SECRET_KEY'),\n        'endpoint' =\u003e env('IMS_ENDPOINT'),\n        \n        // 可选，默认使用腾讯云默认策略\n        'biz_type' =\u003e env('IMS_BIZ_TYPE'),\n        // 可选，开启后跳过 ims 识别功能\n        'dry' =\u003e env('IMS_DRY', false),\n    ],\n```\n\n## API\n\n### 获取检查结果\n\n调用对应 API 返回数组结果，返回值结构请参考官方 API 文档。\n\n#### 文本\n\n\u003e 接口请求频率限制：1000次/秒。\n\n```php\nuse Overtrue\\LaravelQcloudContentAudit\\Tms;\n\narray Tms::check(string $input);\n```\n\n#### 图片\n\n\u003e - 接口请求频率限制：100次/秒。\n\u003e - 图片检测接口为图片文件内容，大小不能超过5M\n\u003e - 图片将会缩放成 300*300 后检查\n\n```php\nuse Overtrue\\LaravelQcloudContentAudit\\Ims;\n\narray Ims::check(string $contents);\n```\n\u003e [!TIP]\n\u003e\n\u003e `$contents` 可以为：图片内容、图片本地路径或 URL。\n\n### 检查并返回是否通过\n\n```php\nuse Overtrue\\LaravelQcloudContentAudit\\Tms;\nuse Overtrue\\LaravelQcloudContentAudit\\Ims;\n\nbool Tms::validate(string $contents, string $strategy = 'strict')\nbool Ims::validate(string $contents, string $strategy = 'strict')\n```\n\n### 直接替换敏感文本内容\n\n直接将检测到的敏感词替换为 `*`：\n\n```php\nuse Overtrue\\LaravelQcloudContentAudit\\Tms;\n\nstring Tms::mask(string $input, string $char = '*', string $strategy = 'strict');\n\n// 示例：\necho Tms::mask('这是敏感内容哦'); \n// \"这是**哦\"\n```\n\n## 在模型中使用\n\n### 文本校验（CheckTextWithTms）\n\n\u003e [!WARNING]\n\u003e\n\u003e 此操作为同步，可能会影响接口性能，谨慎使用\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Overtrue\\LaravelQcloudContentAudit\\Traits\\CheckTextWithTms;\n\nclass Post extends Model \n{\n    // 文本校验\n    use CheckTextWithTms;\n    \n    protected array $tmsCheckable = ['name', 'description'];\n    protected string $tmsCheckStrategy = 'strict'; // 可选，默认使用最严格模式\n    \n    //...\n}\n```\n\n### 文本打码（MaskTextWithTms）\n\n异步检测到敏感内容时替换为 * 号。\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Overtrue\\LaravelQcloudContentAudit\\Traits\\MaskTextWithTms;\n\nclass Post extends Model \n{\n    use MaskTextWithTms;\n    \n    protected $tmsMaskable = ['name', 'description'];\n    // protected $tmsMaskStrategy = 'strict'; // 开启打码的策略情况，可选，默认使用最严格模式\n    \n    //...\n}\n```\n\n\n\u003e [!WARNING]\n\u003e \n\u003e 此行为为异步，默认监听模型 `saved` 事件，触发 `MaskModelAttributes::dispatch($model)`，如需禁用此行为，可如下设置：\n\u003e ```php\n\u003e protected bool $tmsMaskOnSaved = false;\n\u003e ```\n\n## 使用表单校验规则\n\n您可以在表单验证时使用 `tms` 或者 `tms:{strategy}` 模式来进行表单验证：\n\n```php\n$this-\u003evalidate($request, [\n\t'name' =\u003e 'required|tms',   // 使用默认 strict 策略\n\t'avatar' =\u003e 'required|url|ims', // 使用默认 strict 策略\n\t'description' =\u003e 'required|tms:strict', // 使用指定策略\n\t'logo_url' =\u003e 'required|url|ims:logo',  // 使用指定策略\n]);\n```\n\n## 配置策略\n\n你可以通过以下方式注册一个或多个自定义校验规则，决定是否通过校验：\n\n```php\n// 文字\nTms::setStrategy('strict', function($result) {\n\treturn $result['Suggestion'] === 'Pass';\n});\n\n// 图片\nIms::setStrategy('logo', function($result) {\n\treturn $result['Suggestion'] === 'Pass';\n});\n```\n\n\u003e [!TIP]\n\u003e \n\u003e- 接口返回值中 Suggestion 有三种返回值：Block：建议屏蔽，Review ：建议人工复审，Pass：建议通过，\n\u003e- 另外还有一个 Score，该字段用于返回当前标签（Label）下的置信度，取值范围：0（置信度最低）-100（置信度最高 ），越高代表文本越有可能属于当前返回的标签；如：色情 99，则表明该文本非常有可能属于色情内容；色情 0，则表明该文本不属于色情内容\n\u003e\n\n### Events\n\n当文字被检测敏感并打码的时候，将会触发事件：\n\n`Overtrue\\LaravelQcloudContentAudit\\Events\\ModelAttributeTextMasked`\n\n你可以监听该事件，以获取检测结果：\n\n- `$event-\u003eorigin` 检测前的原始内容，如 `这是敏感内容`\n- `$event-\u003eresult`  打码后的结果，如 `这是**内容`\n- `$event-\u003emodel`   模型对象\n- `$event-\u003eattribute` 模型属性名，如 `name`\n\n## 异常处理\n\n验证失败将抛出以下异常：\n\n- `Overtrue\\LaravelQcloudContentAudit\\InvalidTextException`\n    - `$contents` - (string) 被检测的文本内容\n    - `$response` - (array) API 原始返回值\n- `Overtrue\\LaravelQcloudContentAudit\\InvalidImageException`\n    - `$response` - (array) API 原始返回值\n\n## :heart: Sponsor me \n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n\n## Contributing\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-package/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-package/issues).\n3. Contribute new features or update the wiki.\n\n_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any\nnew code contributions must be accompanied by unit tests where applicable._\n\n## PHP 扩展包开发\n\n\u003e 想知道如何从零开始构建 PHP 扩展包？\n\u003e\n\u003e 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-qcloud-content-audit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-qcloud-content-audit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-qcloud-content-audit/lists"}