Last active
November 26, 2020 11:08
-
-
Save manuhabitela/9179019 to your computer and use it in GitHub Desktop.
DrawingBoard: let people draw and save their images server-side https://github.com/Leimi/drawingboard.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//server-side code where we save the given drawing in a PNG file | |
$img = filter_input(INPUT_POST, 'image', FILTER_SANITIZE_URL); | |
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); | |
//see http://j-query.blogspot.fr/2011/02/save-base64-encoded-canvas-image-to-png.html | |
$img = str_replace(' ', '+', str_replace('data:image/png;base64,', '', $img)); | |
$data = base64_decode($img); | |
//create the image png file with the given name | |
file_put_contents(__DIR__.'/path/to/images/folder/'. str_replace(' ', '_', $name) .'.png', $data); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- html page where you can draw, name and submit your creations --> | |
<form class="drawing-form" action="create-drawing.php" method="post"> | |
<!-- this will be the drawingboard container --> | |
<div id="board"> | |
</div> | |
<!-- this will be the input used to pass the drawingboard content to the server --> | |
<input type="hidden" name="image" value=""> | |
<label>Name <input type="text" name="name" /></label> | |
<button>Submit</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//javascript code that intercepts form submission to add drawingboard content to the form data sent to the server | |
var myBoard = new DrawingBoard.Board('board'); | |
$('.drawing-form').on('submit', function(e) { | |
//get drawingboard content | |
var img = myBoard.getImg(); | |
//we keep drawingboard content only if it's not the 'blank canvas' | |
var imgInput = (myBoard.blankCanvas == img) ? '' : img; | |
//put the drawingboard content in the form field to send it to the server | |
$(this).find('input[name=image]').val( imgInput ); | |
//we can also assume that everything goes well server-side | |
//and directly clear webstorage here so that the drawing isn't shown again after form submission | |
//but the best would be to do when the server answers that everything went well | |
myBoard.clearWebStorage(); | |
}); |
It was a rule to prevent a XSS attack that prevented $(this).find('input[name=image]').val( imgInput ); to works and give a 403 error
What do you call to reset the board (set it back to a fresh canvas after saving)
What do you call to reset the board (set it back to a fresh canvas after saving)
Try this,
myBoard.reset({
background: true });`
What do you call to reset the board (set it back to a fresh canvas after saving)
Try this,
myBoard.reset({
background: true });`
Worked!!!
$('#clear').on({ click: function () { imageBoard.reset({background: true}); } });
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It finally worked. I was a mod_security issue. Don't know yet wich one. But if they give me the information I will put it here.