I want to save canvas as PNG, without opening it in a new window as base64-encoded image.
I used this code:
jQuery(\"#btnPreview\").click(function(){
In script.js file
$(document).on('click','#btnPreview',function(){
var img =$scope.canvas.toDataURL('image/png');
$.ajax({
type: "POST",
url: 'ajax.php',
data: {'img':img},
success: function(data) {
$("#loader_message").html("Image saved successfully");
}
});
});
In ajax.php
$encodedData=explode(',', $_POST["img"]);
$data = base64_decode($encodedData[1]);
$urlUploadImages = $_SERVER['DOCUMENT_ROOT'].$projName.'/images/canvas/';
$nameImage = "test.png";
$img = imagecreatefromstring($data);
if($img) {
imagepng($img, $urlUploadImages.$nameImage, 0);
imagedestroy($img);
echo "OK";
}
else {
echo 'ERROR';
}