PluginProbe
Auto Alt Text / 2.5.2
Auto Alt Text v2.5.2
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
← All changes | src/App/AIProviders/Azure/AzureComputerVisionCaptionsResponse.php +37 -148 2.8.22.5.2 View file →
@@ -1,144 +1,28 @@
1 1 <?php
2 2
3 3 namespace AATXT\App\AIProviders\Azure;
4 4
5 +use AATXT\App\Admin\PluginOptions;
5 6 use AATXT\App\AIProviders\AIProviderInterface;
6 -use AATXT\App\AIProviders\Contracts\RequiresAuthentication;
7 -use AATXT\App\AIProviders\Contracts\SupportsImageValidation;
8 -use AATXT\App\AIProviders\Contracts\SupportsTranslation;
9 -use AATXT\App\Configuration\AzureConfig;
10 7 use AATXT\App\Exceptions\Azure\AzureComputerVisionException;
11 8 use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException;
12 -use AATXT\App\Infrastructure\Http\HttpClientInterface;
13 9 use AATXT\Config\Constants;
14 10
15 -/**
16 - * Azure Computer Vision provider for generating image captions.
17 - *
18 - * Implements SupportsImageValidation, RequiresAuthentication, and SupportsTranslation
19 - * interfaces following the Interface Segregation Principle.
20 - */
21 -class AzureComputerVisionCaptionsResponse implements
22 - AIProviderInterface,
23 - SupportsImageValidation,
24 - RequiresAuthentication,
25 - SupportsTranslation
11 +class AzureComputerVisionCaptionsResponse implements AIProviderInterface
26 12 {
27 - /**
28 - * @var HttpClientInterface
29 - */
30 - private $httpClient;
31 -
32 - /**
33 - * @var AzureConfig
34 - */
35 - private $config;
36 -
37 - /**
38 - * @var AzureTranslator
39 - */
40 - private $translator;
41 -
42 - /**
43 - * Constructor.
44 - *
45 - * @param HttpClientInterface $httpClient HTTP client for API calls
46 - * @param AzureConfig $config Azure configuration with API keys and endpoints
47 - * @param AzureTranslator $translator Azure translator for caption translation
48 - */
49 - public function __construct(
50 - HttpClientInterface $httpClient,
51 - AzureConfig $config,
52 - AzureTranslator $translator
53 - ) {
54 - $this->httpClient = $httpClient;
55 - $this->config = $config;
56 - $this->translator = $translator;
57 - }
58 -
59 - /**
60 - * Get the list of supported MIME types for Azure Computer Vision.
61 - *
62 - * @return array<string> List of supported MIME types
63 - */
64 - public function getSupportedMimeTypes(): array
13 + private function __construct()
65 14 {
66 - return Constants::AATXT_AZURE_ALLOWED_MIME_TYPES;
67 15 }
68 16
69 - /**
70 - * Check if a specific MIME type is supported.
71 - *
72 - * @param string $mimeType The MIME type to check
73 - * @return bool True if supported, false otherwise
74 - */
75 - public function supportsImage(string $mimeType): bool
17 + public static function make(): AzureComputerVisionCaptionsResponse
76 18 {
77 - return in_array($mimeType, $this->getSupportedMimeTypes(), true);
19 + return new self();
78 20 }
79 21
80 22 /**
81 - * Validate that valid credentials are configured.
82 - *
83 - * @return bool True if credentials are valid
84 - */
85 - public function validateCredentials(): bool
86 - {
87 - $apiKey = $this->config->getApiKey();
88 - $endpoint = $this->config->getEndpoint();
89 -
90 - return !empty($apiKey) && !empty($endpoint);
91 - }
92 -
93 - /**
94 - * Check if an API key is configured.
95 - *
96 - * @return bool True if API key is set
97 - */
98 - public function hasApiKey(): bool
99 - {
100 - return !empty($this->config->getApiKey());
101 - }
102 -
103 - /**
104 - * Translate text to the specified target language.
105 - *
106 - * @param string $text The text to translate
107 - * @param string $targetLanguage The target language code
108 - * @return string The translated text
109 - * @throws AzureTranslateInstanceException
110 - */
111 - public function translate(string $text, string $targetLanguage): string
112 - {
113 - return $this->translator->translate($text, $targetLanguage);
114 - }
115 -
116 - /**
117 - * Check if translation is enabled and configured.
118 - *
119 - * @return bool True if translation is available
120 - */
121 - public function isTranslationEnabled(): bool
122 - {
123 - $language = $this->config->getTranslationLanguage();
124 - return !empty($language) && $language !== Constants::AATXT_AZURE_DEFAULT_LANGUAGE;
125 - }
126 -
127 - /**
128 - * Get the configured target language for translation.
129 - *
130 - * @return string The language code, or empty string if not configured
131 - */
132 - public function getTargetLanguage(): string
133 - {
134 - return $this->config->getTranslationLanguage();
135 - }
136 -
137 - /**
138 - * Make a request to Azure Computer Vision APIs to retrieve the contents of the uploaded image.
139 - * If necessary, translate the description into the requested language.
140 - *
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
141 25 * @param string $imageUrl
142 26 * @return string
143 27 * @throws AzureComputerVisionException
144 28 * @throws AzureTranslateInstanceException
@@ -144,40 +28,45 @@
144 28 * @throws AzureTranslateInstanceException
145 29 */
146 30 public function response(string $imageUrl): string
147 31 {
148 - $endpoint = $this->config->getEndpoint();
149 - $url = $endpoint . 'computervision/imageanalysis:analyze?api-version='
150 - . Constants::AATXT_AZURE_COMPUTER_VISION_API_VERSION
151 - . '&features=caption&language=en&gender-neutral-caption=False';
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 + );
152 45
153 - $headers = [
154 - 'content-type' => 'application/json',
155 - 'Ocp-Apim-Subscription-Key' => $this->config->getApiKey(),
156 - ];
157 -
158 - $payload = [
159 - 'url' => $imageUrl,
160 - ];
161 -
162 - try {
163 - $bodyResult = $this->httpClient->post($url, $headers, $payload);
164 - } catch (\Exception $e) {
165 - throw new AzureComputerVisionException('HTTP request failed: ' . $e->getMessage());
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");
166 55 }
167 56
57 + $bodyResult = json_decode($responseBody, true);
168 58 if (array_key_exists('error', $bodyResult)) {
169 - throw new AzureComputerVisionException(
170 - "Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']
171 - );
59 + throw new AzureComputerVisionException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']);
172 60 }
173 61
174 62 $altText = $bodyResult['captionResult']['text'];
175 -
176 - // If translation is not enabled, return the original text
177 - if (!$this->isTranslationEnabled()) {
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) {
178 67 return $altText;
179 68 }
180 69
181 - return $this->translate($altText, $this->getTargetLanguage());
70 + return (AzureTranslator::make())->translate($altText, $selectedLanguage);
182 71 }
183 -}
72 +}