将包含SVG元素的HTML转换为图像文件

将包含SVG元素的HTML转换为图像文件

问题描述:

我试图将html代码转换为图像文件(png,jpg,无论)。但是,我尝试了所有方法是行不通的,因为我的HTML代码有SVG元素那样:将包含SVG元素的HTML转换为图像文件

<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 150.000000 150.000000" preserveAspectRatio="xMidYMid meet" class="img30p icon-main-color"> 
    <g transform="translate(0.000000,150.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none"> 
    <path d="M572 1430 c-233 -62 -408 -227 -490 -459 -24 -69 -27 -89 -27 -221 0 -132 3 -152 27 -221 39 -110 92 -194 172 -275 81 -80 165 -133 275 -172 69 -24 89 -27 221 -27 132 0 152 3 221 27 110 39 194 92 275 172 80 81 133 165 172 275 24 69 27 89 27 221 0 132 -3 152 -27 221 -39 110 -92 194 -172 275 -81 81 -166 134 -275 171 -105 36 -291 42 -399 13z m376 -83 c100 -35 171 -80 245 -154 76 -76 126 -158 158 -255 20 -60 24 -94 24 -188 0 -94 -4 -128 -24 -188 -32 -97 -82 -179 -158 -255 -73 -74 -144 -119 -245 -155 -63 -23 -89 -26 -193 -27 -100 0 -132 4 -193 24 -368 121 -544 530 -377 876 79 164 240 294 421 340 92 23 250 15 342 -18z"></path> 
    <path d="M873 788 l-183 -183 -100 100 c-83 83 -103 98 -117 89 -48 -30 -44 -38 90 -171 l127 -128 212 212 212 212 -24 26 c-13 14 -26 25 -29 25 -3 0 -88 -82 -188 -182z"></path> 
    </g> 
</svg> 

我试图用Html2Canvas和不工作:

<html> 
<head> 
<meta charset="utf-8"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript" src="html2canvas.js"></script> 
<script> 

$(function(){ 
    $('#save_image_locally').click(function(){ 
     html2canvas($('#imagesave'), { 
      onrendered: function (canvas) { 
       var a = document.createElement('a'); 
       a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream"); 
       a.download = 'test.jpg'; 
       a.click(); 
      } 
     }); 
    }); 
}); 

</script> 
</head> 
<body> 

<div id="imagesave" style="width: 200px"> 
    <svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 150.000000 150.000000" preserveAspectRatio="xMidYMid meet" class="img30p icon-main-color"> 
    <g transform="translate(0.000000,150.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none"> 
    <path d="M572 1430 c-233 -62 -408 -227 -490 -459 -24 -69 -27 -89 -27 -221 0 -132 3 -152 27 -221 39 -110 92 -194 172 -275 81 -80 165 -133 275 -172 69 -24 89 -27 221 -27 132 0 152 3 221 27 110 39 194 92 275 172 80 81 133 165 172 275 24 69 27 89 27 221 0 132 -3 152 -27 221 -39 110 -92 194 -172 275 -81 81 -166 134 -275 171 -105 36 -291 42 -399 13z m376 -83 c100 -35 171 -80 245 -154 76 -76 126 -158 158 -255 20 -60 24 -94 24 -188 0 -94 -4 -128 -24 -188 -32 -97 -82 -179 -158 -255 -73 -74 -144 -119 -245 -155 -63 -23 -89 -26 -193 -27 -100 0 -132 4 -193 24 -368 121 -544 530 -377 876 79 164 240 294 421 340 92 23 250 15 342 -18z"></path> 
    <path d="M873 788 l-183 -183 -100 100 c-83 83 -103 98 -117 89 -48 -30 -44 -38 90 -171 l127 -128 212 212 212 212 -24 26 c-13 14 -26 25 -29 25 -3 0 -88 -82 -188 -182z"></path> 
    </g> 
    </svg> 
</div> 

<input type="button" id="save_image_locally" value="click"/> 
</body> 
</html> 

没有人有任何想法使它工作?或者使用PHP的一些方法?谢谢。

+0

的可能的复制[转换SVG图像(JPEG,PNG等)在浏览器]看到这个(http://*.com/questions/3975499/convert- svg-to-image-jpeg-png-etc-in-the-browser) – Terry

将SVG转换为图像是种类troublesome。通常的策略是首先将SVG转换为canvas元素,然后转换为图像文件。

为此,您将需要canvg library

让我们开始吧。首先,你需要使用canvg您的SVG到serialiaze您的SVG

var svgImage = $('#imagesave').children('svg')[0]; 
var serializer = new XMLSerializer(); 
var str = serializer.serializeToString(svgImage); 

然后,创建一个画布,并将其插入到DOM现在

var $canvas = $('<canvas/>'); 
$canvas.attr('width', '150px;') 
$canvas.attr('height', '150px;') 
$canvas.appendTo('body'); 

, “漆” 画布

canvg($canvas.get(0), str); 

,然后将图像保存

html2canvas($canvas, { 
    onrendered: function (canvas) { 
    var a = document.createElement('a'); 
    a.href = canvas.toDataURL(); 
    a.download = 'test.png'; 
    a.click(); 
    $canvas.remove(); //removes canvas from body 
    } 
}); 

您可以完全在这个jsFiddle

+1

不可思议!这十分完美!非常感谢我的朋友,它非常有帮助! –

+0

高兴地帮助:) – matheusr

我的建议是创建你需要离开你的SVG代码由这样不同的文件类型:

您可以使用Inkscape的导出功能,通过先导入您的SVG代码到Inkscape中创建一个PNG文件。然后,使用如Paint这样的程序,打开新创建的.png文件并将副本另存为.jpg。现在你有两个图像文件准备好保存。

我不确定是否有其他方式通过网页将svg图像保存为多种文件类型。不过,这是一个好主意,所以也许有一些现有的开源。