Quickstart: Generate a thumbnail using the Computer Vision REST API and PHP

In this quickstart, you generate a thumbnail from an image by using Computer Vision's REST API. With the Get Thumbnail method, you can generate a thumbnail of an image. You specify the height and width, which can differ from the aspect ratio of the input image. Computer Vision uses smart cropping to intelligently identify the area of interest and generate cropping coordinates based on that region.

If you don't have an Azure subscription, create a Trial before you begin.

Prerequisites

  • You must have PHP installed.
  • You must have Pear installed.
  • You must have a subscription key for Computer Vision. You can follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.

Create and run the sample

To create and run the sample, do the following steps:

  1. Install the PHP5 HTTP_Request2 package.

    1. Open a command prompt window as an administrator.

    2. Run the following command:

      pear install HTTP_Request2
      
    3. After the package is successfully installed, close the command prompt window.

  2. Copy the following code into a text editor.

  3. Make the following changes in code where needed:

    1. Replace the value of subscriptionKey with your subscription key.
    2. Replace the value of uriBase with the endpoint URL for the Get Thumbnail method from the Azure region where you obtained your subscription keys, if necessary.
    3. Optionally, replace the value of imageUrl with the URL of a different image for which you want to generate a thumbnail.
  4. Save the code as a file with a .php extension. For example, get-thumbnail.php.

  5. Open a browser window with PHP support.

  6. Drag and drop the file into the browser window.

<html>
<head>
    <title>Get Thumbnail Sample</title>
</head>
<body>
<?php
// Replace <Subscription Key> with a valid subscription key.
$ocpApimSubscriptionKey = '<Subscription Key>';

$uriBase = 'https://api.cognitive.azure.cn/vision/v2.0/';

$imageUrl =
    'https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg';

require_once 'HTTP/Request2.php';

$request = new Http_Request2($uriBase . 'generateThumbnail');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey
);
$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'width' => '100',  // Width of the thumbnail.
    'height' => '100', // Height of the thumbnail.
    'smartCropping' => 'true',
);
$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_POST);

// Request body parameters
$body = json_encode(array('url' => $imageUrl));

// Request body
$request->setBody($body);

try
{
    $response = $request->send();
    echo "<pre>" .
        json_encode(json_decode($response->getBody()), JSON_PRETTY_PRINT) . "</pre>";
}
catch (HttpException $ex)
{
    echo "<pre>" . $ex . "</pre>";
}
?>
</body>
</html>

Examine the response

A successful response is returned as binary data, which represents the image data for the thumbnail. If the request fails, the response is displayed in the browser window. The response for the failed request contains an error code and a message to help determine what went wrong.

Clean up resources

When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2 package. To uninstall the package, do the following steps:

  1. Open a command prompt window as an administrator.

  2. Run the following command:

    pear uninstall HTTP_Request2
    
  3. After the package is successfully uninstalled, close the command prompt window.

Next steps

Explore the Computer Vision API to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing console.