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

File size: 1.16Kb
<?php
// Directory where the image will be saved
$output_dir = '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 the base64 header
    $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 success response with the file path
        echo json_encode([
            'success' => true,
            'filePath' => $file_path
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to save image']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'No image data provided']);
}
?>