I have an svg file that is generating a dataURI-png and that works great. And I want that dataURI to be saved as an image, therefore I try to send the dataURI through ajax to another server that can execute PHP. But I can't get it to work.
This is the code for generating the dataURI (that works)
var mySVG = document.querySelector('svg'), // Inline SVG element
tgtImage = document.querySelector('.tgtImage'); // Where to draw the result
can = document.createElement('canvas'), // Not shown on page
ctx = can.getContext('2d'),
loader = new Image; // Not shown on page
console.log(mySVG);
loader.width = can.width = tgtImage.width;
loader.height = can.height = tgtImage.height;
loader.onload = function(){
ctx.drawImage( loader, 0, 0, loader.width, loader.height );
tgtImage.src = can.toDataURL("image/png");
};
This is the ajax-code to send it to the external php-server:
$.ajax({
type: "POST",
data: {id:'testID',datauri: can.toDataURL("image/png")},
crossDomain: true,
//dataType: "jsonp",
url: "https://urltoscript.php",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
The PHP-code to generate the png
$dataUrl = $_REQUEST['datauri'];
$id = $_REQUEST['id'];
list($meta, $content) = explode(',', $dataUrl);
$content = base64_decode($content);
file_put_contents('./tmp-png/'.$id.'.png', $content);
The PNG-generation works when manualy inserting the dataURI. But it doesn't work with the ajax function above.
Thank you!