简体   繁体   English

使用 SVG 内的图像将 SVG 导出为 PNG

[英]export SVG to PNG with image inside SVG

I am trying to make a website where you can draw an image on top of another image using Raphael.js.我正在尝试创建一个网站,您可以在其中使用 Raphael.js 在另一个图像上绘制图像。 The drawing parts are finished, and I am able to export the lines as a png.绘图部分已完成,我可以将线条导出为 png。 I insert images to the SVG raphael generates by the function paper.image() ;我将图像插入到由函数paper.image()生成的 SVG raphael 中; unfortunately my export function doesn't include the imported image.不幸的是,我的导出功能不包括导入的图像。

These are the scripts I'm using, but I don't think I use them all.这些是我正在使用的脚本,但我认为我并没有全部使用它们。

<script src="../jquery-2.0.1.min.js"></script>
<script src="raphael-min.js"></script>
<script src="rgbcolor.js"></script>
<script src="canvg.js"></script>
<script src="StackBlur.js"></script>
<script src="svg.min.js"></script>

Here's the export-function $('#save').onmousedown...这是导出功能 $('#save').onmousedown...

var canvas = document.getElementById('canvas2');
var svg = document.getElementById('canvas');
svg = svg.innerHTML;
canvg(canvas, svg);
var img_url = canvas.toDataURL('image/png');
$('#converted_image').attr('src', img_url);
var img = canvas.toDataURL("image/png");
document.write('<img src="'+img+'"/>');

Here's how I import images by a button which represents the image $('#img1').onmousedown ...这是我通过代表图像$('#img1').onmousedown的按钮导入图像的方法...

paper.clear();
var c = paper.image("images/img1.png", 10, 10, 200, 200);

Here's how the output looks like in the dom-tree with the image and a white line as example.下面是输出在 dom-tree 中的样子,以图像和白线为例。

<div id="canvas">
    <svg height="300" version="1.1" width="300" 
         xmlns="http://www.w3.org/2000/svg"
         style="overflow: hidden; position: relative; " id="canvassvg">
        <desc>Created with Raphaël 2.1.0</desc>
        <defs></defs>
        <image x="10" y="10" width="200" height="200"
               preserveAspectRatio="none" href="images/img1.png">
        </image>
        <path style="stroke-linecap: round; stroke-linejoin: round; "
              fill="none" stroke="#ffffff" 
              d="M383,201L383,201L383,202L383,203"
              stroke-linecap="round" stroke-linejoin="round" stroke-width="4">
        </path>
    </svg>
</div>

Thank you very much for any reply, and please excuse my english.非常感谢您的任何回复,请原谅我的英语。

I've successfully done this using Batik, which you can download here: http://xmlgraphics.apache.org/batik/tools/rasterizer.html我已经使用蜡染成功完成了这项工作,您可以在此处下载: http : //xmlgraphics.apache.org/batik/tools/rasterizer.html

Once you have the SVG, it's just a matter of saving the svg to a file and passing it to batik, and you're done!获得 SVG 后,只需将 svg 保存到文件并将其传递给蜡染,就大功告成了! Outputs a very nice PNG (and even works when images are included remotely).输出一个非常漂亮的 PNG(甚至可以在远程包含图像时工作)。

Have you tried the Canvas drawImage() method?您是否尝试过 Canvas drawImage() 方法?

Example here: https://developer.mozilla.org/samples/canvas-tutorial/3_1_canvas_drawimage.html这里的例子: https : //developer.mozilla.org/samples/canvas-tutorial/3_1_canvas_drawimage.html

The answer proposed here worked for me! 这里提出的答案对我有用!

After defining your Raphael object, you should do something like:定义 Raphael 对象后,您应该执行以下操作:

raphaelObj.attr('xmlns:xlink',"http://www.w3.org/1999/xlink");

I THINK IT SHOULD BE WORK我认为它应该有效

 function convertsvg(selectors) { [].forEach.call(document.querySelectorAll(selectors), function (div) { try { var sourceImage; var imgs = document.getElementById('svgpng'), svg = div.querySelector('svg'), can = div.querySelector('canvas'), ctx = can.getContext('2d'); can.width =500; can.height = 550; sourceImage = new Image; sourceImage.width = can.width; sourceImage.height = can.height; sourceImage.onload = function () { ctx.drawImage(sourceImage,80,90); imgs.src = can.toDataURL(); }; sourceImage.src = svg ? svgDataURL(svg) :""; } catch (e) { console.log(e) } }); }} function svgDataURL(svg) { var svgAsXML = (new XMLSerializer).serializeToString(svg); return "data:image/svg+xml," + encodeURIComponent(svgAsXML); }
 <div id="forsvgtopng"> <div id="svgelemntdiv"> <svg id="mySVG" xmlns="http://www.w3.org/2000/svg" version="1.1"> <rect width="150" height="150" fill="rgb(0, 255, 0)" stroke-width="1" stroke="rgb(0, 0, 0)"/> </svg> </div> <div class="canvasdiv"> <canvas ></canvas> </div> <div class="pngdiv"> <img id="svgpng" /> </div> </div>

call the javascript function like convertsvg('#forsvgtopng')调用 javascript 函数,如 convertsvg('#forsvgtopng')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM