55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
// api/avatar.php
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/config/database.php';
|
|
|
|
// Prevent caching to ensure we get the latest image if changed, though we might want to cache it eventually.
|
|
// Let's add standard caching headers for performance (e.g. cache for 1 day).
|
|
$cache_time = 86400;
|
|
header("Cache-Control: max-age=$cache_time, public");
|
|
|
|
$username = $_GET['username'] ?? '';
|
|
$name = $_GET['name'] ?? 'User';
|
|
|
|
$fallback_url = "https://ui-avatars.com/api/?name=" . urlencode($name) . "&background=random";
|
|
|
|
if (empty($username)) {
|
|
header("Location: $fallback_url");
|
|
exit;
|
|
}
|
|
|
|
$hos_pdo = Database::getHosInstance();
|
|
|
|
if (!$hos_pdo) {
|
|
header("Location: $fallback_url");
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$sql = "SELECT HR_IMAGE FROM hr_person WHERE HR_CID = ? AND HR_STATUS_ID='01'";
|
|
$stmt = $hos_pdo->prepare($sql);
|
|
$stmt->execute([$username]);
|
|
$person = $stmt->fetch();
|
|
|
|
if ($person && !empty($person['HR_IMAGE'])) {
|
|
// Output the BLOB image
|
|
// Try to determine mime type, or default to jpeg
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
$mime = $finfo->buffer($person['HR_IMAGE']);
|
|
|
|
if ($mime === 'application/x-empty' || empty($mime)) {
|
|
$mime = 'image/jpeg';
|
|
}
|
|
|
|
header("Content-Type: $mime");
|
|
echo $person['HR_IMAGE'];
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
// Silently fall back to UI Avatars on error
|
|
}
|
|
|
|
// If no image or error, redirect to fallback
|
|
header("Location: $fallback_url");
|
|
exit;
|