I'm trying to make a function work (trigger) for example canvas2image or could be any function via a jquery ajax call from a mobile. because I don't use a browser I made a cURL page that calls the page with canvas2image.
the page with the function canvas2image sends an ajax request to a page that creates the image as .png and saved s it on the server
so I created 3 pages 1 with cURL 1 with canvas2image and 1 to create and save the image.
below the ajax function that appeals to the cURL
function mediafunction(){
$.ajax({
url: 'http://www.server.com/curl.php',
cache: false,
success: function(response) {
// do something
},
error: function(xhr, error) {
try {
console.debug(xhr);
console.debug(error);
} catch (err) {
alert(err);
}
}
});
}
the cURL code:
$url="http://www.server.com/canvastoimage.php?questid=1";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);
the canvas2image page:
var targetDiv = $("#widget");
html2canvas(targetDiv, {
onrendered: function(canvas) {
var canvasData = canvas.toDataURL("image/png"),
xhr = new XMLHttpRequest();
$.ajax({
url: 'saveimg.php',
type: "POST",
async: false,
//contentType: 'application/json; charset=utf-8',
dataType: "text",
data: { "base64data" : canvasData
},
beforeSend: function () {
},
complete: function (response) {
},
success: function (result) {
result;
},
error: function (request,error) {
//alert(error);
}
});
}
});
the save image page:
if ( isset($_POST["base64data"]) && !empty($_POST["base64data"]) ) {
// get the dataURL
$dataURL = $_POST["base64data"];
// the dataURL has a prefix (mimetype+datatype)
// that we don't want, so strip that prefix off
$parts = explode(',', $dataURL);
$data = $parts[1];
// Decode base64 data, resulting in an image
$data = base64_decode($data);
// create a temporary unique file name
$file = 'mediasave/' . uniqid() . '.png';
// write the file to the upload directory
$success = file_put_contents($file, $data);
// return the temp file name (success)
// or return an error message just to frustrate the user (kidding!)
echo $output = $success ? $file : 'Unable to save this image.';
}
this only works if I visit the cURL page with a browser. I hope you understand my bad explanation, I did not know how to describe it.