Skip to content

Instantly share code, notes, and snippets.

@symisc
Last active September 28, 2024 23:56
Show Gist options
  • Save symisc/a20bbd0525b899b9821ac8bfee86b6a6 to your computer and use it in GitHub Desktop.
Save symisc/a20bbd0525b899b9821ac8bfee86b6a6 to your computer and use it in GitHub Desktop.
Scan Malaysia ID Card (MyKAD) using the PixLab DOCSCAN API - Extract Face, and all ID Fields - https://ekyc.pixlab.io/
<?php
/*
* Usage sample of the ID card scanner from PixLab. In this sample, we shall scan
* ID Card from Malaysia (MyKAD); therefore we will extract the user's face, date of birth,
* full name, address, and religion if available.
*/
/*
* PixLab PHP Client which is just a single class PHP file without any dependency that you can get from Github
* https://github.com/symisc/pixlab-php
*/
require_once "pixlab.php";
# Scan over 11K ID Documents from over 197 countries using the PixLab DOCSCAN API Endpoint
# documented at: https://ekyc.pixlab.io/docscan
#
# In this example, given a Passport document, extract the passport holder face and convert/parse all Machine Readable Zone
# to textual content ready to be consumed by your application.
#
# PixLab recommend that you connect your AWS S3 bucket via the dashboard at https://console.pixlab.io
# so that any extracted face or MRZ crop is automatically stored on your S3 bucket rather than the PixLab one.
# This feature should give you full control over your analyzed media files.
#
# Refer to the official documentation at: https://ekyc.pixlab.io/docscan for the API reference guide and more code samples.
$idcard_link = 'https://buletinonline.net/v7/wp-content/uploads/2016/06/Mykad-penghuni-puan-Noraini-2.jpg'; /* ID card prototype: Of course, replace with a real government issued id. */
$key = 'PIXLAB_API_KEY'; # Your PixLab API key that you can fetch from https://console.pixlab.io/
/* Process */
$pix = new Pixlab($key);
if( !$pix->get('docscan',[
'img' => $idcard_link, # ID Card Scanned Image
'type' => 'id', # Type of document we are going to scan
'country' => 'my' # Malysia country code
]) ){
echo $pix->get_error_message()."\n";
die;
}
/* Output the scan result */
echo "User Cropped Face: " . $pix->json->face_url . "\n";
#echo "Raw Text: " . $pix->json->full_text . "\n";
echo "Fields:\n";
# At this stage, the face should be extracted and the array populated with the appropriate information.
if( isset($pix->json->country) ) echo "Country: ".$pix->json->fields->country . "\n";
if( isset($pix->json->fields->id) ) echo "ID: ".$pix->json->fields->id . "\n";
if( isset($pix->json->fields->name) ) echo "Name: ".$pix->json->fields->name . "\n";
if( isset($pix->json->fields->address) ) echo "Address: ".$pix->json->fields->address . "\n";
if( isset($pix->json->fields->religion) ) echo "Religion: ".$pix->json->fields->religion . "\n";
if( isset($pix->json->fields->sex) ) echo "Sex: ".$pix->json->fields->sex . "\n";
if( isset($pix->json->fields->race) ) echo "Race: ".$pix->json->fields->race . "\n";
if( isset($pix->json->fields->birth) ) echo "Date of birth: ".$pix->json->fields->birth . "\n";
if( isset($pix->json->fields->birth_country) ) echo "Country of birth: ".$pix->json->fields->birth_country . "\n";
if( isset($pix->json->fields->nationality) ) echo "Nationality: ".$pix->json->nationality . "\n";
?>
@symisc
Copy link
Author

symisc commented Jul 17, 2023

Usage sample of the ID card scanner from PixLab. In this sample, we shall scan ID Card from Malaysia (MyKAD); therefore we will extract the user's face, date of birth, full name, address, and religion if available.

PixLab recommend that you connect your AWS S3 bucket via the dashboard at https://console.pixlab.io/ so that any extracted face is automatically stored on your S3 bucket rather than the PixLab one. This feature should give you full control over your analyzed media files.

https://ekyc.pixlab.io/ for additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment