All Products
Search
Document Center

Content Moderation:Manage custom image libraries

Last Updated:Aug 07, 2023

This topic describes how to use Content Moderation SDK for PHP to manage custom image libraries.

Description

You can customize image samples based on your management requirements for personalized content. Then, you can use custom image libraries to intelligently detect pornographic content, terrorist content, and ad violations in images and videos. For more information about the related parameters, see CreateImageLib.

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.

Query custom image libraries

You can use the following code to query image libraries, including custom image libraries and feedback-based image libraries:

<?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::v20170823()->describeImageLib()
        ->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. 
        ->withServiceModule('open_api')
        ->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;
}

Create a custom image library

You can use the following code to create a custom image library:

Note

Set the parameters based on your business scenario.

<?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::v20170823()->createImageLib()
        ->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. 
        ->withServiceModule('open_api')
        ->withName ('Name of the custom image library')
        ->withScene('Business scenario to which the custom image library applies')
        ->withCategory ('Type of the custom image library')
        ->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;
}

Modify a custom image library

You can use the following code to change the name of a custom image library and the business scenario to which the custom image library applies:

<?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::v20170823()->updateImageLib()
        ->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. 
        ->withId ('Image ID')
        ->withName ('Name of the custom image library')
        ->withScene('Business scenario to which the custom image library applies')
        ->withCategory ('Type of the custom image library')
        ->withBizTypes(array ('Business scenario 1', 'Business scenario 2'))
        ->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;
}

Delete a custom image library

You can use the following code to delete a custom image library:

Important

After you delete a custom image library, all images in the library are also deleted.

<?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::v20170823()->deleteImageLib()
        ->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. 
        ->withId ('ID of the custom image library')
        ->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 images in a custom image library

You can use the following code to query all images that have been added to a custom image library:

<?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::v20170823()->describeImageFromLib()
        ->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. 
        ->withImageLibId ('ID of the custom image library')
        ->withPageSize('10')
        ->withCurrentPage('1')
        ->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;
}

Delete images from a custom image library

You can use the following code to delete multiple images from a custom image library:

<?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();

    // Separate multiple image IDs with commas (,). 
    $result = Green::v20170823()->deleteImageFromLib()
        ->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. 
        ->withIds("['Image ID_1', 'Image ID_2']")
        ->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;
}