This topic shows you how to use Content Moderation SDK for PHP to call the optical character recognition (OCR) operation and detect text in images.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information about how to install the dependencies, 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

Interface Description Supported region
ImageSyncScanRequest Submits synchronous OCR tasks to recognize text in images. Set the scenes parameter to ocr.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
  • ap-southeast-5: Indonesia (Jakarta)
Sample code
<?php

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

try {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->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 on processing the request starts from when the request is initiated to when the last image is moderated. 
     * Generally, 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 moderation unit price of the card or certificate images. 
     */
    $result = Green::v20180509()->imageSyncScan()
        ->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;
}