jquery - saving canvas in php but no data in image file -


following code trying save canvas on machine . in folder image file created of 0kb .

on click of download button setting value in text , on next php page reading value $_post['img_name']; .

jquery ::

$("#download").click(function() {     console.log('download clicked');     //alert('download clicked');     var myval = canvas.todataurl();     $('#img_txt').val(myval); }); 

php code ::

<?php $myval = $_post['img_name']; echo "string ".$myval;  ?>  <?php $data = base64_decode($_post['img_name']);  /* name file want */ $filename = 'my_image.png';  /* remove 1st line of base64 string */  /* note: replace '$base64' own variable */ $img = str_replace('data:image/png;base64,', '', $data);   /* replace spaces */ $img = str_replace(' ', '+', $img);  /* decode string */ $data = base64_decode($img);  /* full path upload directory @ end filename */ $file = 'd:/upload/'.$filename;  /* save, make sure directory writeable! */ file_put_contents($file, $data); ?> 

i have check $_post['img_name'] getting base64 string ? problem in code?

your jquery isn't sending .php

try this:

// convert canvas base64 data  var dataurl=canvas.todataurl();  // call upload.php , post data  $.ajax({   type: "post",   url: "upload.php",   data: {image: dataurl} }).done(function( respond ) {  console.log("saved filename: "+respond); }); 

your upload file should remove datatype prefix before decoding.

try upload.php:

<?php  if ( isset($_post["image"]) && !empty($_post["image"]) ) {       // image data     $data = $_post['image'];      // remove prefix     $uri =  substr($data,strpos($data,",") 1);      // create filename new image     $file = md5(uniqid()) . '.png';      // decode image data , save file     file_put_contents($file, base64_decode($uri));      // return filename     echo $file;  } 

also, make sure php server configured:

  • assign upload directory
  • give write permission upload directory

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -