This is my HTML code.
<canvas id='sig-canvas' width='' height='100%'></canvas>
This is my ajax call.
function signature(todo) {
var canvas = document.getElementById("sig-canvas");
var pngUrl = canvas.toDataURL("image/png");
var sign = { img: pngUrl };
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
$.ajax({
type: "POST",
url: '/addSignature',
data: sign, // serializes the form's elements.
success: function (data) {
$("#msg").delay(0).fadeIn(300);
},
error: function (data) {
$("#msg").delay(5200).fadeOut(300);
}
});
This is my Controller code.
public function addSignature(Request $request){
if(isset($_POST['img'])){
$new_name = time().'.'.'txt';
$base64 = $_POST['img'];
$data = explode(',', $base64);
$file = base64_decode($data[1]);
$output_file = public_path().'/sign/'.$new_name;
$file->move($output_file, $new_name);
$file = fopen($output_file, "wb");
fwrite($file, $base64);
fclose($file);
$st_id = Auth::user()->id;
$keys = array('sign');
$values = array($new_name);
$data = array_combine($keys, $values);
DB::table("students")->where('st_id',$st_id)->update($data);
}
I want to get a signature on canvas and wants to convert that signature into an image and to upload that canvas image to my public directory, so that I can show that on Dashboard.