dregduc63699 2014-03-17 02:10
浏览 104
已采纳

将画布图像调用到另一个div或页面

Can anybody help me with this I'm solving this problem for almost a week and never get success. I want to happen is to call the image that was been created the image goes to "image" in a div and I want to call that image to the another page but No image show. Please take a look my code.

     <script>
          function myFunction(){
          html2canvas([document.getElementById('card-container')], {
          onrendered: function (canvas) {

         var data = canvas.toDataURL('image/png');


 // AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server

          var image = new Image();
          image.src =  data;

         document.getElementById('image').appendChild(image);

         }
          });
         }
          </script>

          <button onclick="myFunction()" >Try it</button>

          <div id="image">
          <p>Image:</p>
          </div>

<form enctype="multipart/form-data" action="sample.com" method="post" onsubmit="this.divcontent.value = document.getElementById('image').innerHTML;" >
            <input type="hidden" name="image" id="divcontent" value="" />
            <input type="submit" value="Submit" />
         </form>

Here is the ready-image.php

<?php
$img = $_POST['divcontent'];
echo "<img src='$img' alt='image' />";

 define('UPLOAD_DIR', 'wp-content/uploads/2014/03/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';   
           ?>

Please help me guys. Thank you very much.

  • 写回答

1条回答 默认 最新

  • duafagm1066 2014-03-17 02:55
    关注

    I am not sure if this is what you want. Since the ajax part is missing from your code, I am just trying to pass the image captured to the ready-image.php and display it.

    I am assuming that the codes are on 2 separate files.

    <script>
        function myFunction() {
            html2canvas([document.getElementById('card-container')], {
                onrendered: function (canvas) {
                    var data = canvas.toDataURL('image/png');
                    var image = new Image();
                    image.src =  data;
                    document.getElementById('image').appendChild(image);
                    $("#divcontent").val(data); //save the image base64 string to a hidden input
                }
            });
        }
    </script>
    
    <div id="card-container">
        <button onclick="myFunction()" >Try it</button>
    
        <div id="image">
            <p>Image:</p>
        </div>
    
        <form action="ready-image.php" method="POST">
            <input type="hidden" name="divcontent" id="divcontent" />
            <input type="submit" value="Submit" />
        </form>
    </div>
    

    ready-image.php:

    <?php
        $img = $_POST['divcontent'];
        echo "<img src='$img' alt='image' />";
    
        define('UPLOAD_DIR', 'wp-content/uploads/2014/03/');
    
        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $file = UPLOAD_DIR . uniqid() . '.png';
        $success = file_put_contents($file, $data);
        print $success ? $file : 'Unable to save the file.'; 
    ?>
    

    Update:

    <?php
        $img = $_POST['divcontent'];
        echo "<img src='$img' alt='image' />";
    
        define('UPLOAD_DIR', 'wp-content/uploads/2014/03/');
        $filteredData = substr($img, strpos($img, ",") + 1);
        $decodedData = base64_decode($filteredData);
    
        $file = UPLOAD_DIR . uniqid() . '.png';
    
        $fp = fopen($file,'wb');
        if ($fp) {
            fwrite($fp, $decodedData);
            print $file;
        } else {
            print 'Unable to save the file.';
        }
        fclose($fp);
    ?>
    

    I commented out the download part because I am not sure how exactly you call the ajax to save the file, so that part is omitted from my solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多