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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值