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

Background information

You can customize image samples based on personalized content management requirements. 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 parameters, see CreateImageLib.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information about how to install the dependencies, 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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20170823()->describeImageLib()
        ->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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20170823()->createImageLib()
        ->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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20170823()->updateImageLib()
        ->withId('ID of the custom image library')
        ->withName('New name of the custom image library')
        ->withScene('New 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:
Notice If 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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20170823()->deleteImageLib()
        ->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 are 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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $result = Green::v20170823()->describeImageFromLib()
        ->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 {
    // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
    AlibabaCloud::accessKeyClient('Your AccessKey ID', 'Your AccessKey secret')
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    // Separate multiple images with commas (,). 
    $result = Green::v20170823()->deleteImageFromLib()
        ->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;
}