简体   繁体   English

如何将html2canvas图像保存到我的项目图像文件夹中?

[英]How to save html2canvas image to my project images folder?

I am not sure how to specify the path for the file to be save (like ../images/screenshot.png)I tried saving the image like 我不确定如何指定要保存文件的路径(例如../images/screenshot.png)

html2canvas($my-div, {
    useCORS: true,
    allowTaint: true,
    onrendered: function (canvas) {
      var img = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
      location.href = img;
   }
});

This gets downloaded to my system downloads.How do I save this to the folder inside the project (want to specify the path I want to save the file)? 这将下载到我的系统下载文件中。如何将其保存到项目内的文件夹中(想要指定要保存文件的路径)?

You have to use some library like Canvas2Image to download image. 您必须使用诸如Canvas2Image之类的来下载图像。 User will specify image path by it self (you don't know where to save it, because there may be no such directory as 'C:\\Users{SomeUsername}\\Downloads' 用户将自行指定图像路径(您不知道将其保存在哪里,因为可能没有诸如“ C:\\ Users {SomeUsername} \\ Downloads”的目录

html2canvas($my-div, {
    onrendered: function(canvas) {
        return Canvas2Image.saveAsPNG(canvas);
    }
});

Finally solved this problem. 终于解决了这个问题。 The plugin that helped me to save canvas to image in my local storage is canvas2image plugin from devgeeks . 帮助我将画布保存到本地存储中的图像的插件devgeeks的canvas2image插件 Directly pass your canvas as parameter to the save function of plugin which will save that canvas as image to your local storage. 直接将画布作为参数传递给插件的保存功能,该功能会将画布作为图像保存到本地存储。

    html2canvas($my-div, {
    height: myDivHeight,
    width: 350,
    useCORS: true,
    allowTaint: true,
    proxy: "your url",
    onrendered: function (canvas) {
      window.canvas2ImagePlugin.saveImageDataToLibrary(
        function(msg){
          console.log(msg) //path where image is saved
        },
        function(err){
            console.log(err);
        },
        canvas // pass canvas as third parameter
      );
    }
  });

I used html2canvas library to captured the current screen and used canvas2Image plugin to save it to my local storage. 我使用html2canvas库捕获当前屏幕,并使用canvas2Image插件将其保存到本地存储中。 Cheers!!! 干杯!!!

This is the way of screenshot upload 这是截图上传的方式

Step 01: 步骤01:

include html2canvas CDN inside your html file 在您的html文件中包含html2canvas CDN

Step 02: 步骤02:

  <form id="imgupload" enctype="multipart/form-data">
     <input type="hidden" name="imagename" id="imghidden">
    </form>

    <canvas id=img>
      <img src="....">
    </canvas> 

    <button id="imgupload"></button>

Step 03: 步骤03:

  $("#imgupload").click(function(){
  html2canvase($("#img"),{
  onrendered:function(canvas)
  {
   var canvasinfo=canvas;
   $(#imghidden).val(canvasinfo.toDataURL('image/png'));
   var formid=$("#imgupload")[0];
   var data=new FormData(formid);

   $.ajax({
               type:"post",
               url:"dataSave.php", 
               data:data,
               contentType: false,
               cache: false,
               processData:false,
               success:function (data) {
                   console.log(data);
               },error:function(error)
               {
                   console.log(error);
               }
           });
  }

});
});

Step 04: 步骤04:

Put this code inside the dataSave.php file 将此代码放入dataSave.php文件中

<?php
    $filteredData=substr($POST['imghidden'],strpos($_POST['imghidden'],',')+1);
    $DecodeData=base64_decode($filteredData);

    //Now you can upload image inside the folder
    $imgPath='img'.time().'.png';
    file_put_contents($imgPath,$DecodeData);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM