coryd.dev/api/og-image.php

41 lines
1.2 KiB
PHP

<?php
$id = $_GET['id'] ?? null;
$class = $_GET['class'] ?? null;
$extension = $_GET['extension'] ?? 'png';
$isValidId = is_string($id) && preg_match('/^[a-f0-9\-]{36}$/', $id);
if (!$isValidId || !$class) {
header("Location: /404", true, 302);
exit;
}
$cdnUrl = "https://cdn.coryd.dev/$id.$extension?class=$class";
$ch = curl_init($cdnUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$image = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
error_log("CDN response code: $httpCode, Content-Type: $contentType");
if ($httpCode !== 200 || $image === false) {
error_log("Failed to fetch image: $cdnUrl");
header("Location: /404", true, 302);
exit;
}
if (strpos($contentType, 'image/') === 0) {
header("Content-Type: $contentType");
header("Cache-Control: public, max-age=86400");
echo $image;
} else {
error_log("Invalid content type: $contentType");
header("Location: /404", true, 302);
exit;
}