【问题标题】:Convert SVG with image pattern and clipPath to PNG将带有图像模式和剪辑路径的 SVG 转换为 PNG
【发布时间】:2015-12-21 09:45:01
【问题描述】:

我需要将 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;
?>

致命错误:在 /home/[文件路径]:10 中未捕获异常 'ImagickException' 并带有消息 'no decode delegate for this image format `' @ error/blob.c/BlobToImage/346' 堆栈跟踪:#0 /home/path to file: Imagick->readimageblob('small.svg') #1 {main} throw in /home/[path to file] on line 10

如果可能的话,我宁愿用 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>

【问题讨论】:

    标签: javascript svg png


    【解决方案1】:

    我最近也遇到了这个问题,至少在图案填充方面 - 这似乎是远程图像的问题,所以如果你将它们 base64 编码为数据 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 中)。

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2017-05-11
      相关资源
      最近更新 更多