| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\Azure; |
| 4 |
|
| 5 |
use AATXT\App\Admin\PluginOptions; |
| 6 |
use AATXT\App\AIProviders\AIProviderInterface; |
| 7 |
use AATXT\App\Exceptions\Azure\AzureComputerVisionException; |
| 8 |
use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException; |
| 9 |
use AATXT\Config\Constants; |
| 10 |
|
| 11 |
class AzureComputerVisionCaptionsResponse implements AIProviderInterface |
| 12 |
{ |
| 13 |
private function __construct() |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
public static function make(): AzureComputerVisionCaptionsResponse |
| 18 |
{ |
| 19 |
return new self(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Make a request to Azure Computer Vision APIs to retrieve the contents of the uploaded image |
| 24 |
* If necessary, translate the description into the requested language |
| 25 |
* @param string $imageUrl |
| 26 |
* @return string |
| 27 |
* @throws AzureComputerVisionException |
| 28 |
* @throws AzureTranslateInstanceException |
| 29 |
*/ |
| 30 |
public function response(string $imageUrl): string |
| 31 |
{ |
| 32 |
$response = wp_remote_post( |
| 33 |
PluginOptions::endpointAzureComputerVision() . 'computervision/imageanalysis:analyze?api-version='.Constants::AATXT_AZURE_COMPUTER_VISION_API_VERSION.'&features=caption&language=en&gender-neutral-caption=False', |
| 34 |
[ |
| 35 |
'headers' => [ |
| 36 |
'content-type' => 'application/json', |
| 37 |
'Ocp-Apim-Subscription-Key' => PluginOptions::apiKeyAzureComputerVision(), |
| 38 |
], |
| 39 |
'body' => json_encode([ |
| 40 |
'url' => $imageUrl, |
| 41 |
]), |
| 42 |
'method' => 'POST', |
| 43 |
] |
| 44 |
); |
| 45 |
|
| 46 |
$responseBody = wp_remote_retrieve_body($response); |
| 47 |
if(empty($responseBody)) { |
| 48 |
if (is_object($response) && property_exists($response, 'errors') && array_key_exists('http_request_failed', $response->errors)) { |
| 49 |
throw new AzureTranslateInstanceException("Error: " . $response->errors['http_request_failed'][0]); |
| 50 |
} |
| 51 |
if (is_array($response) && array_key_exists('response', $response)) { |
| 52 |
throw new AzureTranslateInstanceException("Code: " . $response['response']['code'] . " - " . $response['response']['message']); |
| 53 |
} |
| 54 |
throw new AzureComputerVisionException("Error: please check if the Azure endpoint in plugin options is right"); |
| 55 |
} |
| 56 |
|
| 57 |
$bodyResult = json_decode($responseBody, true); |
| 58 |
if (array_key_exists('error', $bodyResult)) { |
| 59 |
throw new AzureComputerVisionException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']); |
| 60 |
} |
| 61 |
|
| 62 |
$altText = $bodyResult['captionResult']['text']; |
| 63 |
$selectedLanguage = PluginOptions::languageAzureTranslateInstance(); |
| 64 |
|
| 65 |
// If the default language (en) is selected it is not necessary a translation |
| 66 |
if ($selectedLanguage == Constants::AATXT_AZURE_DEFAULT_LANGUAGE) { |
| 67 |
return $altText; |
| 68 |
} |
| 69 |
|
| 70 |
return (AzureTranslator::make())->translate($altText, $selectedLanguage); |
| 71 |
} |
| 72 |
} |