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

  • The PHP SDK supports both synchronous and asynchronous image moderation. If you use synchronous image moderation, the moderation results are returned in real time. If you use asynchronous image moderation, you must poll the moderation results or configure a callback notification to receive the moderation results.
  • You can submit the URL of an online image, the URL of a local image, or a binary image stream for image moderation.

Preparations

Before calling operations, make the following preparations:
  1. Create an AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information, see Create an AccessKey.
  2. Install PHP dependencies. For more information, see Installation.

(Recommended) Submit synchronous image moderation tasks

Sample code
  • Submit the URL of an online image for image moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\ImageSyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    $task1 = array('dataId' =>  uniqid(),
        'url' => 'http://Your image.jpg'
    );
    // 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 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 will be extended.
    // The sample code uses a single image as an example. If you want to moderate multiple images at a time, create one task for each image to be moderated.
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple images at a time and specify multiple moderation scenarios for each image. The expenses of all scenarios are calculated separately and summed up.
    // For example, if you moderate two images for both pornography and terrorist content, you are charged for moderating two images for pornography and two images for terrorist content.
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn","terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        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 a further action on the image based on the values of the scene and suggestion parameters.
                        // do something
                    }
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit the URL of a local image for image moderation
    <? php
    
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    
    $request = new Green\ImageSyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    
    // Upload your local image before moderation.
    $uploader = ClientUploader::getImageClientUploader($client);
    $url = $uploader->uploadFile("d:/1.png");
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    // 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 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 will be extended.
    // The sample code uses a single image as an example. If you want to moderate multiple images at a time, create one task for each image to be moderated.
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple images at a time and specify multiple moderation scenarios for each image. The expenses of all scenarios are calculated separately and summed up.
    // For example, if you moderate two images for both pornography and terrorist content, you are charged for moderating two images for pornography and two images for terrorist content.
    $request->setContent(json_encode(array("tasks" => array($task1),
        "scenes" => array("porn","terrorism"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        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 a further action on the image based on the values of the scene and suggestion parameters.
                        // do something
                    }
                }else{
                    print_r("task process fail:" + $response->code);
                }
            }
        }else{
            print_r("detect not success. code:" + $response->code);
        }
    } catch (Exception $e) {
        print_r($e);
    }
  • Submit a binary image stream for image moderation
    <? php
    require_once 'aliyuncs/aliyun-oss-php-sdk/autoload.php';
    include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
    use Green\Request\V20180509 as Green;
    use Green\Request\Extension\ClientUploader;
    
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret"); 
    DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new Green\ImageSyncScanRequest();
    $request->setMethod("POST");
    $request->setAcceptFormat("JSON");
    $uploader = ClientUploader::getImageClientUploader($client);
    
    // Read and convert a local image to binary data and submit the binary data for moderation.
    $bytes = file_get_contents("d:/Your image.jpg");
    $url = $uploader->uploadBytes($bytes);
    $task1 = array('dataId' =>  uniqid(),
        'url' => $url
    );
    // 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 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 will be extended.
    // The sample code uses a single image as an example. If you want to moderate multiple images at a time, create one task for each image to be moderated.
    // The system charges you based on the moderation scenario that you specify.
    // You can send a request to moderate multiple images at a time and specify multiple moderation scenarios for each image. The expenses of all scenarios are calculated separately and summed up.
    // For example, if you moderate two images for both pornography and terrorist content, you are charged for moderating two images for pornography and two images for terrorist content.
    $request->setContent(json_encode(array("tasks" => array($task1),
                                  "scenes" => array("porn"))));
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
        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 a further action on the image based on the values of the scene and suggestion parameters.
                        // do something
                        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 $e) {
        print_r($e);
    }

Submit asynchronous image moderation tasks

The following section describes how to use the ImageAsyncScanRequest operation of the PHP SDK to moderate images for risky content. You can send an asynchronous request to submit an image moderation task. When submitting the request, you can configure a callback notification to receive the moderation results. Alternatively, you can call the ImageAsyncScanResultsRequest operation to poll the moderation results.

As with synchronous image moderation, you can submit the URL of an online image, the URL of a local image, or a binary image stream for asynchronous image moderation. The following sample code uses the URL of an online image as an example.

Sample code
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\ImageAsyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' =>  uniqid(),
    'url' => 'http://xxx.jpg'
);

// Valid values of the scenes parameter are porn, terrorism, qrcode, ad, and ocr.
$request->setContent(json_encode(array("tasks" => array($task1),
    "scenes" => array("porn","terrorism"))));
try {
    $response = $client->getAcsResponse($request);
    print_r($response);
    if(200 == $response->code){
        $taskResults = $response->data;
        foreach ($taskResults as $taskResult) {
            if(200 == $taskResult->code){
                $taskId = $taskResult->taskId;
                // Save the task ID, which is used to poll the moderation results at the specified interval. For more information, see the description of the ImageAsyncScanResultsRequest operation.
                print_r($taskId);
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}

Query the results of asynchronous image moderation

Sample code
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$request = new Green\ImageAsyncScanResultsRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$request->setContent(json_encode(array('img4QZbr4m3rGh4Fqp$vsHWsK-1q5mP@')));

try {
    $response = $client->getAcsResponse($request);
    print_r($response, JSON_FORCE_OBJECT);
    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 a further action on the image based on the values of the scene and suggestion parameters.
                    // do something
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}