将SVG与图像模式和clipPath转换为PNG

时间:2015-09-23 22:09:48

标签: javascript svg png

我需要将svg转换为png。我尝试使用此代码

<!DOCTYPE html>
<meta charset="utf-8">
<style>

path {
  stroke: #000;
  fill-opacity: .8;
}

</style>
<body>
    <div id="svg">
        <?php include 'small.svg'; ?> 
    </div>
    <button id="save">Save as Image</button>
    <h2>SVG dataurl:</h2>
    <div id="svgdataurl"></div>

    <h2>SVG converted to PNG dataurl via HTML5 CANVAS:</h2>
    <div id="pngdataurl"></div>

    <canvas width="960" height="500" style="display:none"></canvas>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );

var canvas = document.createElement( "canvas" );
var ctx = canvas.getContext( "2d" );

var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );

img.onload = function() {
    ctx.drawImage( img, 0, 0 );

    // Now is done
    console.log( canvas.toDataURL( "image/png" ) );
};

document.getElementById("pngdataurl").appendChild(img); 
</script>

但它确实有效。我很确定这是因为我使用图像模式,矩形和剪辑路径来生成我的SVG。我这样说,因为我只使用路径对象尝试了这个代码,它运行正常。我也尝试使用此代码的图像魔法

<?php 
    error_reporting(E_ALL);

    $svg ="small.svg";
    $mask = new Imagick('mask.png');

      $im = new Imagick();

      //$im->setBackgroundColor(new ImagickPixel('transparent'));
      $im->readImageBlob($svg);
      $im->setImageFormat("png32");
      $im->compositeImage( $mask, imagick::COMPOSITE_DEFAULT, 0, 0 );

      header('Content-type: image/png');
      echo $im;
?>

致命错误:未捕获异常'ImagickException',消息'此图像格式没有解码委托',@ home / blob.c / BlobToImage / 346'/ home / [文件路径]:10堆栈跟踪:#0 / home / path to file:Imagick-&gt; readimageblob('small.svg')#1 {main}在第10行/ home / [文件路径]中抛出

如果可能的话,我宁愿用js做这件事。请帮忙......我的老板现在真的很讨厌我。这是我的svg

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 3384.2 2608.6" enable-background="new 0 0 3384.2 2608.6" xml:space="preserve">
    <defs>
  <pattern id="img1" patternUnits="userSpaceOnUse" width="100%" height="100%">
    <image xlink:href="https://upload.wikimedia.org/wikipedia/commons/7/71/Weaved_truncated_square_tiling.png" x="0" y="0" width="100%" height="100%" />
  </pattern>
</defs>

<rect width="3384.2" height="2608.6" clip-path="url(#shirt)" fill="url(#img1)"  />
    <clipPath id="shirt">
    <path fill-rule="evenodd" clip-rule="evenodd" fill="url(#img1)"  d="[coordinates that are too long for this post ]"/>
        </clipPath> 
</svg>

1 个答案:

答案 0 :(得分:0)

我最近也遇到过这个问题,至少是模式填充 - 这似乎是远程图像的一个问题,所以如果你将它们编码为数据URL,它就可以正常工作:

var img = new Image();
img.onload = function () {
  var canvas = document.createElement('canvas');
  canvas.width = img.naturalWidth;
  canvas.height = img.naturalHeight;
  canvas.getContext('2d').drawImage(img, 0, 0);
  var patternImage = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  patternImage.setAttributeNS('http://www.w3.org/1999/xlink', 'href', canvas.toDataURL());
};
img.src = patternImageUrl;

然后,您可以在模式def中添加/更新此图像(或者执行此操作并将canvas.toDataURL()的结果硬编码到模式图像href中)。