This topic describes how to install Image Search SDK for PHP and provides sample code on how to use the SDK.

Methods

Method Description
Add Adds one or more images.
SearchImageByPic Searches for similar images based on an image.
SearchImageByName Searches for similar images based on the name of an existing image in the image gallery.
Delete Deletes one or more images.

Preparations

Before you install and use an Alibaba Cloud SDK, make sure that you have created an Alibaba Cloud account and obtained the AccessKey pair of the Alibaba Cloud account. For more information, see Create an AccessKey pair.

Note The version of PHP must be 5.5 or later.

Obtain Composer from the Alibaba Cloud mirror:

https://developer.aliyun.com/composer
Install dependencies.
  • Use Composer to obtain Image Search SDK for PHP.
    composer require alibabacloud/imagesearch-20201214
  • Use the composer.json file.
    {
       "require": {
           "alibabacloud/imagesearch-20201214": "^3.1"    
        }
    }
The following sample code shows the dependency-related information:
declare (strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20201214\ImageSearch;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\AddImageAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\SearchImageByPicAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\SearchImageByNameRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\DeleteImageRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

Add

  • Sample code
    function _addImage()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new AddImageAdvanceRequest();
        // Required. The name of the Image Search instance. 
        $request->instanceName = "xxxxxx";
        // Required. The ID of the product. The value cannot exceed 512 characters in length. 
        // A product ID can relate to multiple image names. 
        $request->productId = "testPhp";
        // Required. The name of the image. The value cannot exceed 512 characters in length. 
        // 1. An image is uniquely identified by the values of the productId and picName parameters. 
        // 2. If you add multiple images that share the same values of the productId and picName parameters, only the last image that you add is retained. 
        $request->picName = "testPhp";
        // Optional. The ID of the product category. 
        // 1. For product image searches: If you set a category ID for an image, the specified category prevails. If you do not set a category ID for an image, the system predicts the category, and returns the ID of the predicted category in the response. 
        // 2. For cloth, brand, and generic image searches: The category ID is set to 88888888 regardless of whether a category ID is set. 
        $request->categoryId = 3;
        // Optional. The description that you want to add. The value cannot exceed 4,096 characters in length. 
        // If you set this parameter, the response includes this parameter and its value. You can add text such as an image description. 
        $request->customContent = "this is a simple test";
        // Optional. The attribute of the INT type. The attribute can be used to filter images in image searches. If you set this parameter, the response includes this parameter and its value. 
        // For example, you can set different attributes for images from different sites or different users. This way, users can filter images by attribute and obtain more accurate search results.
        $request->intAttr = 56;
        // Optional. The attribute of the STRING type. The value cannot exceed 128 characters in length. The attribute can be used to filter images in image searches. If you set this parameter, the response includes this parameter and its value. 
        $request->strAttr = "test";
        // Optional. Specifies whether to recognize the main subject in the image. Default value: true. 
        // 1.If you set this parameter to true, the system recognizes the main subject in the image, and searches for images based on the recognized subject. The response includes the recognition result. 
        // 2. If you set this parameter to false, the system does not recognize the main subject in the image, and searches for images based on the entire image. 
        // 3.For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->crop = true;
        // Optional. The main area of the image. The value is in the format of x1,x2,y1,y2. x1 and y1 indicate the position of the upper-left point of the area, in pixels. x2 and y2 indicate the position of the lower-right point of the area, in pixels. The specified area cannot cross the boundary of the image. 
        // If you set the main area, the system searches for images based on the main area regardless of the value of the crop parameter. 
        // For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->region = "167,467,221,407";
        // The image. The image cannot exceed 4 MB in size. The transmission timeout period is 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WebP, TIFF, and PPM.
        // For product, brand, and generic images, the length and the width of the image must range from 100 pixels to 4,096 pixels.
        // For cloth images, the length and the width of the image must range from 448 pixels to 4,096 pixels.
        // The image cannot contain rotation information.
        $request->picContentObject = new Stream(fopen("<filePath>", 'r+'));
          
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->addImageAdvance($request, $runtime);
            var_dump($response->toMap());
          } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } 
    }
  • Sample response
    {
      ["requestId"]=>
      string(36) "242CB8B7-DD36-42ED-9A76-37915DC7ED81"
      ["success"]=>
      bool(true)
      ["message"]=>
      string(7) "success"
      ["code"]=>
      int(0)
      ["picInfo"]=>
      array(2) {
        ["CategoryId"]=>
        int(0)
        ["Region"]=>
        string(15) "167,477,221,407"
      }
      ["_name":protected]=>
      array(5) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["message"]=>
        string(7) "Message"
        ["code"]=>
        string(4) "Code"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

SearchImageByPic

  • Sample code
    function _getImageByPic()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new SearchImageByPicAdvanceRequest();
        // Required. The name of the Image Search instance. 
        $request->instanceName = "xxxxx";    
        // The image. The image cannot exceed 4 MB in size. The transmission timeout period is 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WebP, TIFF, and PPM.
        // For product, brand, and generic images, the length and the width of the image must range from 100 pixels to 4,096 pixels.
        // For cloth images, the length and the width of the image must range from 448 pixels to 4,096 pixels.
        // The image cannot contain rotation information.
        $imageStream = new Stream(fopen("<filePath>", 'r+'));    
        // Optional. The ID of the product category. 
        // 1. For product image searches: If you set a category ID for an image, the specified category prevails. If you do not set a category ID for an image, the system predicts the category, and returns the ID of the predicted category in the response. 
        // 2. For cloth, brand, and generic image searches: The category ID is set to 88888888 regardless of whether a category ID is set. 
        $request->categoryId = 3;
        // 1.If you set this parameter to true, the system recognizes the main subject in the image, and searches for images based on the recognized subject. The response includes the recognition result. 
        // 2. If you set this parameter to false, the system does not recognize the main subject in the image, and searches for images based on the entire image. 
        // 3.For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->crop = true;
        // Optional. The main area of the image. The value is in the format of x1,x2,y1,y2. x1 and y1 indicate the position of the upper-left point of the area, in pixels. x2 and y2 indicate the position of the lower-right point of the area, in pixels. The specified area cannot cross the boundary of the image. 
        // If you set the main area, the system searches for images based on the main area regardless of the value of the crop parameter. 
        // For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->region = "167,467,221,407";
        // Optional. The filter condition. The int_attr field supports the following operators: >, >=, <, <=, and =. The str_attr field supports the following operators: = and !=.You can join multiple filter conditions by using the AND or OR logical operator. 
        // Examples:
        // 1. Filter images based on the int_attr field: int_attr>=100
        // 2. Filter images based on the str_attr field: str_attr!="value1" 
        // 3. Filter images based on int_attr and str_attr fields: int_attr=1000 AND str_attr="value1"
        $request->filter = "int_attr=56 AND str_attr =\"test\"";
        $request->picContentObject = $imageStream;
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->searchImageByPicAdvance($request, $runtime);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } catch (Exception $e) {
        }
    }
  • Sample response
    {
      ["requestId"]=>
      string(36) "453517A3-7D00-476E-A946-8CC3B9DC6FA1"
      ["success"]=>
      bool(true)
      ["code"]=>
      int(0)
      ["msg"]=>
      string(7) "success"
      ["auctions"]=>
      array(5) {
        [0]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(7) "vae.jpg"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
        [1]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "test"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5)
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(171)
      }
      ["picInfo"]=>
      array(3) {
        ["CategoryId"]=>
        int(0)
        ["MultiRegion"]=>
          array(1) {
            [0]=>
            array(1) {
              ["Region"]=>
              string(15) "134,705,187,785"
            }
         }
        ["AllCategories"]=>
        array(14) {
          [0]=>
          array(2) {
            ["Id"]=>
            int(0)
            ["Name"]=>
            string(4) "Tops"
          }
      ........
          [13]=>
          array(2) {
            ["Id"]=>
            int(88888888)
            ["Name"]=>
            string(5) "Other"
          }
        }
        ["Region"]=>
        string(15) "167,477,221,407"
      }
      ["_name":protected]=>
      array(7) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["code"]=>
        string(4) "Code"
        ["msg"]=>
        string(3) "Msg"
        ["auctions"]=>
        string(8) "Auctions"
        ["head"]=>
        string(4) "Head"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

SearchImageByName

  • Sample code
    function _getImageByName()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new SearchImageByNameRequest();
        // Required. The name of the Image Search instance. 
        $request->instanceName = "xxxxxxx";
        // Required. The ID of the product. The value cannot exceed 512 characters in length. 
        // A product ID can relate to multiple image names. 
        $request->productId = "testPhp";
        // Required. The name of the image. The value cannot exceed 512 characters in length. 
        // 1. An image is uniquely identified by the values of the productId and picName parameters. 
        // 2. If you add multiple images that share the same values of the productId and picName parameters, only the last image that you add is retained. 
        $request->picName = "testPhp";    
        // Optional. The ID of the product category. 
        // 1. For product image searches: If you set a category ID for an image, the specified category prevails. If you do not set a category ID for an image, the system predicts the category, and returns the ID of the predicted category in the response. 
        // 2. For cloth, brand, and generic image searches: The category ID is set to 88888888 regardless of whether a category ID is set. 
        $request->categoryId = 3;
        // 1.If you set this parameter to true, the system recognizes the main subject in the image, and searches for images based on the recognized subject. The response includes the recognition result. 
        // 2. If you set this parameter to false, the system does not recognize the main subject in the image, and searches for images based on the entire image. 
        // 3.For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->crop = true;
        // Optional. The main area of the image. The value is in the format of x1,x2,y1,y2. x1 and y1 indicate the position of the upper-left point of the area, in pixels. x2 and y2 indicate the position of the lower-right point of the area, in pixels. The specified area cannot cross the boundary of the image. 
        // If you set the main area, the system searches for images based on the main area regardless of the value of the crop parameter. 
        // For cloth image searches, this parameter does not take effect. The system searches for images based on the entire image. 
        $request->region = "167,467,221,407";
        // Optional. The filter condition. The int_attr field supports the following operators: >, >=, <, <=, and =. The str_attr field supports the following operators: = and !=.You can join multiple filter conditions by using the AND or OR logical operator. 
        // Examples:
        // 1. Filter images based on the int_attr field: int_attr>=100
        // 2. Filter images based on the str_attr field: str_attr!="value1" 
        // 3. Filter images based on int_attr and str_attr fields: int_attr=1000 AND str_attr="value1"
        $request->filter = "int_attr=56 AND str_attr =\"test\"";
     
        try {
            $response = $client->SearchImageByName($request);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } catch (Exception $e) {
        }
    }
  • Sample response
    ["requestId"]=>
      string(36) "9248A5FC-97AB-4A96-8764-C771CF61D9A1"
      ["success"]=>
      bool(true)
      ["code"]=>
      int(0)
      ["msg"]=>
      string(7) "success"
      ["auctions"]=>
      array(5) {
        [0]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(7) "vae.jpg"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5)
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(21)
      }
      ["picInfo"]=>
      array(2) {
        ["CategoryId"]=>
        int(0)
        ["MultiRegion"]=>
          array(1) {
            [0]=>
            array(1) {
              ["Region"]=>
              string(15) "134,705,187,785"
            }
        }
        ["AllCategories"]=>
        array(14) {
          [0]=>
          array(2) {
            ["Id"]=>
            int(0)
            ["Name"]=>
            string(4) "Tops"
          }
          ........
          [13]=>
          array(2) {
            ["Id"]=>
            int(88888888)
            ["Name"]=>
            string(5) "Other"
          }
        }
      }
      ["_name":protected]=>
      array(7) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["code"]=>
        string(4) "Code"
        ["msg"]=>
        string(3) "Msg"
        ["auctions"]=>
        string(8) "Auctions"
        ["head"]=>
        string(4) "Head"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

Delete

  • Sample code
    function _deleteImage()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new DeleteImageRequest();
        // Required. The name of the Image Search instance. 
        $request->instanceName = "xxxxxx";
        // Required. The ID of the product. The value cannot exceed 512 characters in length. 
        // 1. An image is uniquely identified by the values of the productId and picName parameters. 
        $request->productId = "test";
        // 2. Optional. The name of the image. If you do not set this parameter, the system deletes all the images that are related to the specified product ID. If you set this parameter, the system deletes only the image that is specified by the productId and picName parameters. 
        $request->picName = "test";
          
        try {
            $response = $client->deleteImage($request);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
          } catch (Exception $e) {
        }
    }
  • Sample response
    {
      ["requestId"]=>
      string(36) "052ECD87-933D-4E65-94F7-F0C0947E41A1"
      ["success"]=>
      bool(true)
      ["message"]=>
      string(7) "success"
      ["code"]=>
      int(0)
      ["_name":protected]=>
      array(4) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["message"]=>
        string(7) "Message"
        ["code"]=>
        string(4) "Code"
      }
      ["_required":protected]=>
      array(0) {
      }
    }