habibalkhabbaz/identity-documents 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

habibalkhabbaz/identity-documents

Composer 安装命令:

composer require habibalkhabbaz/identity-documents

包简介

Package to parse identity documents like passports

README 文档

README

Latest Version on Packagist Total Downloads

Important

This is a fork from 365Werk/identitydocuments, and I am wishing to maintain it and keep it up-to-date, because the original repository wasn't updated for a long time and it doesn't support Laravel >= 10

Package that allows you to handle documents like passports and other documents that contain a Machine Readable Zone (MRZ).

This package allows you to process images of documents to find the MRZ, parse the MRZ, parse the Visual Inspection Zone (VIZ) and also to find and return a crop of the passport picture (using face detection).

Installation

Using Composer

$ composer require habibalkhabbaz/identity-documents

Publish config (optional)

$ php artisan vendor:publish --provider="HabibAlkhabbaz\IdentityDocuments\IdentityDocumentsServiceProvider"

Configuration

Services

The first important thing to know about the package is that you can use any OCR and or Face Detection API that you want. This package is not doing any of those itself.

Google Vision Service

Included with the package is a Google service class that will be loaded for both OCR and Face Detection by default. If you wish to use the Google service, no further configuration is required besides providing your credentials. To do this, make a service account and download the JSON key file. Then convert the JSON to a PHP array so it can be used as a normal Laravel config file. Your config file would have to be called google_key.php, be placed in the config folder and look like this:

return [
    "type" => "service_account",
    "project_id" => "",
    "private_key_id" => "",
    "private_key" => "",
    "client_email" => "",
    "client_id" => "",
    "auth_uri" => "",
    "token_uri" => "",
    "auth_provider_x509_cert_url" => "",
    "client_x509_cert_url" => "",
];

Creating Custom Services

If you want to use any other API for OCR and/or Face Detection, you can make your own service, or take a look at our list of available services not included in the main package (WIP).

Making a service is relatively easy, if you want to make a service that does the OCR, all you have to do is create a class that implements HabibAlkhabbaz\IdentityDocuments\Interfaces\Ocr. Similarly, there is also a HabibAlkhabbaz\IdentityDocuments\Interfaces\FaceDetection interface. To make creating custom services even easier you can use the following command:

$ php artisan id:service <name> <type>

Where name is the ClassName of the service you wish to create, and type is either Ocr, FaceDetection or Both. This will create a new (empty) service for you in your App\Services namespace implementing the Ocr, FaceDetection or both interfaces.

Usage

Basic usage

Create a new Identity Document with a maximum of 2 images (optional) in this example we'll use a POST request that includes 2 images on our example controller.

use Illuminate\Http\Request;
use HabibAlkhabbaz\IdentityDocuments\IdentityDocument;

class ExampleController {
	public function id(Request $request){
		$document = new IdentityDocument($request->front, $request->back);
	}
}

Warning

In this example I use uploaded files, but you can use any files supported by Intervention

There are now a few things we can do with this newly created Identity Document. First of all finding and returning the MRZ:

$mrz = $document->getMrz();

We can then also get a parsed version of the MRZ by using

$parsed = $document->getParsedMrz();

As the MRZ only allows for A-Z and 0-9 characters, anyone with accents in their name would not get a correct first or last name from the MRZ. To (attempt to) find the correct first and last name on the VIZ part of the document, use:

$viz = $document->getViz();

This will return an array containing both the found first and last names as well as a confidence score. The confidence score is a number between 0 and 1 and shows the similarity between the MRZ and VIZ version of the name. Please not that results can differ based on your system's iconv() implementation.

To get the passport picture from the document use:

$face = $document->getFace()

This returns an Intervention\Image\Image

Get all of the above

If you wish to use all of these in a simplified way, you can also use the static all() method, which also expects up to two images as argument. For example:

use Illuminate\Http\Request;
use HabibAlkhabbaz\IdentityDocuments\IdentityDocument;

class ExampleController {
  public function id(Request $request){
  	$response = IdentityDocument::all($request->front, $request->back);
  	return response()->json($response);
  }
}

The all() method returns an array that looks like this:

[
	'type' => 'string', // TD1, TD2, TD3, MRVA, MRVB
	'mrz' => 'string', // Full MRZ
	'parsed' => [], // Array containing parsed MRZ
	'viz' => [], // Array containing parsed VIZ
	'face' => 'string', // Base64 image string
]

As you can see this includes all the above mentioned methods, plus the $document->type variable. The detected face will be returned as a base64 image string, with an image height of 200px.

Merging images

There are a couple of methods that will configure how the Identity Document is handled. First of all there's the mergeBackAndFrontImages() method. This method can be used to reduce the amount of OCR API calls have to be made. Images will be stacked on top of each other when this method is used. Please note that this method would have to be used before the getMrz() method. Example:

use Illuminate\Http\Request;
use HabibAlkhabbaz\IdentityDocuments\IdentityDocument;

class ExampleController {
	public function id(Request $request){
		$document = new IdentityDocument($request->front, $request->back);
		$document->mergeBackAndFrontImages();
		$mrz = $document->getMrz();
	}
}

Warning

Please note that merging images might cause high memory usage, depending on the size of your images

If you wish to use the static all() method and merge the images, publish the package's config file and enable it in there. Note that changing the option in the config will only apply to the all() method. Default config value:

	'merge_images' => false, // bool

Setting an OCR service

If you have made a custom OCR service or are using one different than the default Google service, you can use the setOcrService() method. For example let's say we've creating a new TesseractService using the methods described above, we can use it for OCR like this:

use Illuminate\Http\Request;
use App\Services\TesseractService;
use HabibAlkhabbaz\IdentityDocuments\IdentityDocument;

class ExampleController {
	public function id(Request $request){
		$document = new IdentityDocument($request->front, $request->back);
		$document->setOcrService(TesseractService::class);
		$mrz = $document->getMrz();
	}
}

If you wish to use the all() method, publish the package's config and set the correct service class there.

Setting a Face Detection Service

This can be done in a similar way as the OCR service, using the setFaceDetectionService() method. For example:

use Illuminate\Http\Request;
use App\Services\AmazonFdService;
use HabibAlkhabbaz\IdentityDocuments\IdentityDocument;

class ExampleController {
	public function id(Request $request){
		$document = new IdentityDocument($request->front, $request->back);
		$document->setFaceDetectionService(AmazonFdService::class);
		$mrz = $document->getFace();
	}
}

If you wish to use the all() method, publish the package's config and set the correct service class there.

Other methods

addBackImage() sets the back image of the IdentityDocument. addFrontImage() sets the front image of the IdentityDocument. setMrz() sets the IdentityDcoument MRZ, for if you just wish to use the parsing functionality.

Contributing

Please see contributing.md for details and a todolist.

Credits

License

Please see the license file for more information.

habibalkhabbaz/identity-documents 适用场景与选型建议

habibalkhabbaz/identity-documents 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.19k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「passport」 「mrz」 「IdentityDocuments」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 habibalkhabbaz/identity-documents 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 habibalkhabbaz/identity-documents 我们能提供哪些服务?
定制开发 / 二次开发

基于 habibalkhabbaz/identity-documents 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 42
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-03-02