All Products
Search
Document Center

Content Moderation:OCR

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for PHP to perform optical character recognition (OCR). This way, you can recognize text in images.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information, see Installation.

Note

You must use the required PHP version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit synchronous OCR tasks

Operation

Description

Supported region

ImageSyncScanRequest

Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images.

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('We recommend that you obtain the AccessKey ID of your RAM user from environment variables', 'We recommend that you obtain the AccessKey secret of your RAM user from environment variables')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings. 
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings. 
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'ID of the image to be moderated',
        'url' => 'URL of the image to be moderated'
    );
    // Example: If you moderate the image of the front side of an ID card, specify id-card-front. 
    $extras = array('card' => 'id-card-front');
    /* Create one task for each image to be moderated. 
     * If you moderate multiple images in a request, the total response time that the server spends processing the request begins when the request is initiated and ends upon moderation of the last image. 
     * In most cases, the average response time of moderating multiple images in a request is longer than that of moderating a single image. The more images you submit at a time, the higher the probability that the average response time is extended. 
     * In this example, a single image is moderated. If you want to moderate multiple images at a time, create a task for each image to be moderated. 
     * The OCR expense equals the product of the number of card or certificate images moderated and the price for moderating each card or certificate image. 
     */
    $result = Green::v20180509()->imageSyncScan()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request. 
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request. 
        ->body(json_encode(array('tasks' => array($task1), 'scenes' => array('ocr'), 'extras' => array($extras))))
        ->request();
    print_r($result->toArray());

} catch (Exception $exception) {
    echo $exception->getMessage() . PHP_EOL;
}