2013-10-16 55 views
0

我有一個網頁有幾個圖形。 圖形已使用Google Chart API構建。 圖形在SVG中,我想要一個按鈕,當我點擊它時,生成一個PNG圖像並用「另存爲」(保存圖像)打開窗口。 我不明白爲什麼這不工作:(svg到png的下載php

HTML文件

<script type="text/javascript" src="jquery.min.js"></script> 
    <script type="text/javascript" src="rgbcolor.js"></script> 
    <script type="text/javascript" src="canvg.js"></script> 
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
     google.load("visualization", "1", {packages:["corechart"]}); 
     google.setOnLoadCallback(drawChart); 
     function drawChart() { 
      var data = google.visualization.arrayToDataTable([ 
       ['Task', 'Hours per Day'], 
       ['Work', 11], 
       ['Eat',  2], 
       ['Commute', 2], 
       ['Watch TV', 2], 
       ['Sleep', 7], 
       ['teste 1', 5], 
       ['teste 2', 3], 
       ['teste 3', 8], 
       ['teste 4', 4], 
       ['teste 5', 7] 
      ]); 

      var options = { 
       title: 'My Daily Activities' 
      }; 

      var chart = new google.visualization.PieChart(document.getElementById('svg')); 
      chart.draw(data, options); 
     } 
    </script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#converter').click(function (event){ 

       var canvas = document.getElementById("canvas"); 
        var svg = $("#svg :last-child").html().replace(/>\s+/g, ">").replace(/\s+</g, "<").replace(" xlink=", " xmlns:xlink=").replace(/\shref=/g, " xlink:href="); 
        canvg(canvas, svg); 

       var data = canvas.toDataURL("image/png"); 
       $('#imagem').attr('src', data); 
       $('#canvas').remove(); 



       // Send the screenshot to PHP to save it on the server 
       var url = 'export.php'; 
       $.ajax({ 
        type: "POST", 
        url: url, 
        dataType: 'text', 
        data: { 
         base64data : data 
        } 
       }); 

      }); 
     }); 
    </script> 

</head> 

<body> 

    <h1>Convert SVG in to PNG Imagem (Javascript)</h1> 

    <div id="apresentacao"> 
     <div class="esquerda"> 
      <h2>SVG</h2> 
      <div id="svg"> 

      </div> 
     </div> 

     <div class="botao"> 
      <input type="button" id="converter" value="Converter" /> 
     </div> 

     <div class="direita"> 
      <h2>PNG</h2> 
      <div class="fundo"> 
       <canvas id="canvas" width="200px" height="200px"></canvas> 
       <img id="imagem"/> 
      </div> 
     </div> 

    </div> 
</body> 

PHP文件

# we are a PNG image 
header('Content-type: image/png'); 

# we are an attachment (eg download), and we have a name 
header('Content-Disposition: attachment; filename="imagem.png"'); 

#capture, replace any spaces w/ plusses, and decode 
$encoded = $_POST['base64data']; 
$encoded = str_replace(' ', '+', $encoded); 
$decoded = base64_decode($encoded); 

#write decoded data 
echo $decoded; 
+0

替換空格的目的是什麼?有效的base64字符串不會包含任何空格。 –

+0

[將Google圖表保存爲圖片]的可能重複(http://stackoverflow.com/questions/13824096/save-google-charts-as-a-image) –

+0

http://chartstoimage.eu/我相信這是你在找什麼:) – paranoid

回答

0

這個問題已經被問在計算器上谷歌快速搜索發現以下內容: https://stackoverflow.com/a/13824542/2332336

在PHP文件中,圖像數據發佈爲Base64編碼字符串,您可以直接將其保存在數據庫中se或您服務器上的文件:

<?php 

// Get the request 
$data = $_GET['data']; 

// Save image to file 
$h = fopen('chart.png', 'w'); 
fwrite($h, base64_decode($data)); 
fclose($h); 

?> 
+0

你需要將圖像保存在服務器上?我需要的是在客戶端(瀏覽器)下載 –