Wygląda jakby plik był zakodowany base64 (funkcja base64_encode w PHP), dorzucony był do niego URL schema "data:", ale o dziwo bez deklaracji typu (czyli wystarczy, że dokleisz prefix "data:" z przodu), a całość była opakowana w JSON jako array.
GPT-4 mówi że kod wyglądałby tak:
function fileToJsonBase64($fileName) {
// Check if file exists and is readable
if (!file_exists($fileName) || !is_readable($fileName)) {
return json_encode(['error' => 'File not found or not readable']);
}
// Read file content
$fileContent = file_get_contents($fileName);
// Encode file content as base64
$base64Content = base64_encode($fileContent);
// Add data: prefix
// [tutaj byćmoże trzeba będzie trochę zmienić, tj usunąc typ i prefix base64 //gynvael]
$dataUrl = 'data:' . mime_content_type($fileName) . ';base64,' . $base64Content;
// Add the result to an array under the key "file"
$result = ['file' => $dataUrl];
// Return JSON encoded string
return json_encode($result);
}
Przykład użycia:
$fileName = "example.png";
$jsonResult = fileToJsonBase64($fileName);
echo $jsonResult;
Prompt:
Write a PHP function that takes a file name as an argument. Then it reads the file content, encodes it as base64 and adds a "data:" prefix to that. The result should be then added to an array under the key "file", and return as a JSON encoded string.