Skip to content

Commit

Permalink
fix: change logic to send raw data to api
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-imhof committed Dec 21, 2023
1 parent 6ca41b2 commit fca9f2c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
26 changes: 13 additions & 13 deletions resources/js/components/AltTextGeneratorInputsFieldtype.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div ref="node" />
<div ref="node"/>
</template>

<script>
Expand Down Expand Up @@ -67,7 +67,7 @@ export default {
let language = '';
const parent = e.currentTarget.closest('.form-group');
const selectedInput = parent.querySelector('.input-group')
.querySelector('.input-text');
.querySelector('.input-text');
if (selectedInput.getAttribute('id') === 'field_alt_de') {
language = 'de';
} else if (selectedInput.getAttribute('id') === 'field_alt_fr') {
Expand All @@ -87,18 +87,18 @@ export default {
},
body: dataToSend,
})
.then((response) => response.json())
.then((data) => {
selectedInput.value = data.altText;
const inputEvent = new Event('input', {
bubbles: true,
cancelable: true,
.then((response) => response.json())
.then((data) => {
selectedInput.value = data.altText;
const inputEvent = new Event('input', {
bubbles: true,
cancelable: true,
});
selectedInput.dispatchEvent(inputEvent);
})
.catch((error) => {
console.log(error);
});
selectedInput.dispatchEvent(inputEvent);
})
.catch((error) => {
console.log(error);
});
},
},
};
Expand Down
8 changes: 4 additions & 4 deletions src/Controllers/AltTextGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function generateAltText(string $url, string $textLanguage)
$imageDataService = new ImageDataService();
$altTextAiApiRequestService = new AltTextAiApiRequestService();

$imageUrl = $imageDataService->processImageUrl($url);
if (!$imageUrl) {
return new JsonResponse(['error' => 'imageUrl could not be created'], '401');
$rawData = $imageDataService->processImageUrl($url);
if (!$rawData) {
return new JsonResponse(['error' => 'Image raw data could not be created'], '401');
}

$altText = $altTextAiApiRequestService->sendRequest($imageUrl, $textLanguage);
$altText = $altTextAiApiRequestService->sendRequest($rawData, $textLanguage);

if (!$altText) {
return new JsonResponse(['error' => 'alt text could not be generated'], '401');
Expand Down
6 changes: 3 additions & 3 deletions src/Services/AltTextAiApiRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AltTextAiApiRequestService
{
public function sendRequest(string $imageUrl, string $textLanguage)
public function sendRequest(string $rawData, string $textLanguage)
{
$apiEndpoint = AltTextAiApi::API_ENDPOINT;
$client = new Client();
Expand All @@ -21,7 +21,7 @@ public function sendRequest(string $imageUrl, string $textLanguage)
$data = [
'lang' => $textLanguage,
'image' => [
'url' => $imageUrl
'raw' => $rawData
]
];

Expand All @@ -32,7 +32,7 @@ public function sendRequest(string $imageUrl, string $textLanguage)
]);

$responseData = json_decode($response->getBody());
$altText = $responseData['alt_text'];
$altText = $responseData->alt_text;
} catch (Exception $e) {
return $e->getMessage();
}
Expand Down
15 changes: 12 additions & 3 deletions src/Services/ImageDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ class ImageDataService
public function processImageUrl(string $url): string
{
$replacements = array(
"/cp" => "",
"https://boilerplate.gridonic.test/cp" => "",
"/browse" => "",
"_assets" => "",
"/edit" => "",
);
$imageUrl = strtr($url, $replacements);
$imagePath = $_SERVER['DOCUMENT_ROOT'] . strtr($url, $replacements);

return $imageUrl;
$rawData = $this->encodeImage($imagePath);
return $rawData;
}

public function encodeImage(string $imagePath): string
{
$image = file_get_contents($imagePath);
$rawData = base64_encode($image);

return $rawData;
}
}

0 comments on commit fca9f2c

Please sign in to comment.