<?php
if (isset($_POST['image'])) {
    $data = $_POST['image'];

    // Remove the header (data:image/png;base64,...)
    $data = str_replace('data:image/png;base64,', '', $data);
    $data = str_replace(' ', '+', $data);
    $decodedData = base64_decode($data);

    // Save the image
    $fileName = 'signature_' . time() .rand(1000, 9999) . '.png';
    $filePath = 'signatures/' . $fileName;

    if (!is_dir('signatures')) {
        mkdir('signatures', 0777, true);
    }

    if (file_put_contents($filePath, $decodedData)) {
        echo "$fileName";
    } else {
        echo '0';
    }
} else {
    echo 'No image data received';
}
?>