All Products
Search
Document Center

Content Moderation:Image moderation

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for PHP to moderate images for risky content.

Description

Content Moderation SDK for PHP supports both synchronous and asynchronous image moderation.

  • If you use synchronous image moderation, the moderation results are returned in real time. For more information about the related parameters, see /green/image/scan.

  • If you use asynchronous image moderation, you must poll the moderation results or configure a callback notification to receive the moderation results. For more information about the related parameters, see /green/image/asyncscan and /green/image/results.

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.

(Recommended) Submit synchronous image moderation tasks

Operation

Description

Supported region

ImageSyncScanRequest

Sends synchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

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'
    );

    /* 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. 
     */
    $response = 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('porn'), 'bizType' => 'Business scenario')))
        ->request();
    print_r($response->toArray());
    if (200 == $response->code) {
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if (200 == $taskResult->code) {
                $sceneResults = $taskResult->results;
                foreach ($sceneResults as $sceneResult) {
                    $scene = $sceneResult->scene;
                    $suggestion = $sceneResult->suggestion;
                    // Take further action based on the values of the scene and suggestion parameters. 
                    print_r($scene);
                    print_r($suggestion);
                }
            } else {
                print_r("task process fail:" . $response->code);
            }
        }
    } else {
        print_r("detect not success. code:" . $response->code);
    }
} catch (Exception $exception) {
    echo $exception->getMessage() . PHP_EOL;
}

Submit asynchronous image moderation tasks

This section describes how to use Content Moderation SDK for PHP to submit asynchronous image moderation tasks. When you submit a request, you can configure a callback notification to receive the moderation results. Alternatively, you can call the ImageAsyncScanResultsRequest operation to poll the moderation results.

Operation

Description

Supported region

ImageAsyncScanRequest

Sends asynchronous requests to moderate images for risky content in multiple moderation scenarios, including pornography, terrorist content, ad, QR code, undesirable scene, and logo detection.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
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'
    );

    // scenes: the moderation scenarios. Example: porn and terrorism. 
    $result = Green::v20180509()->imageAsyncScan()
        ->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("porn", "terrorism"), "bizType" => "Business scenario")))
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Query the results of asynchronous image moderation

Operation

Description

Supported region

ImageAsyncScanResultsRequest

Queries asynchronous image moderation results. You can query the moderation results of multiple asynchronous image moderation tasks at a time.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
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();

    $result = Green::v20180509()->imageAsyncScanResults()
        ->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('ID of the asynchronous image moderation task')))
        ->request();
    print_r($result->toArray());

} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Provide feedback on image moderation results

If the results of image moderation do not meet your expectations, you can call the ImageScanFeedbackRequest operation to modify the results. Content Moderation adds the moderated image to the similar image blacklist or whitelist based on your feedback. When you submit a similar image for moderation, Content Moderation returns moderation results based on the label in your feedback.

For more information, see /green/image/feedback.

Operation

Description

Supported region

ImageScanFeedbackRequest

Provides feedback on image moderation results and modifies the machine-assisted moderation results based on the feedback.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
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();

    // scenes: the moderation scenarios. Example: porn and terrorism. 
    $result = Green::v20180509()->imageScanFeedback()
        ->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("suggestion" => "block", "scenes" => array("porn", "terrorism"), "url" => "URL of the moderated image")))
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}