View file Remove image backgrounds/remove-bg/light/save_combined_image_fashion.php

File size: 1.3Kb
<?php
// Directory where you want to save the combined image
$output_dir = "C:\\xampp\\htdocs\\remove-bg-app\\output\\";

// Ensure the directory exists
if (!is_dir($output_dir)) {
    mkdir($output_dir, 0777, true);
}

// Get the POST data
$data = json_decode(file_get_contents('php://input'), true);

if (isset($data['image'])) {
    $image_data = $data['image'];

    // Remove base64 headers
    $image_data = str_replace('data:image/png;base64,', '', $image_data);
    $image_data = str_replace(' ', '+', $image_data);

    // Decode the base64 image
    $decoded_image = base64_decode($image_data);

    // Generate a unique file name
    $file_name = uniqid() . '.png';
    $file_path = $output_dir . $file_name;

    // Save the decoded image to the file path
    if (file_put_contents($file_path, $decoded_image)) {
        // Return the file path relative to the web root for downloading
        echo json_encode([
            'success' => true,
            'filePath' => '/remove-bg-app/output/' . $file_name  // Adjust this to be relative to your web root
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to save the image.']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'No image data provided.']);
}
?>