Search code examples
javascriptphphtmlhtml5-canvashtml2canvas

want to change the path of the saving images folder... how to do this


javascript function to call the html2 canvas element to scrrenshot the the target div

<script type="text/javascript">
function capture() {
    $('#target').html2canvas({
        onrendered: function (canvas) {
            //Set hidden field's value to image data (base-64 string)
            $('#img_val').val(canvas.toDataURL("image/png"));
            //Submit the form manually
            document.getElementById("myForm").submit();
        }
    });
}
</script>

php code

<?php
     //Get the base-64 string from data
     $filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);

   //Decode the string
   $unencodedData=base64_decode($filteredData);
   //echo $unencodedData;
   //Save the image

    $date = date_create();
    $timestamp= date_timestamp_get($date);
    echo $timestamp;
    $rand = mt_rand(100000,999999);
    echo $rand; 
   $string = "cp-string";
   echo $string;
   $name = "$string.$timestamp.$rand.png";
 //$name = "$string/$timestamp/$rand.png";
 //$unencodedData = "$string/$timestamp/$rand";


 file_put_contents($name , $unencodedData);

 ?>

Dont know how to give different folders path for saving the image


Solution

  • Normally use copy function for upload file into a folder. according to your code you can do something like

    $dir_to_save = "foldername"
    if (!is_dir($dir_to_save)) {
      mkdir($dir_to_save);
    }
    
    $path = $dir_to_save."/".$name;
    file_put_contents($path, $unencodedData);
    

    NOTE: use ABSOLUTE_PATH not relative paths

    For more information