PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
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
auto-alt-text / src / App / AIProviders / Azure / AzureComputerVisionCaptionsResponse.php

AzureComputerVisionCaptionsResponse.php in Auto Alt Text 2.7.0, at src/App/AIProviders/Azure/AzureComputerVisionCaptionsResponse.php

184 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AATXT\App\AIProviders\Azure;
4
5 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 use AATXT\App\Exceptions\Azure\AzureComputerVisionException;
11 use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException;
12 use AATXT\App\Infrastructure\Http\HttpClientInterface;
13 use AATXT\Config\Constants;
14
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
26 {
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
65 {
66 return Constants::AATXT_AZURE_ALLOWED_MIME_TYPES;
67 }
68
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
76 {
77 return in_array($mimeType, $this->getSupportedMimeTypes(), true);
78 }
79
80 /**
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 *
141 * @param string $imageUrl
142 * @return string
143 * @throws AzureComputerVisionException
144 * @throws AzureTranslateInstanceException
145 */
146 public function response(string $imageUrl): string
147 {
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';
152
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());
166 }
167
168 if (array_key_exists('error', $bodyResult)) {
169 throw new AzureComputerVisionException(
170 "Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']
171 );
172 }
173
174 $altText = $bodyResult['captionResult']['text'];
175
176 // If translation is not enabled, return the original text
177 if (!$this->isTranslationEnabled()) {
178 return $altText;
179 }
180
181 return $this->translate($altText, $this->getTargetLanguage());
182 }
183 }
184