0

hello there i'm trying to add the .toDataURL() from the canvas to the wordpress library, but i'm very stuck. Here is my code:

function take_screenshot(e){
    var ajaxurl = e.getAttribute("data-url");
    var canvas = document.getElementById('threeD-canvas');
    var dataURL = canvas.toDataURL();

    $.ajax({
        url : ajaxurl,
        type : 'POST',
        data : {
            action: 'screenshot',
            data : dataURL      
        },
        error : function( response ){
            console.log(response);
        },
        success : function( response ){
            console.log(response);
        }

    });
}

and here is the screenshot processing on the wordpress side of things

add_action( 'wp_ajax_nopriv_screenshot', 'screenshot' );
add_action( 'wp_ajax_screenshot', 'screenshot' );

function screenshot(){
    $upload_dir = wp_upload_dir();
    $imgstring = $_POST['data'];
    $dir = getcwd(); // get the current working directory
    $now = date("U");
    $imgstring = base64_decode($imgstring);
    file_put_contents("$dir/wp-content/uploads/image-$now.png", $imgstring);
}

when i log the success message it just prints the html of the calling page, which is really strange, i also can't find the image in the folder.

I've tried other methods, like this one Saving data-URI to media library but it also didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.