簡體   English   中英

如何使用JavaScript將渲染的網頁保存為圖像?

[英]How to save a rendered webpage as image with JavaScript?

在我的應用程序中,我必須提供“另存為圖像”按鈕。 我想將我在網頁上呈現的HTML保存為JavaScript中的圖像。 它是一個webapp,將用於桌面/平板電腦/手機的瀏覽器。 如何將渲染的HTML保存為圖像?

看看html2canvas 一個javascript框架,用於在canvas元素上呈現頁面內容。 將畫布保存為圖像非常簡單:

var canvas = document.getElementById("mycanvas");
var img    = canvas.toDataURL("image/png");
document.write('<img src="'+img+'"/>');

資源

你的問題非常不完整。 首先,是移動應用程序還是桌面應用程序? 在這兩種情況下,問題的解決方案將在很大程度上取決於呈現頁面的HTML引擎:例如,Webkit,Geko / Firefox,Trident / IE都有自己的方法來生成要保存為圖像的視圖。

無論如何,你可以開始看看這個Firefox插件是如何工作的: https//addons.mozilla.org/it/firefox/addon/pagesaver/

它應該做你想要實現的,尋找它的源代碼。

使用http://html2canvas.hertzen.com/

的index.php

<style>.wrap{background:white;padding:0 0 16px 0;width:1500px;}.colour{width:1500px;position:relative;height:1400px;}p.footer{text-align:center;font-family:arial;line-height:1;font-size:28px;color:#333;}.wrap img{height:389px;width:389px;position:absolute;bottom:0;right:0;left:0;top:0;margin:auto;}p.invert{position:absolute;top:300px;left:0;right:0;text-align:center;display:block;font-family:arial;font-size:48px;padding:0 40px;filter:invert(0%);}p.inverted{-webkit-filter: invert(100%);filter:invert(100%);}</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>
<script type="text/javascript" src="js/jquery.plugin.html2canvas.js"></script>
<?php // Open our CSV File
         $colours = fopen('colours.csv', 'r');

      // Reset Count
         $i=0;

      // Loop Through the Colours
         while (($colour = fgetcsv($colours)) !== FALSE){

          // Get the First Item and display some HTML
          if($i==0){ ?>
          <div id="target" class="wrap">
           <div class="colour" style="background:<?php echo $colour[0]; ?>">
            <img src="paragon2.png" alt="Image" />
            <p class="invert inverted" style="filter:invert(100%);"><?php echo $colour[1]; ?></p>
           </div>
           <p class="footer"><?php echo $colour[1]; ?></p>
          </div>
          <form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
           <input type="hidden" name="img_val" id="img_val" value="" />
           <input type="hidden" name="filename" id="filename" value="<?php echo $colour[0].".png"; ?>" />
          </form>
         <?php } // Count
              $i++;
             } // Loop

      // Close the CSV File
         fclose($colours); ?>

<script type="text/javascript">
 $(window).load(function () {
  $('#target').html2canvas({
   onrendered: function (canvas) {
   $('#img_val').val(canvas.toDataURL("image/png"));
   document.getElementById("myForm").submit();
  }
 });
});
</script>

save.php

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

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

      // Save the image
         file_put_contents("IMG2/".$filename, $unencodedData);

      // Open the CSV File
         $file = fopen('colours.csv', 'r');

       // Loop through the colours
          while (($line = fgetcsv($file)) !== FALSE) {
          // Store every line in an array
          $data[] = $line;
          }

       // Remove the first element from the stored array
          array_shift($data);

       // Write remaining lines to file
          foreach ($data as $fields){
           fputcsv($file, $fields);
          }

       // Close the File   
          fclose($file);

       // Redirect and start again!
          header( 'Location:/' ) ;  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM