Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
- 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.
To create and run the sample, do the following steps:
Install the PHP5
HTTP_Request2
package.Open a command prompt window as an administrator.
Run the following command:
pear install HTTP_Request2
After the package is successfully installed, close the command prompt window.
Copy the following code into a text editor.
Make the following changes in code where needed:
- Replace the value of
subscriptionKey
with your subscription key. - 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. - Optionally, replace the value of
imageUrl
with the URL of a different image for which you want to generate a thumbnail.
- Replace the value of
Save the code as a file with a
.php
extension. For example,get-thumbnail.php
.Open a browser window with PHP support.
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>
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.
When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2
package. To uninstall the package, do the following steps:
Open a command prompt window as an administrator.
Run the following command:
pear uninstall HTTP_Request2
After the package is successfully uninstalled, close the command prompt window.
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.