File size: 1.95Kb
<?php
$target_dir = "C:\\xampp\\htdocs\\remove-bg-app\\input\\";
$output_dir = "C:\\xampp\\htdocs\\remove-bg-app\\output\\";
$processed_images = [];
$allowedFormats = ['jpg', 'jpeg', 'png'];
if (isset($_FILES['images'])) {
foreach ($_FILES['images']['name'] as $key => $name) {
$file_extension = pathinfo($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 JPG or PNG images.']);
exit;
}
if ($_FILES['images']['size'][$key] > 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['images']['tmp_name'][$key], $target_file)) {
// Execute the Python script, passing the image file name
$command = escapeshellcmd('python C:\\xampp\\htdocs\\remove-bg-app\\upload_image.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)) {
$processed_images[] = $output_image_path;
}
}
}
}
if (!empty($processed_images)) {
echo json_encode(['success' => true, 'images' => $processed_images]);
} else {
echo json_encode(['success' => false, 'message' => 'Your file format is not correct. Please upload JPG or PNG images.']);
}
} else {
echo json_encode(['success' => false, 'message' => 'No files uploaded.']);
}