194 lines
6.8 KiB
PHP
194 lines
6.8 KiB
PHP
<?php
|
|
// Test script for image downloading capabilities
|
|
|
|
// Display errors
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Create necessary directories
|
|
$directories = [
|
|
'tmp',
|
|
'images',
|
|
'images/products',
|
|
'log'
|
|
];
|
|
|
|
echo "Setting up test directories...\n";
|
|
foreach ($directories as $dir) {
|
|
if (!file_exists($dir)) {
|
|
if (mkdir($dir, 0777, true)) {
|
|
echo "Created directory: $dir\n";
|
|
} else {
|
|
echo "Failed to create directory: $dir\n";
|
|
}
|
|
} else {
|
|
echo "Directory exists: $dir\n";
|
|
}
|
|
chmod($dir, 0777);
|
|
}
|
|
|
|
// Define test image URL
|
|
$testImageUrl = 'https://dropship.ecomzone.eu/storage/products/2023/10/05/2442_911_01_Xmas_bag_2_6BFQAO9yv8.jpg';
|
|
|
|
echo "\nTesting image download...\n";
|
|
echo "Image URL: $testImageUrl\n\n";
|
|
|
|
// Method 1: Using file_get_contents
|
|
echo "Method 1: Using file_get_contents\n";
|
|
try {
|
|
$tempFile = 'tmp/test_image_1.jpg';
|
|
$imageData = @file_get_contents($testImageUrl);
|
|
|
|
if ($imageData === false) {
|
|
echo "FAILED: Could not download image using file_get_contents\n";
|
|
echo "Error: " . error_get_last()['message'] . "\n";
|
|
} else {
|
|
file_put_contents($tempFile, $imageData);
|
|
$fileSize = filesize($tempFile);
|
|
echo "SUCCESS: Downloaded " . number_format($fileSize) . " bytes to $tempFile\n";
|
|
|
|
// Check if it's a valid image
|
|
$imageInfo = getimagesize($tempFile);
|
|
if ($imageInfo) {
|
|
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 "\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";
|