forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetHeroImage.php
More file actions
68 lines (55 loc) · 1.94 KB
/
GetHeroImage.php
File metadata and controls
68 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
// GetHeroImage.php - Fetch hero cropped image and return as base64
// This allows the frontend to bypass CORS restrictions by proxying through the backend
header('Content-Type: application/json');
try {
// Get the hero name from query parameter
$heroName = isset($_GET['hero']) ? $_GET['hero'] : null;
if (!$heroName) {
http_response_code(400);
echo json_encode(['error' => 'Hero name is required']);
exit;
}
// Validate hero name to prevent directory traversal
if (preg_match('/[^a-zA-Z0-9_-]/', $heroName)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid hero name']);
exit;
}
// Construct the image URL
$imageUrl = "https://images.talishar.net/public/crops/{$heroName}_cropped.webp";
// Fetch the image with a timeout
$context = stream_context_create([
'http' => [
'timeout' => 10,
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
]
]);
$imageData = @file_get_contents($imageUrl, false, $context);
if ($imageData === false) {
http_response_code(404);
echo json_encode(['error' => 'Failed to fetch image']);
exit;
}
// Convert to base64
$base64Image = base64_encode($imageData);
// Determine MIME type
$mimeType = 'image/png';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo) {
$mimeType = finfo_buffer($finfo, $imageData) ?: 'image/png';
finfo_close($finfo);
}
// Return as data URL
$dataUrl = "data:{$mimeType};base64,{$base64Image}";
http_response_code(200);
echo json_encode(['dataUrl' => $dataUrl]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => 'Server error: ' . $e->getMessage()]);
}
?>