Path : /home/vishqocm/pcib.in/
File Upload :
Current File : //home/vishqocm///pcib.in/setup_images.php

<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set time limit to allow for downloads - increase to 600 seconds
set_time_limit(600);

// Start session
session_start();

// Include database configuration
require_once __DIR__ . '/admin/database/db_config.php';

echo "<h1>Setting up sample images for your website</h1>";

// Create directories if they don't exist
$directories = [
    'assets/images',
    'assets/images/courses',
    'assets/images/testimonials',
    'assets/images/team',
    'assets/images/blog',
    'assets/images/hero'
];

foreach ($directories as $dir) {
    if (!file_exists($dir)) {
        if (mkdir($dir, 0755, true)) {
            echo "<p>Created directory: $dir</p>";
        } else {
            echo "<p style='color: red;'>Failed to create directory: $dir</p>";
        }
    } else {
        echo "<p>Directory already exists: $dir</p>";
    }
}

// Function to download an image with timeout handling
function downloadImage($url, $path) {
    // Check if file already exists to avoid re-downloading
    if (file_exists($path)) {
        echo "<p style='color: blue;'>File already exists: $path</p>";
        return true;
    }
    
    // Use local placeholder if URL is from placehold.co
    if (strpos($url, 'placehold.co') !== false) {
        // Extract dimensions and colors from URL
        preg_match('/(\d+)x(\d+)\/([^\/]+)\/([^\/\?]+)/', $url, $matches);
        if (count($matches) >= 5) {
            $width = $matches[1];
            $height = $matches[2];
            $bgColor = $matches[3];
            $textColor = $matches[4];
            
            // Create a simple placeholder image
            $im = imagecreatetruecolor($width, $height);
            
            // Convert hex to RGB
            $bgRGB = sscanf($bgColor, "%2x%2x%2x");
            $textRGB = sscanf($textColor, "%2x%2x%2x");
            
            // Create colors
            $bg = imagecolorallocate($im, $bgRGB[0] ?? 77, $bgRGB[1] ?? 115, $bgRGB[2] ?? 223); // Default to #4e73df if parsing fails
            $text = imagecolorallocate($im, $textRGB[0] ?? 255, $textRGB[1] ?? 255, $textRGB[2] ?? 255); // Default to white
            
            // Fill background
            imagefilledrectangle($im, 0, 0, $width, $height, $bg);
            
            // Add text (extract from URL if available)
            preg_match('/text=([^&]+)/', $url, $textMatches);
            $displayText = count($textMatches) > 1 ? str_replace('+', ' ', $textMatches[1]) : basename($path);
            
            // Center text
            $font = 5; // Built-in font
            $textWidth = imagefontwidth($font) * strlen($displayText);
            $textHeight = imagefontheight($font);
            $x = ($width - $textWidth) / 2;
            $y = ($height - $textHeight) / 2;
            
            imagestring($im, $font, $x, $y, $displayText, $text);
            
            // Save image
            $result = imagepng($im, $path);
            imagedestroy($im);
            
            if ($result) {
                echo "<p style='color: green;'>Created local placeholder image: $path</p>";
                return true;
            } else {
                echo "<p style='color: red;'>Failed to create local placeholder image: $path</p>";
                return false;
            }
        }
    }
    
    // If not a placeholder or couldn't parse, try downloading with curl
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 10 seconds connection timeout
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 30 seconds total timeout
    $data = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    
    if ($error) {
        echo "<p style='color: red;'>Error downloading $url: $error</p>";
        return false;
    }
    
    if (file_put_contents($path, $data)) {
        echo "<p style='color: green;'>Downloaded image to: $path</p>";
        return true;
    } else {
        echo "<p style='color: red;'>Failed to save image to: $path</p>";
        return false;
    }
}

// Sample images to download
$images = [
    // Logo
    [
        'url' => 'https://placehold.co/200x60/4e73df/ffffff?text=YourLogo',
        'path' => 'assets/images/logo.png',
        'type' => 'Logo'
    ],
    // Hero images
    [
        'url' => 'https://placehold.co/1920x800/1cc88a/ffffff?text=Hero+Image+1',
        'path' => 'assets/images/hero/hero1.jpg',
        'type' => 'Hero'
    ],
    [
        'url' => 'https://placehold.co/1920x800/36b9cc/ffffff?text=Hero+Image+2',
        'path' => 'assets/images/hero/hero2.jpg',
        'type' => 'Hero'
    ],
    // Course images
    [
        'url' => 'https://placehold.co/600x400/4e73df/ffffff?text=Course+1',
        'path' => 'assets/images/courses/course1.jpg',
        'type' => 'Course'
    ],
    [
        'url' => 'https://placehold.co/600x400/1cc88a/ffffff?text=Course+2',
        'path' => 'assets/images/courses/course2.jpg',
        'type' => 'Course'
    ],
    [
        'url' => 'https://placehold.co/600x400/36b9cc/ffffff?text=Course+3',
        'path' => 'assets/images/courses/course3.jpg',
        'type' => 'Course'
    ],
    [
        'url' => 'https://placehold.co/600x400/f6c23e/ffffff?text=Course+4',
        'path' => 'assets/images/courses/course4.jpg',
        'type' => 'Course'
    ],
    // Team members
    [
        'url' => 'https://placehold.co/300x300/4e73df/ffffff?text=Team+1',
        'path' => 'assets/images/team/team1.jpg',
        'type' => 'Team'
    ],
    [
        'url' => 'https://placehold.co/300x300/1cc88a/ffffff?text=Team+2',
        'path' => 'assets/images/team/team2.jpg',
        'type' => 'Team'
    ],
    [
        'url' => 'https://placehold.co/300x300/36b9cc/ffffff?text=Team+3',
        'path' => 'assets/images/team/team3.jpg',
        'type' => 'Team'
    ],
    // Testimonials
    [
        'url' => 'https://placehold.co/150x150/4e73df/ffffff?text=User+1',
        'path' => 'assets/images/testimonials/user1.jpg',
        'type' => 'Testimonial'
    ],
    [
        'url' => 'https://placehold.co/150x150/1cc88a/ffffff?text=User+2',
        'path' => 'assets/images/testimonials/user2.jpg',
        'type' => 'Testimonial'
    ],
    [
        'url' => 'https://placehold.co/150x150/36b9cc/ffffff?text=User+3',
        'path' => 'assets/images/testimonials/user3.jpg',
        'type' => 'Testimonial'
    ],
    // Blog posts
    [
        'url' => 'https://placehold.co/800x500/4e73df/ffffff?text=Blog+Post+1',
        'path' => 'assets/images/blog/post1.jpg',
        'type' => 'Blog'
    ],
    [
        'url' => 'https://placehold.co/800x500/1cc88a/ffffff?text=Blog+Post+2',
        'path' => 'assets/images/blog/post2.jpg',
        'type' => 'Blog'
    ],
    [
        'url' => 'https://placehold.co/800x500/36b9cc/ffffff?text=Blog+Post+3',
        'path' => 'assets/images/blog/post3.jpg',
        'type' => 'Blog'
    ]
];

// Download images
echo "<h2>Downloading Images</h2>";
$success_count = 0;
$failed_count = 0;

// Process images in smaller batches to avoid timeout
$batch_size = 5;
$total_images = count($images);
$batches = ceil($total_images / $batch_size);

for ($batch = 0; $batch < $batches; $batch++) {
    echo "<h3>Processing Batch " . ($batch + 1) . " of $batches</h3>";
    
    // Get current batch of images
    $start = $batch * $batch_size;
    $end = min($start + $batch_size, $total_images);
    
    for ($i = $start; $i < $end; $i++) {
        $image = $images[$i];
        echo "<h4>Processing {$image['type']} Image</h4>";
        
        // Reset time limit for each image to prevent timeout
        set_time_limit(60);
        
        if (downloadImage($image['url'], $image['path'])) {
            $success_count++;
        } else {
            $failed_count++;
        }
        
        // Flush output buffer to show progress
        flush();
        ob_flush();
    }
}

echo "<h2>Image Setup Summary</h2>";
echo "<p>Successfully downloaded/created: $success_count images</p>";
echo "<p>Failed to download/create: $failed_count images</p>";

// Create a CSS file with image references
$css_content = <<<CSS
/* Sample CSS for images */
.logo {
    background-image: url('../images/logo.png');
    background-size: contain;
    background-repeat: no-repeat;
}

.hero-section {
    background-image: url('../images/hero/hero1.jpg');
    background-size: cover;
    background-position: center;
}

.course-1 {
    background-image: url('../images/courses/course1.jpg');
}

.course-2 {
    background-image: url('../images/courses/course2.jpg');
}

.course-3 {
    background-image: url('../images/courses/course3.jpg');
}

.course-4 {
    background-image: url('../images/courses/course4.jpg');
}

.team-member-1 {
    background-image: url('../images/team/team1.jpg');
}

.team-member-2 {
    background-image: url('../images/team/team2.jpg');
}

.team-member-3 {
    background-image: url('../images/team/team3.jpg');
}

.testimonial-1 {
    background-image: url('../images/testimonials/user1.jpg');
}

.testimonial-2 {
    background-image: url('../images/testimonials/user2.jpg');
}

.testimonial-3 {
    background-image: url('../images/testimonials/user3.jpg');
}

.blog-post-1 {
    background-image: url('../images/blog/post1.jpg');
}

.blog-post-2 {
    background-image: url('../images/blog/post2.jpg');
}

.blog-post-3 {
    background-image: url('../images/blog/post3.jpg');
}
CSS;

if (file_put_contents('assets/css/images.css', $css_content)) {
    echo "<p style='color: green;'>Created CSS file with image references: assets/css/images.css</p>";
} else {
    echo "<p style='color: red;'>Failed to create CSS file</p>";
}

echo "<h2>Next Steps</h2>";
echo "<p>1. Include the CSS file in your HTML:</p>";
echo "<pre>&lt;link rel='stylesheet' href='assets/css/images.css'&gt;</pre>";
echo "<p>2. Use the CSS classes in your HTML elements:</p>";
echo "<pre>&lt;div class='hero-section'&gt;...&lt;/div&gt;</pre>";
echo "<p>3. Or use the images directly in your HTML:</p>";
echo "<pre>&lt;img src='assets/images/logo.png' alt='Logo'&gt;</pre>";

echo "<h2>Navigation</h2>";
echo "<p><a href='index.php' style='background-color: #4e73df; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px; margin-right: 10px;'>Go to Homepage</a>";
echo "<a href='admin/check_dashboard.php' style='background-color: #1cc88a; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px;'>Check Admin Dashboard</a></p>";
?>