Skip to content

Commit

Permalink
fix: expand logic to send image as base64 encoded raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-imhof committed Feb 1, 2024
1 parent fca9f2c commit 68fd2f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion resources/js/components/AltTextGeneratorInputsFieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
if (labelNode.querySelector('.generate-button')) {
return;
}
// Create button and add listener
const btn = document.createElement('button');
btn.className = 'generate-button';
btn.id = 'generate-button';
Expand All @@ -68,6 +68,7 @@ export default {
const parent = e.currentTarget.closest('.form-group');
const selectedInput = parent.querySelector('.input-group')
.querySelector('.input-text');
// Get text language from text field
if (selectedInput.getAttribute('id') === 'field_alt_de') {
language = 'de';
} else if (selectedInput.getAttribute('id') === 'field_alt_fr') {
Expand Down
1 change: 1 addition & 0 deletions src/Controllers/AltTextGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function submitData(Request $request)
echo json_encode($responseData);
}

// Process image data and send request to AltText.ai API
public function generateAltText(string $url, string $textLanguage)
{
$imageDataService = new ImageDataService();
Expand Down
10 changes: 5 additions & 5 deletions src/Services/ImageDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public function processImageUrl(string $url): string
);
$imagePath = $_SERVER['DOCUMENT_ROOT'] . strtr($url, $replacements);

$rawData = $this->encodeImage($imagePath);
return $rawData;
// Image gets converted to base64 string to circumvent browserlock on stage
return $this->encodeImage($imagePath);
}

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

return $rawData;
//Encode the image to base64 and remove the prefix to get raw data
$imageData = base64_encode($image);
return preg_replace('#^data:image/[^;]+;base64,#', '', $imageData);
}
}

0 comments on commit 68fd2f2

Please sign in to comment.