More Detailed background on this question is here, and I have attempted to look at the answer linked in that thread, which is where I am stuck.
A have created a PHP file that loads via ajax inside a div element in another PHP file via ajax calls shown in that other thread. The PHP file uses a database pull and html with chart.js to make a chart canvas, and I would like to save it as an image on the server.
Below is an example of code for what I'm trying to do.
// html stuff to make a html canvas is above here.
// PHP stuff to take post data, get data from database and insert it into barChartData and chartOptions vars is above here. (these two vars contain all the chart.js config stuff and the chart works.)
var ctx = document.getElementById("Cloudbar1").getContext("2d");
Chart.defaults.global.defaultFontColor = 'black';
Chart.defaults.global.defaultFontFamily = "Arial, sans-serif";
Chart.defaults.global.defaultFontSize = 16;
window.myBar = new Chart(ctx, {
type: "bar",
data: barChartData,
options: chartOptions
});
//convert canvas element to a url format.
var dataURL = ctx.toDataURL();
//now send the file to the server with yet another ajax call.
$.ajax({
type: "POST",
url: "savecanvasimgtoserver.php",
data: {
imgBase64: dataURL
}
})
When trying to run this, I'm getting a console error saying
"Uncaught TypeError: ctx.toDataURL is not a function"
I don't think it matters as I've not got this far in the code for the above error yet, but the php file the above code points to just contains:
$img = $_POST['data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
//saving
$fileName = 'chart.png';
file_put_contents($fileName, $fileData);
Taken from this question.
From what I know, when you get a 'x is not a function' when it is a function, that means that the thing your trying to put into the function is in some way invalid, but 'ctx' is defiantly the canvas element and works in this case.