All Products
Search
Document Center

Content Moderation:Image OCR

Last Updated:Dec 29, 2025

This topic describes how to use the PHP SDK for OCR to detect text in images.

Features

In addition to regular images, general-purpose OCR can recognize text on structured cards and certificates. For more information about the parameters, see Image OCR detection API.

Note
  • This SDK supports only image URLs. It does not support local files or binary data.

  • Supported URL types: Public HTTP/HTTPS URLs that are up to 2,048 characters in length.

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 a synchronous image detection task

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: Reuse the instantiated client as much as possible to improve detection performance. This avoids repeated connections.
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of a Resource Access Management (RAM) user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of a RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('Obtain the AccessKey ID of a RAM user from an environment variable', 'Obtain the AccessKey secret of a RAM user from an environment variable')
        ->timeout(10) // The timeout period is 10 seconds. This setting applies to all requests that are sent using this client and do not have a timeout period specified.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is milliseconds. This setting applies to all requests that are sent using this client and do not have a connection timeout period specified.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $task1 = array('dataId' => 'Business data ID',
        'url' => 'URL of the image to detect'
    );
    // Example: Recognize the front of an ID card.
    $extras = array('card' => 'id-card-front');
    /* Set the images to detect. One image corresponds to one detection task.
     * If you detect multiple images at the same time, the processing time is determined by the time when the last image is processed.
     * Typically, the average response time for batch detection is longer than that for single-image detection. The more images you submit in a batch, the higher the probability that the response time increases.
     * The sample code shows how to detect a single image. To detect multiple images in a batch, create multiple detection tasks.
     * OCR detection is billed based on the number of detected images multiplied by the unit price for the detected card or certificate type.
     */
    $result = Green::v20180509()->imageSyncScan()
        ->timeout(10) // The timeout period is 10 seconds. This request-level timeout setting is valid only for the current request.
        ->connectTimeout(3) // The connection timeout period is 3 seconds. If the value is less than 1, the unit is milliseconds. This request-level timeout setting is valid only for 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;
}