<?php
// image-proxy.php --> This file called when execute image fake url

$imagePath = $_SERVER['REQUEST_URI']; // This provide fake url
$imageName = basename($imagePath); // $imageName contain fake url basename mean file name that is actual
$baseUrl = 'https://app.watify.io/client_media/company_logo/'; //we use this file path to send actual file for fake url
$realImageUrl = $baseUrl . $imageName;

// Initialize cURL
$ch = curl_init($realImageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects

$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

curl_close($ch);

// If image not found
if ($httpCode !== 200) {
    http_response_code(404);
    echo "Image not found.";
    exit;
}

// Serve image with correct content type
header("Content-Type: $contentType");
echo $body;
exit;
?>
