[ 'header' => 'User-Agent: Mozilla/5.0', 'ignore_errors' => true, 'timeout' => 10 // Таймаут 10 секунд ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); $image_data = @file_get_contents($image_url, false, $context); if ($image_data === false) { // Пробуем отдать старый кэш при ошибке if (file_exists($cache_file)) { $content_type = mime_content_type($cache_file); if ($content_type !== false) { header('Content-Type: ' . $content_type); readfile($cache_file); exit; } } header("HTTP/1.0 404 Not Found"); exit('Remote image not found'); } // Определяем Content-Type $content_type = 'application/octet-stream'; if (isset($http_response_header)) { foreach ($http_response_header as $header) { if (stripos($header, 'Content-Type:') === 0) { $content_type = trim(substr($header, 13)); break; } } } // Сохраняем в кэш if (file_put_contents($cache_file, $image_data) === false) { error_log("Failed to write cache file: $cache_file"); } if (file_put_contents($meta_file, json_encode([ 'content_type' => $content_type, 'timestamp' => time(), 'source_url' => $image_url ])) === false) { error_log("Failed to write meta file: $meta_file"); } // Отправляем изображение header('Content-Type: ' . $content_type); echo $image_data; ?>