36908-vm/ocr/config.php
2025-12-13 11:06:47 +00:00

28 lines
852 B
PHP

<?php
// OCR.space configuration.
// Reads the API key from an environment variable.
function get_ocr_key() {
$key = getenv('OCR_API_KEY');
if ($key === false || $key === null || $key === '') {
// Attempt to load from .env if not in process environment
$envPath = realpath(__DIR__ . '/../../.env');
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
foreach ($lines as $line) {
if (str_starts_with(trim($line), 'OCR_API_KEY')) {
[, $v] = array_map('trim', explode('=', $line, 2));
$key = trim($v, "' ");
break;
}
}
}
}
return ($key === false) ? null : $key;
}
return [
'api_key' => get_ocr_key(),
];