View file Remove image backgrounds/remove-bg/light/fashion-photo-creator-color.php

File size: 1.68Kb
<?php
$target_dir = "C:\\xampp\\htdocs\\remove-bg-app\\input\\";
$output_dir = "C:\\xampp\\htdocs\\remove-bg-app\\output\\";
$allowedFormats = ['jpg', 'jpeg', 'png'];

if (isset($_FILES['image'])) {
    $file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);

    // Check if the file format is allowed
    if (!in_array(strtolower($file_extension), $allowedFormats)) {
        echo json_encode(['success' => false, 'message' => 'Your file format is not correct. Please upload a JPG or PNG image.']);
        exit;
    }

    if ($_FILES['image']['size'] > 0) {
        $uploaded_file = uniqid('uploaded_', true) . '.' . $file_extension;
        $target_file = $target_dir . basename($uploaded_file);

        // Move the uploaded file to the input folder
        if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
            // Execute the Python script, passing the image file name
            $command = escapeshellcmd('python C:\\xampp\\htdocs\\remove-bg-app\\fashion-photo-creator-color.py ' . escapeshellarg($uploaded_file));
            $output = shell_exec($command);

            // Check if the output image exists
            $output_image_path = 'output/' . pathinfo($uploaded_file, PATHINFO_FILENAME) . '-output.png';
            if (file_exists("C:\\xampp\\htdocs\\remove-bg-app\\" . $output_image_path)) {
                echo json_encode(['success' => true, 'imagePath' => $output_image_path]);
                exit;
            }
        }
    }

    echo json_encode(['success' => false, 'message' => 'Image processing failed.']);
} else {
    echo json_encode(['success' => false, 'message' => 'No file uploaded.']);
}
?>