このトピックでは、Alibaba Cloud Image Search SDKs for PHP の使用方法について説明します。
準備
- Alibaba Cloud SDK をインストールして使用する前に、Alibaba Cloud アカウントを作成し、AccessKey ペアを取得していることを確認してください。 詳細については、「AccessKey ペアの作成」をご参照ください。
- Alibaba Cloud Image Search SDK for PHPをダウンロードします。
サンプルコード
完全なサンプルコードは次のとおりです。
<? php
// Reference the SDK package that you download.
require_once "./aliyun-php-sdk-core/Config.php";
use ImageSearch\Request\V20190325\AddImageRequest;
use ImageSearch\Request\V20190325\SearchImageRequest;
use ImageSearch\Request\V20190325\DeleteImageRequest;
DefaultProfile::addEndPoint("<region>", "<region>", "ImageSearch", "imagesearch.<region>.aliyuncs.com");
$profile = DefaultProfile::getProfile("<region>", "<your-access-key-id>", "<your-access-key-secret>");
$client = new DefaultAcsClient($profile);
// Add the image.
$addRequest = new AddImageRequest();
$addRequest->setInstanceName("demo");
$addRequest->setProductId("test");
$addRequest->setPicName("test");
$content = file_get_contents("/home/admin/demo.jpg");
$encodePicContent = base64_encode($content);
$addRequest->setPicContent($encodePicContent);
// Optional. Specifies whether to recognize the subject in the image and search for images based on the recognized subject. The default value is true.
// 1. true: The system recognizes the subject in the image, and searches for images based on the recognized subject. You can obtain the recognition result in the response.
// 2. false: The system does not recognize the subject of the image, and searches for images based on the entire image.
//$addRequest->setCrop("false");
//$addRequest->setCrop("true");
// Optional. The subject area in the image. The subject area is in the format of x1,x2,y1,y2. x1 and y1 represent the upper-left corner pixel. x2 and y2 represent the lower-right pixel.
// If you specify the Region parameter, the system searches for images based on this parameter setting regardless of the value of the Crop parameter.
// $ addRequest-> setRegion( "100 、300 、100 、300 ");
try {
$addResponse = $client->getAcsResponse($addRequest);
print_r($addResponse);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
// Search for images.
$searchRequest = new SearchImageRequest();
$searchRequest->setInstanceName("demo");
$searchRequest->setType("SearchByName");
$searchRequest->setProductId("test");
$searchRequest->setPicName("test");
try {
$response = $client->getAcsResponse($request);
print_r($searchResponse);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
// Delete the image.
$deleteRequest = new DeleteImageRequest();
$deleteRequest->setInstanceName("demo");
$deleteRequest->setProductId("test");
try {
$deleteResponse = $client->getAcsResponse($deleteRequest);
print_r($deleteResponse);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}