getMessage() . "\n"; } echo "\n"; // Method 2: Using cURL with detailed debugging echo "Using cURL with detailed debugging\n"; try { $tempFile = 'tmp/test_image_debug.jpg'; if (!function_exists('curl_init')) { echo "FAILED: cURL is not installed\n"; } else { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $testImageUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5, CURLOPT_TIMEOUT => 30, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', CURLOPT_HEADER => 1, // Include headers in the response CURLOPT_VERBOSE => true, ]); // Create a stream for the verbose output $verbose = fopen('tmp/curl_verbose.log', 'w+'); curl_setopt($ch, CURLOPT_STDERR, $verbose); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $error = curl_error($ch); // Extract headers and body $headers = substr($response, 0, $headerSize); $imageData = substr($response, $headerSize); curl_close($ch); // Close and read the verbose log rewind($verbose); $verboseLog = stream_get_contents($verbose); fclose($verbose); echo "HTTP Status Code: $httpCode\n"; echo "Content Type: $contentType\n"; echo "Headers:\n$headers\n"; if ($httpCode !== 200 || empty($imageData)) { echo "FAILED: HTTP Error $httpCode - $error\n"; } else { file_put_contents($tempFile, $imageData); $fileSize = filesize($tempFile); echo "Downloaded " . number_format($fileSize) . " bytes to $tempFile\n"; // Check if it's a valid image $imageInfo = @getimagesize($tempFile); if ($imageInfo) { echo "SUCCESS: Valid image detected\n"; echo "Image dimensions: " . $imageInfo[0] . 'x' . $imageInfo[1] . ", type: " . $imageInfo['mime'] . "\n"; } else { echo "WARNING: The downloaded file is not a valid image\n"; // Analyze the first 100 bytes of the content echo "First 100 bytes of the content:\n"; $contentPreview = bin2hex(substr($imageData, 0, 50)); echo chunk_split($contentPreview, 2, ' ') . "\n"; // Show first 100 characters if it looks like text echo "Content as text (first 100 chars):\n"; $textPreview = substr($imageData, 0, 100); echo preg_replace('/[^\x20-\x7E]/', '.', $textPreview) . "\n"; } echo "\nCURL Verbose Log:\n$verboseLog\n"; } } } catch (Exception $e) { echo "FAILED: Exception occurred: " . $e->getMessage() . "\n"; } // Try a publicly accessible image as a reference test echo "\n\nTesting with a public reference image...\n"; $publicImageUrl = 'https://www.php.net/images/logos/new-php-logo.svg'; echo "Public Image URL: $publicImageUrl\n\n"; try { $tempFile = 'tmp/test_reference_image.jpg'; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $publicImageUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5, CURLOPT_TIMEOUT => 30, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', ]); $imageData = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); echo "HTTP Status Code: $httpCode\n"; echo "Content Type: $contentType\n"; if ($httpCode !== 200 || empty($imageData)) { echo "FAILED: Could not download reference image\n"; } else { file_put_contents($tempFile, $imageData); $fileSize = filesize($tempFile); echo "Downloaded " . number_format($fileSize) . " bytes to $tempFile\n"; // Check if it's a valid image $imageInfo = @getimagesize($tempFile); if ($imageInfo) { echo "SUCCESS: Valid image detected\n"; echo "Image dimensions: " . $imageInfo[0] . 'x' . $imageInfo[1] . ", type: " . $imageInfo['mime'] . "\n"; } else { echo "WARNING: The downloaded file is not a valid image\n"; } } } catch (Exception $e) { echo "FAILED: Exception occurred: " . $e->getMessage() . "\n"; } echo "\nTest completed\n";