withStatus(404, 'Image not found'); } // Get file extension and set appropriate content type $extension = strtolower(pathinfo($fullPath, PATHINFO_EXTENSION)); $contentTypes = [ 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', ]; $contentType = $contentTypes[$extension] ?? 'application/octet-stream'; // Read and serve the file $fileContent = file_get_contents($fullPath); $response = $response->withHeader('Content-Type', $contentType); $response = $response->withHeader('Cache-Control', 'public, max-age=3600'); // Cache for 1 hour $response->getBody()->write($fileContent); return $response; } }