This topic describes how to use Alibaba Cloud Image Search SDKs for PHP.
Preparations
- Before you install and use Alibaba Cloud SDKs, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Create an AccessKey pair.
- Download the Alibaba Cloud Image Search SDK for PHP.
Sample code
The following complete sample code is provided:
<? 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 {
$searchResponse = $client->getAcsResponse($searchRequest);
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";
}