feat(og-image.php): proxy open graph + meta images to mitigate cases where query params required by cdn may break requests

This commit is contained in:
Cory Dransfeldt 2025-05-07 16:31:32 -07:00
parent b6dab4831f
commit 4cefdee788
No known key found for this signature in database
16 changed files with 59 additions and 16 deletions

40
api/og-image.php Normal file
View file

@ -0,0 +1,40 @@
<?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");
echo $image;
} else {
error_log("Invalid content type: $contentType");
header("Location: /404", true, 302);
exit;
}