This topic shows you how to use SDKs for PHP to moderate images for risky content.

Background information

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.
  • 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 an on-premises image, or a binary image stream for image moderation.

Before you begin

Before you call operations, make the following preparations:

(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 (Singapore)
  • us-west-1: US (Silicon Valley)
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 pair of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret");
    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.
    // In this example, a single image is moderated. If you need to moderate multiple images at a time, create one task for each image to be moderated.
    // 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 separately calculated 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 based on the values of the scene and suggestion parameters.
                        // Take different actions on the image based on the different values of the suggestion parameter. For example, delete the illegal data.
                    }
                }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 an on-premises 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 pair of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret");
    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 on-premises 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.
    // In this example, a single image is moderated. If you need to moderate multiple images at a time, create one task for each image to be moderated.
    // 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 separately calculated 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 based on the values of the scene and suggestion parameters.
                        // Take different actions on the image based on the different values of the suggestion parameter. For example, delete the illegal data.
                    }
                }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 pair of your Alibaba Cloud account.
    $iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret");
    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 an on-premises 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.
    // In this example, a single image is moderated. If you need to moderate multiple images at a time, create one task for each image to be moderated.
    // 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 separately calculated 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 based on the values of the scene and suggestion parameters.
                        // Take different actions on the image based on the different values of the suggestion parameter. For example, delete the illegal data.
                        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 shows you how to use the ImageAsyncScanRequest operation of SDKs for PHP to moderate images for risky content. You can send an asynchronous request to submit an image moderation task. When you submit 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.

Same as synchronous image moderation, you can submit the URL of an online image, the URL of an on-premises image, or a binary image stream for asynchronous image moderation. In this example, the URL of an online image is used.

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.
Note You can call the ImageAsyncScanResultsRequest operation to poll the moderation results.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
  • us-west-1: US (Silicon Valley)
Sample code
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey pair of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret");
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.
                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

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 (Singapore)
  • us-west-1: US (Silicon Valley)
Sample code
<? php
include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;

// Use the AccessKey pair of your Alibaba Cloud account.
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", "yourAccessKeyId", "yourAccessKeySecret");
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 based on the values of the scene and suggestion parameters.
                    // Take different actions on the image based on the different values of the suggestion parameter. For example, delete the illegal data.
                }
            }else{
                print_r("task process fail:" + $response->code);
            }
        }
    }else{
        print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    print_r($e);
}